Presentation is loading. Please wait.

Presentation is loading. Please wait.

Parameter Passing to Functions in C. C Parameter passing Review of by-value/by-reference.

Similar presentations


Presentation on theme: "Parameter Passing to Functions in C. C Parameter passing Review of by-value/by-reference."— Presentation transcript:

1 Parameter Passing to Functions in C

2 C Parameter passing Review of by-value/by-reference

3 By-Value: The value of a variable is sent to function. The actual parameter cannot be changed by function. "In" parameter

4 void f (int x) // x is an integer { x=x+1; }

5 For example int z = 5; f(z); This wont change z. Only interested in the value of z. What happens is a temporary copy of z is sent to the function. This copy is changed, the value returned and the copy then deleted If we want change we have to pass arguments by reference.

6 By-Reference An address gets sent to function. The function can change the values at that address "Out" or "In-Out“ parameter manipulation

7 Sending by reference in C requires the use of the pointer symbol (*). void h(int *p_x) // p_x is an address where an integer is stored. { *p_x = *p_x + 1; }

8 Can call a by-reference function using the address operator & int x=9; f(x) ; // f cannot change value of x. h(&x); // h can change the value of x

9 Array parameters are similar to Java in effect int array[10]; void g(int arr[]) g (array);

10 Trace the following:

11 Program main() { int x=6; int arr[5]; for (int i=0;i<5;i++) arr[i]=i; f(x); g(arr); h(&x); h(&(arr[4])); } void f(int z) { z=z+1; } void g(int a[]) { a[2]=8; } void h(int *z) { *z = *z+1; }

12 Program main() { int x=6; int arr[5] for (int i=0;i<5;i++) arr[i]=i; f(x); g(arr); h(&x); h(&(arr[4])); } X= 6 arr01234 f(x) x=6 g(arr) 01834 h(&X) x=7 h(&(arr[4])); 01835

13 Exercise Write 2 functions to calculate the square of integer X One which passes parameters by value The other by Reference X^2

14 Pass by Value Square int square_by_value(int x) { return x^2; } Note the return which passes a result back out based on the input value No Change has taken place to the input varaiable. Only its value has been used.

15 Pass by Reference Square void square_by_reference(int *x) { *x= *x^2; } Note there is no return type. However the value pointed at the input address is changed.

16 Example program to demonstrate the passing of an array #include int maximum( int [] ); main() { int values[5], i, max; printf("Enter 5 numbers\n"); for( i = 0; i < 5; ++i ) scanf("%d", &values[i] ); max = maximum( values ); printf("\nMaximum value is %d\n", max ); }

17 Maximum Function int maximum( int values[5] ) { int max_value, i; max_value = values[0]; for( i = 0; i < 5; ++i ) if( values[i] > max_value ) max_value = values[i]; return max_value; }

18 Sample Program Output Enter 5 numbers 7 23 45 9 121 Maximum value is 121 Note: The program defines an array of five elements (values) and initializes each element to the users inputted values. The array values is then passed to the function.

19 maximum function Declaration The declaration int maximum( int values[5] ) defines the function name as maximum, and declares that an integer is passed back as the result, and that it accepts a data type called values, which is declared as an array of five integers.

20 A local variable max_value is set to the first element of values, and a for loop is executed which cycles through each element in values and assigns the lowest item to max_value.local variablefor This number is then passed back by the return statement, and assigned to max in the main section.

21


Download ppt "Parameter Passing to Functions in C. C Parameter passing Review of by-value/by-reference."

Similar presentations


Ads by Google