Page 72 - DCAP407_DATA_STRUCTURE
P. 72
Unit 4: Pointers
getch();
}
Output:
m = 10 and b = 20
In the above example, two functions - num() and main() are used. Execution
always starts from the main() function, where main() function is the calling
function and num() is the called function. The sequence of steps are as
follows:
1. Execution starts from the calling function main(). Since, the variables m
and n are defined within the function, memory is allocated for these
variables during run-time and initialized with junk values ‘?’. Let the
addresses of the variables be 1004 and 1008.
2. After executing the statements:
m = 10;
n = 20;
The values m and n are copied into the memory locations identified by
m and n in the addresses 1004 and 1008.
3. Function num() is invoked with two parameters m and n.
4. Control is then transferred to the called function num().
5. Memory is allocated for the formal parameters x and y. Assuming that
the address of these variables is 2000 and 2004, the values of the actual
parameters 10 and 20 are copied into these locations. Note that the
number and the type of the actual parameters match with the number
and type of formal parameters. So, the formal parameters obtain the
value of actual parameters which is 10 and 20.
6. When the statements
x=100
y = 200
are executed, the earlier values 10 and 20 are replaced by 100 and 200.
7. Then, control transfers from the called function to the calling function. It
points from where it was invoked earlier. Before the control is
transferred to the calling function, the memory allocated for the formal
parameters is de-allocated.
8. Execution stops.
LOVELY PROFESSIONAL UNIVERSITY 65