Presentation is loading. Please wait.

Presentation is loading. Please wait.

L what is a void-function? l what is a boolean function? l is it possible for a function to have no parameters? l what is program stack? function frame?

Similar presentations


Presentation on theme: "L what is a void-function? l what is a boolean function? l is it possible for a function to have no parameters? l what is program stack? function frame?"— Presentation transcript:

1 l what is a void-function? l what is a boolean function? l is it possible for a function to have no parameters? l what is program stack? function frame? l what’s call-by-value? l what’s call-by-reference? l can expression be passed by reference? Previous Lecture Review

2 Arrays

3 l programmers sometimes need to manipulate variables of similar nature l aggregate construct – a construct that allows manipulation of multiple entities as a single element n array is an aggregate construct l array - a list of similar variables with two names: n name of the array it is the same for all variables in the array index (or subscript) - different for every variable, put in square brackets [] example: array score may have following variables: …, score[2], score[3], score[4], … l variables in the array are indexed variables or elements of the array l all variables in the array have the same type called base type of the array l the number of indexed variables is the size of the array array is declared as follows: int score[5]; the number in brackets – 5 is array size l the indexes start from 0. Statement above declared the following variables: score[0], score[1], score[2], score[3], score[4] note, score[5] is not there! l non-array (regular) variable is scalar variable Array Definition

4 Array Terms Again array base type baseType id [ sizeExpession ] ; array name expression specifies number of array elements double X[100]; // subscripts are 0 through 99 !

5 l indexed variables can be used anywhere a scalar variable can be: cin >> score[4] >> score[2]; max = score[4] + score[2]; score [4]=max; cout << score[2] << ” ” << score[4]; l index can be any expression: int student=2; score[student]=99; score[student+1]=100; cout << score[1] << ” ” << score[student]; l loops are ideal for arrays: for (int index=0; index < arraySize; ++index){ // do something with myarray[index] } Array Usage

6 int main(){ int numbers[5]; // array of numbers cout << "Enter the numbers: "; for(int i=0; i < 5; ++i) cin >> numbers[i]; // finding the minimum int minimum=numbers[0]; // assume the first element for (int i=1; i < 5; ++i) // start from second if (minimum > numbers[i]) minimum=numbers[i]; cout << "The smallest number is: " << minimum << endl; } Array with Loops Example

7 l array elements are placed in memory consequently: int a[10], myvar=20; l no range checking is done on index l referring to index that is not present in the array results in an out of range error n it is a logical error (bug/run-time error) with unpredictable consequences. n these examples are both out-of-rage errors a[10]=55; a[myvar]=5; Arrays in Memory, Index Out of Range -- a[4]a[5]a[6]a[3]a[0]a[2]a[8]a[9]a[7]a[1] -- 20 myvar other vars array

8 l assigning values to the array variables at declaration is initalization int a[10]={0, 10, 20, 30, 40, 50, 60, 70, 80, 90}; l do not have to initialize all elements (rest hold arbitrary values): int a[10]={0, 10, 20}; l may skip array size at initialization (size computed to hold all values) int a[]={10,20,30}; // array size is three l using global named constants for array size is good style: int score[NUM_STUDENTS]; Initializing Array

9 l array elements can be passed as arguments to functions: int i=5, n, a[10]; myfunc(n);// ordinary variable as argument myfunc(a[3]); // element as argument myfunc(a[i]); // which element are we passing? l entire arrays can be passed as arguments n always passed by reference (no & needed) n function does not know array size, it is usually passed as a separate variable –alternatively – make size a global constant, using a literal constant is bad style l example: void fillUp(int [], int); // prototype void fillUp(int a[], int size){ // definition cout << ”Enter ” << size << ” numbers:”; for(int i=0; i < size; ++i) cin >> a[i]; } fillUp(score, 5); // invocation, note no brackets Arrays and Functions

10 l array is always passed by reference n may lead to accidental value changes – run time error const type modifier specifies that the parameter shall not be modified in the function are not modified in the function n that is, it turns accidental value change into syntactic error n this allows the compiler to detect it an alert the programmer void printArray(const int [], int); // prototype void printArray(const int a[], int size){ // definition a[0] = 33; // not allowed, syntax error!!! for(int i=0; i < size; ++i) cout << a[i]; } l function prototype and head have to agree on modifiers l function invocation is the same regardless of modifiers const Parameter Type Modifier


Download ppt "L what is a void-function? l what is a boolean function? l is it possible for a function to have no parameters? l what is program stack? function frame?"

Similar presentations


Ads by Google