Formal Parameters

A function parameters, formal parameters, are treated as local variables with-in that function and they will take preference over the global variables. Following is an example: 

#include <stdio.h>
 
/* global variable declaration */
int a = 20;
 
int main ()
{
  /* local variable declaration in main function */
  int a = 10;
  int b = 20;
  int c = 0;

  printf ("value of a in main() = %d\n",  a);
  c = sum( a, b);
  printf ("value of c in main() = %d\n",  c);

  return 0;
}

/* function to add two integers */
int sum(int a, int b)
{
    printf ("value of a in sum() = %d\n",  a);
    printf ("value of b in sum() = %d\n",  b);

    return a + b;
}

When the above code is compiled and executed, it produces following result:

value of a in main() = 10
value of a in sum() = 10
value of b in sum() = 20
value of c in main() = 30


Post a Comment