21
Parameter Passing to Functions in C

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

  • View
    256

  • Download
    5

Embed Size (px)

Citation preview

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

Parameter Passing to Functions in C

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

• C Parameter passing

• Review of by-value/by-reference 

Page 3: 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

 

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

 

  void f (int x)  // x is an integer

   {

  x=x+1;

  }

           

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

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.

Page 6: Parameter Passing to Functions in C. C Parameter passing Review of by-value/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

 

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

• 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;   } 

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

• 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

•  

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

• Array parameters are similar to Java in effect

 

int array[10];

void g(int arr[])

g (array);

 

 

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

• Trace the following:

 

•            

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

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;        }         

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

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

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

Exercise

• Write 2 functions to calculate the square of integer X

• One which passes parameters by value

• The other by Reference

• X^2

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

 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.

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

 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.

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

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 ); }

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

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; }

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

• 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.

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

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.

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

• 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.

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