Pages

FUNCTION UNIT 10 PART 2


PARAMETER PASSING
Introduction
Information can be passed from one function to another using parameters.
Program
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.
Points to Remember
  1. C uses the method of parameter passing by value.
  2. In parameter passing by value, the value before and after the call remains the same.
CALL BY REFERENCE
Introduction
Suppose you want to pass a parameter under the following conditions:
  1. You need to change the value of the parameter inside the function.
  2. You are interested in the changed value after the function completes.
In languages such as Pascal, you have the option of passing the parameter by reference. C, however, does not support this. As explained in the previous example, you cannot have a changed value after the function call because C uses the method of parameter passing by value. Instead, you'll have to implement the function indirectly. This is done by passing the address of the variable and changing the value of the variable through its address.
Program
main ( )
{
    int i;
    i = 0;
   printf (" The value of i before call %d \n", i);
    f1 (&i);      // A
   printf (" The value of i after call %d \n", i);
}
void (int *k)         // B
{
    *k = *k + 10;     // C
}
Explanation
  1. This example is similar to the previous example, except that the function is written using a pointer to an integer as a parameter.
  2. Statement C changes the value at the location specified by *k.
  3. The function is called by passing the address of i using notation &i.
  4. When the function is called, the address of i is copied to k, which holds the address of the integer.
  5. Statement C increments the value at the address specified by k.
  6. The value at the address of i is changed to 10. It means the value of i is changed.
  7. The printf statements after the function call prints the value 10, that is, the changed value of i.
Points to Remember
  1. Call by reference is implemented indirectly by passing the address of the variable.
  2. In this example, the address of i is passed during the function call. It does not change; only the value of the address is changed by the function.
THE CONCEPT OF GLOBAL VARIABLES
Introduction
The various modules can share information by using global variables.
Program
#include <stdio.h>
int i =0;     //Global variable
main()
{
    int j;          // local variable in main
    void f1(void) ;
    i =0;
    printf("value of i in main %d\n",i);
    f1();
    printf("value of i after call%d\n",i);
}
void f1(void)
{
    int k;            // local variable for f1.
    i = 50;
}
Explanation
  1. When you define a variable inside the function block it is called a local variable.
  2. The local variable can be accessed only in the block in which it is declared.
  3. j is the local variable for main and it can be accessed only in the block main. That means you cannot access it in function f1.
  4. k is the local variable for function f1 and it cannot be accessed in main.
  5. The variable i, which is outside main, is called a global variable. It can be accessed from function main as well as function f1.
  6. Any expression in this function is going to operate on the same i.
  7. When you call function f1, which sets the value of i to 50, it is also reflected in main because main and f1 are referring to the same variable, i.
Points to Remember
  1. Global variables can be accessed in all the functions in that file.
  2. Any update to the global variable also affects the other functions, because all functions refer to the same value of i.
  3. When you want to share information between multiple functions, you can use the concept of global variables.
RESOLVING VARIABLE REFERENCES
Introduction
When the same variable is resolved using both local definition and global definition, the local definition is given preference. This is called the rule of inheritance. It says that when you can resolve a reference to the variable by using multiple definitions, the nearest definition is given preference. Since local definition is the nearest, it gets preference.
Program
int i =0;            //Global variable  /A
main()
{
    int i ;                 // local variable for main  / B
    void f1(void) ;         //C
    i =0;                   // D
    printf("value of i in main %d\n",i);   // E
    f1();                                  // F
    printf("value of i after call%d\n",i); // G
}
void f1(void)                       // H
{
    int i=0;          //local variable for f1      // I
    i = 50;                                // J
}
Explanation
  1. Here i is declared globally and locally in function main and in function f1, respectively, as given in statements A, B and I.
  2. Statement D refers to i, which can be resolved by using both local definition and global definition. Local definition is given more preference. So statement D refers to the definition at statement B and all the statements in main refer to the definition at statement B, that is, the local definition.
  3. When a function is called, statement i = 50 refers to the local definition in that function (definition at statement I).
  4. Using statement G, the value of i is 0 because both main and function f1 refer to their local copies of i. So the changed value of f1 is not reflected in main.
  5. Even if you comment local definition of function f1 at statement I the value printed remains the same. This is because main refers to its local copy while f1 refers to the global variable i — the two are different.
Point to Remember
When a variable can be resolved by using multiple references, the local definition is given more preference.