Passing Parameters to a Functions in C Language with Free Notes

What is the Passing Parameters to a Functions

The two way communication between the various Functions can be achieved through parameters or arguments and the return statement.

The set of arguments defined in a Functions are called formal or dummy arguments whereas the set of corresponding arguments sent by the calling function are called actual arguments.

The actual arguments must correspond in number, type and order with formal arguments specified in the function definition. The actual arguments can be constants, variables, expressions or array names.

The Functions that parameters can be called in one of the following two ways:

  1. Call by Value
  2. Call by Reference
Play Quiz Gain Knowledge

1. Call by Value

in this type of parameters passing, only the values are passed from the calling function to the called Functions. Therefore this techniques is accordingly called as call by value.

For the better understanding of the concept of parameter passing let us consider the following function:

/*Bigger  of two numbers*/
int big (int x, int y)
    }
       if (x > y)
           return (x);
       else 
           return (y);
    }

The function big can be called from the Function main() by the following program segment:

:
:
a = 20;
b = 15
c = big (a, b);
printf("\n The Bigger Number  = %d",c);
:
:

Once the program segment is obeyed, the following output is produced:

The Bigger Number = 20  

Example : Call By Value Functions

Let’s take an Example of Call by Value : In this example we write a program to calculate factorial of a given number by call be value function,

/*Factorial of Given Number*/
#include <stdio.h>
#include <conio.h>
    void main()
            {
      //Function Prototype is must if we want to return value other than int
              long fact(int);   
              int num;
              long f;
              clrscr();
              printf("\nEnter a number");
              scanf("%d", &num);
              f = fact(num);
              printf("\n Factorial of %d = %1d", num, f);
                getch():
             }
              long fact (int n)
                 {
                   int i;
                    long f1 - 1;
                      for (i = 1; i<= n; i++)
                             f1 = f1*i;
                    return (f1);
                 }

Output

Enter a number 5
Factorial of 5 = 120

Important Notes:

  • Let us know write a program which reads the values of two variables a and b. The contents of a and b are sent as parameters to a function exchange().
  • The function exchange the contents of the parameters with the help of a variables temp.
  • The changed values of a and b are printed.

2. Call by Reference

If the Programmer desires that the changes made to the formal parameters be reflected to the corresponding actual parameters then he should use call by reference method of parameter passing.

This technique passes the addresses or references of the actual parameters to the called function. Thus the actual and formal parameters share the same memory locations.

This can be achieved by applying an address of operator (&) to the actual parameters in the function call and value at address operator (*) to the formal parameters in the function definiton.

Example: Call by Reference Functions

Let’s take an example of Call by Reference. In this example, we are going to write a program to exchange the contents of two variables by using call bye reference method.

/*Exchange two variables by call by refernce*/
#include <stdio.h>
#include <conio.h>
    void main()
            {
              void exchange (int*, int*);  
              int a, b;
              clrscr();
              printf("\nEnter two number");
              scanf("%d%d", &a, &b);
              Exchange (&a, &b); // Call by Reference
              printf("\n The exchanged contents are %d%d", a, b);
                getch():
             }
              void exchange (int*x, int*y)
                 {
                   int temp;
                    temp = *x;
                    *x = *y;
                    *y = temp;
                 }

Output

Enter two numbers 10 20
The exchanged contents are 20 10 
  • In the function call &a i.e. 2002 and &b i.e. 4004 are passed to a function and received in *x and *y. *x -> value at address x and *y -> value at address y.
  • When we exchange them through temp variables, in the memory location itself, they are exchanged. Therefore printed with exchanged results.

Important Note

So we conclude here if we want to return only one value, we must call a function by value. However if we want to return more than one values at a time then we must call a function by reference.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top