Presentation is loading. Please wait.

Presentation is loading. Please wait.

Passing Arguments to a Function

Similar presentations


Presentation on theme: "Passing Arguments to a Function"— Presentation transcript:

1 Passing Arguments to a Function
There are two mechanisms used generally in C++ to pass arguments to functions. One is called the pass-by-value method, and the other is called the pass-by-reference method. Let’s look into the pass-by-value method first. Passing Arguments to a Function tMyn

2 Let’s use as an example our power() function:
With this mechanism for transferring data to a function, the variables or constants that you specify as arguments are not actually passed to a function at all. Instead, just as with the return value, copies of the arguments are created, and these copies are used as the values to be transferred. Let’s use as an example our power() function: Passing Arguments to a Function tMyn

3 double result=power(value, index);
... double value=20.0; int index=3; double result=power(value, index); Copy of index index 3 3 Copy of value value 20.0 20.0 double power(double x, int n) { The code here cannot access the original values of index and value. } Passing Arguments to a Function tMyn

4 We can demonstrate the effects of this with a simple example:
Each time you call the power() function, the compiler arranges for copies of the arguments that you specify to be stored in a temporary location in memory (in stack). Once the execution of the function is complete, the copies of the arguments are discarded. We can demonstrate the effects of this with a simple example: Passing Arguments to a Function tMyn

5 #include <iostream> using namespace System; using namespace std;
#include "stdafx.h" #include <iostream> using namespace System; using namespace std; double changeIt(double it); int main(array<System::String ^> ^args) { double it=5.0; double result=changeIt(it); cout<<"After function execution, " <<"it="<<it<<"."<<endl; cout<<"Result returned is " <<result<<"."<<endl; return 0; } Passing Arguments to a Function tMyn

6 double changeIt(double it) { it+=10.0;
cout<<"Within function, it=" <<it<<"."<<endl; return it; } Passing Arguments to a Function tMyn

7 double result=changeIt(it);
Copy of it A copy is made when the function is called Variable it in main() 5 5 This is part of main(): This will increment the copy by 10. It always refers to the copy of the argument. double result=changeIt(it); double changeIt(double it) { it+=10.0; cout<<”Within function, it= ” <<it<<endl; return it; } The copy of the value returned is used in main(). The variable it in changeIt() has been discarded and no longer exists at this point. A copy of the value to be returned is made. The variable it has the value 15 when the copy is made. 15 Passing Arguments to a Function tMyn

8 But suppose that we do want to modify values in the calling function.
This pass-by-value mechanism provides quite a lot of security to the calling program. But suppose that we do want to modify values in the calling function. When you use a pointer as an argument, the pass-by-value mechanism operates just as it did before. However, a pointer contains the address of another variable, and a copy of the pointer will point to exactly the same place in memory as the original!: Passing Arguments to a Function tMyn

9 #include <iostream> using namespace System; using namespace std;
#include "stdafx.h" #include <iostream> using namespace System; using namespace std; double changeIt(double* pointerToIt); int main(array<System::String ^> ^args) { double it=5.0; double result=changeIt(&it); cout<<"After function execution, it=" <<it<<"."<<endl <<"Result returned is " <<result<<"."<<endl; return 0; } We don’t need to create a pointer variable to hold the address of it. Since we only need the address to pass to the function, we are able to use the address-of operator in the function call. Passing Arguments to a Function tMyn

10 double changeIt(double* point) { *point+=10.0;
cout<<"Within function, *point=" <<*point<<"."<<endl; return *point; } Passing Arguments to a Function tMyn

11 double result=changeIt(&it);
A copy is made when the function is called. Address of it in main(): 64FDEC 64FDEC The address of the variable it is passed to the function changeIt(): 5 This is part of main():: This will increment the original variable, it, by 10. double result=changeIt(&it); double changeIt(double* point) { *point+=10.0; cout << ”Within function, *point=” << *pointit<<endl; return *point; } A copy of the value to be returned is made. 15 The variable it in the function main() has the value 15 when the copy is made. Passing Arguments to a Function tMyn

12 Passing arrays to a function:
Because an array name can be treated as an address, you can also pass an array as an argument to a function, just by using its name. In this case, the address of the array is copied and passed to the function being called, providing one advantage: because the function called does not deal with the original array address but with a copy of it, the function can treat the parameter as a pointer in the fullest sense. First, passing an array as a function argument (using normal array notation): Passing Arguments to a Function tMyn

13 #include <iostream> using namespace System; using namespace std;
#include "stdafx.h" #include <iostream> using namespace System; using namespace std; double average(double array[], int count); int main(array<System::String ^> ^args) { double values[]={1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.1, 10.2}; cout<<"Average=" <<average(values, sizeof values/sizeof values[0]) <<endl; return 0; } Passing Arguments to a Function tMyn

14 The type of the first parameter is specified as an array
double average(double array[], int count) { double sum=0.0; for (int i=0; i<count; i++) sum+=array[i]; return sum/count; } The type of the first parameter is specified as an array of type double. It is not possible to specify the size of the array between the square brackets, because the size of the first dimension of an array is not part of its type. Passing Arguments to a Function tMyn

15 We can modify the example above (average() function) to work with pointer notation throughout, even though we are using an array: Passing Arguments to a Function tMyn

16 #include <iostream> using namespace System; using namespace std;
#include "stdafx.h" #include <iostream> using namespace System; using namespace std; double average(double* array, int count); int main(array<System::String ^> ^args) { double values[]={1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.1, 10.2}; cout<<"Average=" <<average(values, sizeof values/sizeof values[0]) <<endl; return 0; } Passing Arguments to a Function tMyn

