Menu

Parameter passing techniques

Types of parameters

  1. Actual parameters
  2. Formal parameters

Actual Parameters

        The parameters appeared in the function call statements are called Actual parameters.

Formal parameters

        The parameters appeared in the function definition statements are called formal parameters.

The techniques used for parameter passing are

  1. call by value(pass by value)
  2. call by reference(pass by reference)
  3. call by result(pass by result)
  4. call by value-result(pass by value -result)
  5. call by name(pass by name)

call by value

  • In call by value method,the copy is passed to the procedure.
  • In call by value method, all the values from actual parameters are passed to formal parameters.
  • if there is any change in the formal parameters it will not reflect back to the actual parameters.

Example

    #include<stdio.h>
    int callbyvalue(int);
    main(){
        int var=100;
        callbyvalue(var);
        printf("%d",var);
    }
    callbyvalue(int b){
        b=200;
        return (b);
    }

Output: 100

Call by reference

  • In call by reference, pass a pointer to the actual parameter, and indirect through the pointer .
  • In this,all the values in the actual parameters are passed to formal parametes.
  • if there any change in the formal parameter, it will reflect back to the actual parameter.

Example

     #include<stdio.h>
     void change(int &);
     main(){
         int num;
         num1=30;
         change(num);
         printf("%d",num);
     }
     void change(int &b){
        num2=50;
        return;
     }

Output: 50