Tuesday, March 22, 2011

Program for Parameter Passing with Explanation


Program

void main ( )
{
    int i;
    i = 0;
    printf (" The value of i before call %d \n", i);
    f1 (i);
    printf (" The value of i after call %d \n", i);
}
void f1 (int k)
{
    k = k + 10;
}

Explanation

  1. The parameter used for writing the function is called the formal parameter, k in this case.
  2. The argument used for calling the function is called the actual parameter.
  3. The actual and formal parameters may have the same name.
  4. When the function is called, the value of the actual parameter is copied into the formal parameter. Thus k gets the value 0. This method is called parameter passing by value.
  5. Since only the value of i is passed to the formal parameter k, and k is changed within the function, the changes are done in k and the value of i remains unaffected.
  6. Thus i will equal 0 after the call; the value of i before and after the function call remains the same

No comments:

Post a Comment