17 The pass-by-value mechanism makes a copy of the original
double average(double* array, int count) { double sum=0.0; for (int i=0; i<count; i++) sum+=*array++; return sum/count; } The pass-by-value mechanism makes a copy of the original array address, and passes that to the function. We are modifying the copy here, and the original array address will be quite unaffected!! Passing Arguments to a Function tMyn

18 When you’re defining a multidimensional array as a parameter, you should omit the first dimension’s size (the size of the first dimension is not part of the type definition). The function will then need some way of knowing the size of the first dimension, and for this you can add a second parameter to specify the size of the first dimension of the array. Passing Arguments to a Function tMyn

19 #include <iostream> using namespace System; using namespace std;
#include "stdafx.h" #include <iostream> using namespace System; using namespace std; double totalSum(double array[][4], int rows); int main(array<System::String ^> ^args) { double values[3][4]= {1.2, 2.3, 3.4, 4.5}, {5.6, 6.7, 7.8, 8.9}, {9.1, 10.2, 11.3, 12.4} }; Passing Arguments to a Function tMyn

20 The computation in the function is simply a nested for
cout<<"Adding every element together gives " <<totalSum(values, sizeof values/sizeof values[0]) <<"."<<endl; return 0; } double totalSum(double array[][4], int rows) { double sum=0.0; for(int i=0; i<rows; i++) for(int j=0; j<4; j++) sum+=array[i][j]; return sum; The computation in the function is simply a nested for loop, with the inner loop summing elements of a single row, and the outer loop repeating this for each row. Passing Arguments to a Function tMyn

21 Passing Arguments to a Function
tMyn

22 Usually when working with multidimensional arrays it is good idea to use array notation instead of pointer notation. Let’s demonstrate this principle: Passing Arguments to a Function tMyn

23 #include <iostream> using namespace System; using namespace std;
#include "stdafx.h" #include <iostream> using namespace System; using namespace std; double totalSum(double (*)[4], int); int main(array<System::String ^> ^args) { double values[3][4]= {1.2, 2.3, 3.4, 4.5}, {5.6, 6.7, 7.8, 8.9}, {9.1, 10.2, 11.3, 12.4} }; Passing Arguments to a Function tMyn

24 cout<<"Adding every element together gives "
<<totalSum(values, sizeof values/sizeof values[0]) <<"."<<endl; return 0; } double totalSum(double (*array)[4], int rows) { double sum=0.0; for(int i=0; i<rows; i++) for(int j=0; j<4; j++) sum+=*(*(array+i)+j); return sum; Passing Arguments to a Function tMyn

25 The pass-by-reference mechanism:
A reference is simply an alias for another variable. When you specify a function parameter as a reference type, your function will use the pass-by-reference mechanism for transferring the argument. When the function is called, the argument corresponding to a reference parameter is not copied, because the parameter name simply becomes an alias for the argument value in the calling program. Wherever the parameter name is used in the body of the function, it will access the argument value in the calling function directly. Passing Arguments to a Function tMyn

26 The reference parameter, value, is initialized with number, so value
Calling function in main(): The reference parameter, value, is initialized with number, so value becomes an alias for number. int main() { int number=20; cout<<changeIt(number); …. return 0: } int changeIt(int& value) { value+=10; return value; } This refers to number directly Function with a reference parameter: The return type is int so a copy value, that is, a copy of number in this case, is returned to the calling function. Passing Arguments to a Function tMyn

27 Specifying a function parameter as a reference has two major effects.
Whenever the changeIt() function is called, the reference parameter, value, is initialized with the specified argument, so while changeIt() is executing, value becomes an alternative name for the variable that was passed as an argument, number. If you call the function again later with a different argument, value will then become an alias for that argument. Specifying a function parameter as a reference has two major effects. First, the argument is not copied, so the function accesses the argument in the caller directly. Passing Arguments to a Function tMyn

28 Second, the absence of the copying process makes the function call faster.
Using the pass-by-reference mechanism you can modify the original argument within your function, but the syntax of references makes doing so less obvious than when you use pointers for the same purpose: Passing Arguments to a Function tMyn

29 Function with a reference parameter:
pointer parameter: int changeIt(int* value) { * value+=10; return *value; } An address is passed to the function, indicating clearly the possibility that the argument’s value could be altered by the function!! ... int number=20; cout<<changeIt(&number); Function with a reference parameter: int changeIt(int& value) { value+=10; return value; } A value is passed in the form of a reference; this gives no indication of the possibility that the argument’s value could be altered by the function. int number=20; cout<<changeIt(number); Passing Arguments to a Function tMyn

30 #include <iostream> using namespace System; using namespace std;
#include "stdafx.h" #include <iostream> using namespace System; using namespace std; int larger(int&, int&); int main(array<System::String ^> ^args) { int value1=10; int value2=20; cout<<"The larger one is " <<larger(value1, value2) <<"."<<endl; return 0; } Passing Arguments to a Function tMyn

31 int larger(int& first, int& second) { if(first>second)
return first; else return second; } Passing Arguments to a Function tMyn

32 An important difference between a pointer and a reference is that a pointer can be null, whereas a reference always refers to something. When you declare a reference, it must always be initialized with the name of the variable for which it is an alias. Passing Arguments to a Function tMyn


Download ppt "Passing Arguments to a Function"

Similar presentations


Ads by Google