4.
ARRAYS AND STRINGS
An array is a collection of identical data objects, which are stored in consecutive memory locations
under a common heading or a variable name. In other words, an array is a group or a table of values
referred to by the same name.
The individual values in array are called elements. Array elements are also variables.
4.2. Properties of arrays
Arrays in C++ are zero-bounded; that is the index of the first element in the array is 0 and
the
last element is N-1, where N is the size of the array.
It is illegal to refer to an element outside of the array bounds, and your program will crash
or
have unexpected results, depending on the compiler.
Array can only hold values of one type
All array subscripts begin with zero.
4.3. Array declaration
Declaring the name and type of an array and setting the number of elements in an array is called
dimensioning the array.
So the general syntax for the declaration is: DataTypename arrayname [array size];
4.4. Initializing Arrays
Thus, if in the global scope we declare:
int day [5]; //every element of day will be set initially to 0:
For example: int day [5] = { 16, 2, 77, 40, 12071 };
4.6. Multidimensional arrays
The following figure illustrates a two dimensional array, a. The array contains three rows and four columns,
so it is said to be a 3-by-4 array. In general, an array with m rows and n columns is called an m-by-n array.
Reading assignment about string?
5. FUNCTIONS
A function provides a convenient way of packaging a computational recipe, so that it can be used as often
as required.
Therefore, a function is a block of code designed to tackle a specific problem.
5.2 Advantage of functions in C There are many advantages of functions.
1) Code Reusability
2). Code optimization
5.3. Summarized function basics.
Every function must have a name.
Function names are made up and assigned by the programmer following the same rules that apply
to naming variables. They can contain up to 32 characters, they must begin with a letter, and they
can consist of letters, numbers, and the underscore (_) character.
All function names have one set of parenthesis immediately following them. This helps you (and C+
+ compiler) differentiate them from variables.
The body of each function, starting immediately after parenthesis of the function name, must be
enclosed by braces.
1. Library Functions (Built in functions): are the functions which are declared in the C++ header files
such as strcmp(), strlen(), sqrt(),pow(),.. etc.
2. User-defined functions: are the functions that are created by the C++ programmer, so that he/she
can use it many times. It reduces complexity of a big program and optimizes the code.
5.3. Declaring, defining and calling functions
5.3.1. Declaring function:
The interface of a function (also called its prototype) specifies how it may be used. It consists
of three entities:
The function return type. This specifies the type of value the function returns. A function
which returns nothing should have a return type void.
The function name. this is simply a unique identifier
The function parameters (also called its signature). This is a set of zero or more typed
identifiers used for passing values to and from the function.
5.3.2. Defining a function:
A function definition consists of two parts: interface (prototype) & body. The brace of a
function contains the computational steps (statements) that computerize the function. The
definition consists of a line called the decelerator.
If function definition is done before the main function, then there is no need to put the
prototype, otherwise the prototype should be scripted before the main function starts.
5.3.3. Calling the function:
Using a function involves ‗calling‘ it.
Calling a function means making the instruction of the function to be executed.
A function call consists of the function name followed by the call operator brackets ‗()‘,
inside which zero or more comma-separated arguments appear.
Reading Assignment: Call by value and call by reference in C++