How communication among functions is established in C language?



Functions communicate among themselves with the help of arguments and return value.

Farm of ‘C’ function is as follows −

return-datatype function name (argument list){
   local variable declarations;
   executable statements(s);
   return (expression);
}

For example, void mul (int x, int y)

{
   int p;
   p=x*y;
   printf("product = %d”,p);
}

Return values and their types

  • A function may or may not send back a value to the calling function.
  • It will be done by using the return statement
  • The return types are void, int, float, char and double.
  • If a function is not returning any value, then its return type is ‘void’.

Function name

  • A function must follow the rules just like the variables name in ‘C’.

  • A function name must not a predefined function names.

Argument list

  • In this list the variable names separated by commas.

  • The argument variables receive values from the calling function, which provides a means for data communication from the calling function to the called function.

Calling a function

  • A function can be called by using the function name in a statement.

Function definition

  • Whenever, a function call, the control is transferred to the function definition.

  • All the statements, in called function are called as function definition.

Function header

  • The first line in the function definition.

Actual parameter

  • All the variables inside the function call.

Formal parameters

  • All the variables inside the function header are called formal parameters.

Example

Following is the C program for communication among the functions −

#include<stdio.h>
#include<conio.h>
main ( ){
   int mul (int, int); // function prototype
   int a,b,c;
   clrscr( );
   printf ("enter 2 numbers”);
   scanf("%d %d”, &a, &b);
   c = mul (a,b); // function call
   printf("product =%d”,c); Actual parameters
   getch ( );
}
int mul (int a, int b){ // Formal parameters
    //function header
   int c;
   c = a *b; //Function definition
   return c;
}

Output

When the above program is executed, it produces the following result −

Enter 2 numbers: 10 20
Product = 200
raja
Published on 09-Mar-2021 12:02:12
Advertisements