blogger Indonesia

Follow judhyns blog

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.

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.

Folow me on

Popular Posts

feed

PR Check

activesearchresults

judhyn's blog

 

http://www.judhyn.blogspot.com | |