Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.

Similar presentations


Presentation on theme: "1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies."— Presentation transcript:

1 1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies do not affect the values of the original variables. Call by reference The caller supplies the address of the actual parameter rather than a copy of its value. This allows the caller to modify the values of the original variables. It is often used when we want to avoid copying "large" data. Using the keyword 'const' allows us to pass values by reference without letting the called function modify them.

2 2 Example: Call by value #include using std::cout; using std::endl; void print_num (int num); int main () { int x = 4; print_num(x); return 0; } void print_num(int num) { cout << num << endl; } The value of x is copied into num. Program output: 4

3 3 Example: Call by value #include using std::cout; using std::endl; void update_num (int num); int main () { int num = 4; print_num(num); cout << num << endl; return 0; } void update_num(int num) { num++; cout << num << endl; } Completely different variables that happen to have the same name. They exist in different scopes, so there is no conflict. The value of num is copied into num. The modification of num inside update_num() has no effect on the num defined in main. Program output: 5 4

4 4 Example: Call by value #include using std::cout; using std::endl; void swap (int a, int b); int main () { int num1 = 4, num2 = 12; swap(num1, num2); cout << num1 << endl << num2 << endl; return 0; } void swap (int a, int b) { int temp = a; a = b; b = temp; } Program output: 4 12 Since num1 and num2 were passed by value, the changes to a and b had no effect on num1 and num2. The numbers have not been swapped.

5 5 Example: Call by reference #include using std::cout; using std::endl; void swap (int& a, int& b); int main () { int num1 = 4, num2 = 12; swap(num1, num2); cout << num1 << endl << num2 << endl; return 0; } void swap (int& a, int& b) { int temp = a; a = b; b = temp; } Program output: 12 4 This time, the modifications to a and b have affected the values of num1 and num2, because they were performed on the values stored in the addresses of num1 and num2.

6 6 Example: Call by reference #include using std::cout; void freeze (int& temp); int main () { int temperature; freeze(temperature); cout << temperature; return 0; } void freeze (int& temp) { temp = 32; } Program output: 32 This is an example of using call by reference to initialize a value from inside a called function. It is often used when the called function needs to modify several values and pass them back to the caller.

7 7 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results in a difficult to maintain and error-prone program. Solution: use arrays Array = a named location in memory for many values of the same type, stored sequentially and accessed through an index Compare: variable = a named location in memory for one value.

8 8 Arrays Think of an array a sequence of boxes, each one holding a value Example: An array named scores that holds the quiz scores of 5 students: element 0element 1element 2element 3element 4 11 10 12 9 11 scores

9 9 Arrays Array declaration syntax element_type array_name [ SIZE ] ; element_type is the type of the elements stored in the array. array_name is the name of the array. Naming rules are the same as with variables. SIZE must be a positive integer constant (known at compile time) No variables allowed(*) You cannot change the array size at runtime Use a #define directive to define the size. (*) The new standard of C, C99, allows variable-size arrays. C++ does not.

10 10 Arrays Declaring and initializing an array Example 1: int scores[5]; The elements of scores have not yet been initialized. They may be initialized one at a time (usually in a for- loop: for (int i = 0; i < 5; i++) { cout << “Enter score: “; cin >> scores[i]; } Example 2: int scores[5] = {11, 10, 12, 9, 11}; The elements are initialized at declaration time. In this case, the size of the array may be omitted from the declaration : i nt scores[ ] = {11, 10, 12, 9, 11};

11 11 Arrays Accessing the elements of an array. To access the element at index i specify the name of the array and the index enclosed in brackets. Example: scores[3] = 11; CAUTION! Indexing always starts at zero and ends at SIZE-1 int scores[5]; scores[0] = 11; scores[1] = 12; scores[2] = 10; scores[3] = 9; scores[4] = 11; The compiler will NOT catch a wrong index. Typing scores[10] or scores[-1] can corrupt your data.

12 12 Arrays How exactly do they work? int scores[5]; says: I need a chunk of memory big enough to hold 5 integer variables. That’s a total of 5*4 = 20 bytes. scores itself represents the memory address where this chunk of memory begins. Think of it as a special kind of constant that holds the address of a location in memory. Example: int main () { int arr[2]; cout << arr << endl; return 0; } The output of this program is: 0xbfffb240 (the address of arr in hexadecimal)

13 13 Arrays How exactly do they work? scores[i] represents the contents in box #i starting at address scores. In other words, it represents the contents at address scores + i * sizeof(int)


Download ppt "1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies."

Similar presentations


Ads by Google