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
- The parameter used for
writing the function is called the formal parameter, k in this case.
- The argument used for
calling the function is called the actual parameter.
- The actual and formal
parameters may have the same name.
- 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.
- 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.
- Thus i will equal 0 after the
call; the value of i before and after the
function call remains the same.
Points to Remember
- C uses the method of parameter passing by
value.
- In parameter passing by
value, the value before and after the call remains the same.
Suppose
you want to pass a parameter under the following conditions:
- You need to change the value
of the parameter inside the function.
- 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
- This example is similar to the previous
example, except that the function is written using a pointer to an integer
as a parameter.
- Statement C changes the
value at the location specified by *k.
- The function is called by
passing the address of i
using notation &i.
- When the function is called,
the address of i is copied to k, which holds the address of
the integer.
- Statement C increments the
value at the address specified by k.
- The value at the address of i is changed to 10. It means
the value of i is changed.
- The printf statements after the
function call prints the value 10, that is, the changed value of i.
Points to Remember
- Call by reference is
implemented indirectly by passing the address of the variable.
- 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
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;
f1();
printf("value of i after call%d\n",i);
}
void f1(void)
{
int
k; // local variable for f1.
i = 50;
}
Explanation
- When you define a variable
inside the function block it is called a local variable.
- The local variable can be
accessed only in the block in which it is declared.
- 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.
- k is the local variable for
function f1 and it cannot be accessed
in main.
- The variable i, which is outside main, is
called a global variable. It can be accessed from function main as well as
function f1.
- Any expression in this
function is going to operate on the same i.
- 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
- Global variables can be
accessed in all the functions in that file.
- Any update to the global
variable also affects the other functions, because all functions refer to
the same value of i.
- When you want to share
information between multiple functions, you can use the concept of global
variables.
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
- Here i is declared globally and
locally in function main and in function f1, respectively, as given in statements A, B
and I.
- 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.
- When a function is called,
statement i = 50 refers to the local
definition in that function (definition at statement I).
- 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.
- 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.