parameter passing to functions in c. c parameter passing review of by-value/by-reference
Post on 21-Dec-2015
260 views
TRANSCRIPT
Parameter Passing to Functions in C
• C Parameter passing
• Review of by-value/by-reference
By-Value:
The value of a variable is sent to function.
The actual parameter cannot be changed by function.
"In" parameter
void f (int x) // x is an integer
{
x=x+1;
}
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.
By-Reference
An address gets sent to function.
The function can change the values at that address
"Out" or "In-Out“ parameter manipulation
• 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; }
• 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
•
• Array parameters are similar to Java in effect
int array[10];
void g(int arr[])
g (array);
• Trace the following:
•
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; }
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
arr 0 1 2 3 4f(x) x = 6g(arr) 0 1 8 3 4h(&X) x = 7 h(&(arr[4]));
0 1 8 3 5
Exercise
• Write 2 functions to calculate the square of integer X
• One which passes parameters by value
• The other by Reference
• X^2
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.
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.
Example program to demonstrate the passing of an array
#include <stdio.h> 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 ); }
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; }
• 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.
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.
• 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.
• This number is then passed back by the return statement, and assigned to max in the main section.