Communication between functions C

  • Parameter passing
  • Returning values

Functions are so useful mainly because they can communicate with each other. They accept values from functions calling them and return values to the calling functions

Parameter passing

  • Passing parameter by value
  • Passing address by value

Communication between calling and called function can be made possible by using the argument mechanism. There are two terms related in this context. They are ‘formal arguments’ and ‘actual arguments’.
In Example (1) main () calls sum() by passing two integer values to it. These values are called arguments. There names are ‘x’ and ‘y’. Variable used in the statement of a function call are called actual arguments, x and y are actual arguments.

In the definition of sum we have used the names of arguments to be ‘a’ and ‘b’. ‘a’ and ’b’ are called formal arguments. Instead if using different variable names a and b if we used same names x and y compiler would still treat them as different variable because they are in different functions.
In ‘C’ parameter are passed by values. But you can either pass

  1. Contents of variables by value.
  2. OR

  3. Address of variable by value.
  1. Passing parameter by value
    What happens when function is called by passing arguments? A temporary copy of the actual arguments is made to the formal arguments. The called function works on this copy of variables. The changes are made to this copy of variables. Hence original values of these variables remain intact with the calling function.

    The change made by the called function is not seen by the calling function. This is called ‘Call by value’. The following example clears the concept.

    /* Program to demonstrate call by value
    /* Value of arguments passed to function
    /* Function exchgv, is called from main .
    It exchange the values of a and b.*/

    The output of the above program would be
    In main before calling exchgv
    a = 10 b = 20
    In exchgv
    x = 20 y = 10
    In main after returning from exchgv
    a = 10 b = 20

    The values of ‘a’ and ‘b’ remain unchanged even after values of ‘x’ and ‘y’ are exchanged. When a and b are passed to exchgv, value of a i.e. 10 is copied to x and value of b i.e. 20 is copied to y. Now the action of exchanging the values is done on x and y and not on a and b. When function returns value, a and b in main() are not changed.

  2. Passing address by value
    Before going further, let us make the concept of address and pointer clear. When a variable is defined, a memory location is set aside for the variable. This location has a location number which is called the ‘address’. The symbol ‘&’ (ampersand) is used to represent address. E.g. If we have a variable i, defined and initialized as follows
    int i = 3;Passing address by value

    A pointer is a variable which stores address of any data type. Symbol ‘*’ is used to represent pointers.

    int * j;
    j = &i;

    j is a pointer variable.
    j stores the address of an integer.
    Value of that integer is 3.
    i.e. j = 2003. (i and *j are both equal to 3)
    We will study pointers in detail in later chapter.
    Let us rewrite by passing address instead of values


/* program to demonstrate passing of address by value
Address of arguments passed to function
/ * Function exchgr, is called from main
It exchange the address of a and b.
The change is seen in main */

The output of the above program would be
In main before calling exchgr
a = 10 b = 20
In exchgr – pass address of variable by value
a = 20 b = 10
In main after returning from exchgr
a = 20 b = 10

As can be seen from the output of the program when we pass address of variable the value of actual arguments can be altered and this alteration can be seen by the calling function. One more point to be noticed is that by passing address of variable we can make a function return more than one value at a time, indirectly. Exchgr has changed value of a and b both. These changes are available to main() hence main() has got two values from exchgr.

Returning values

  • Calling function passes values to called function whereas called function returns value to a calling function.
  • It is not necessary that a function should return a value.
  • A function with no return statement causes control to the calling function when the ending right brace is encountered, but no value is returned as in function display() of Example(1)
  • If the called function does not return a value, it should be specified with the keyword ‘void’ in the declaration and definition of function.
  • A calling function can ignore the value returned by a function.
  • There is no restriction on the number of return statement that may be present in a function.
  • A function can return only one value at a time. Hence return(2,3) is an invalid return statement.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

C Topics