Functions are used to provide modularity
to the software. By using functions, you can divide complex tasks into small
manageable tasks. The use of functions can also help avoid duplication of work.
For example, if you have written the function for calculating the square root,
you can use that function in multiple programs.
Program
#include <stdio.h>
int add (int x, int y) //A
{
int
z; //B
z = x +
y;
return
(z); //C
}
main ()
{
int i, j,
k;
i = 10;
j = 20;
k =
add(i, j); //D
printf
("The value of k is%d\n", k);
//E
}
Explanation
- This function adds two
integers and returns their sum.
- When defining the name of
the function, its return data type and parameters must also be defined.
For example, when you write
- int add (int x, int y)
int is the
type of data to be returned, add is the
name of the function, and x and y are the parameters of the type int. These
are called formal parameters.
- The body of a function is
just like the body of main. That means you can have variable declarations
and executable statements.
- A function should contain
statements that return values compatible with the function's return type.
- The variables within the
function are called local variables.
- After executing the return
statement, no further statements in the function body are executed.
- The name of the function can
come from the arguments that are compatible with the formal parameters, as
indicated in statement D.
- The arguments that are used
in the call of the function are called actual parameters.
- During the call, the value
of the actual parameter is copied into the formal parameter and the
function body is executed.
- After the return statement,
control returns to the next statement which is after the call of the
function.
Points to Remember
- A function provides
modularity and readability to the software.
- To define the function, you
have to define the function name, the return data type and the formal
parameters.
- Functions do not require
formal parameters.
- If the function does not return
any value, then you have to set the return data type as void.
- A call to a function should
be compatible with the function definition.
A stack is memory in which values are stored
and retrieved in "last in first out" manner by using operations
called push and pop.
Program
Suppose
you want to insert values in a stack and retrieve values from the stack. The
operations would proceed in the following manner:

Explanation
- Initially, the stack is
empty. When you start push A, A is placed in the stack.
- Similarly, push B and push C put these elements in the
stack; the last element pushed is C.
- The pop operation takes the topmost
element from the stack. Thus the element C, which was put in last, is retrieved first.
This method is called last-in first-out (LIFO).
- The push D operation puts element D in the stack above B.
- Thus push puts the element on the top
of the stack and pop takes the element from the
top of the stack. The element A which is pushed is the last element taken
from the stack.
Point to Remember
The last-in first-out retrieval from the stack is
useful for controlling the flow of execution during the function call.
When the
function is called, the current execution is temporarily stopped and the
control goes to the called function. After the call, the execution resumes from
the point at which the execution is stopped.
To get
the exact point at which execution is resumed, the address of the next
instruction is stored in the stack. When the function call completes, the
address at the top of the stack is taken.
Program
main ( )
{
printf
("1 \n"); // 1
printf
("2 \n"); // 2
printf
("3 \n"); // 3
printf
("4 \n"); // 4
printf
("5 \n"); // 5
f1 ( );
printf
("6 \n"); // 6
printf
("7 \n"); // 7
printf
("8 \n"); // 8
}
void f1 (void)
{
printf
("f1-9 \n"); // 9
printf
("f1-10 \n"); // 10
f2 ( );
printf
("f1-11 \n"); // 11
printf
("f1-12 \n"); // 12
}
{
printf
("f2-13 \n"); // 13
printf
("f2-14 \n"); // 14
printf
("f3-15 \n"); // 15
}
Explanation
- Statements 1 to 5 are
executed and function f1( ) is
called.
- The address of the next
instruction is pushed into the stack.
- Control goes to function f1( ), which starts executing.
- After the 10th statement,
fuction f2 is called and address of
the next instruction, 11, is pushed into the stack.
- Execution begins for
function f2 and statements 13, 14, and
15 are executed.
- When f2 is finished, the address is
popped from the stack. So address 11 is popped.
- Control resumes from
statement 11.
- Statements 11 and 12 are
executed.
- After finishing the f1 address is popped from the
stack, i.e. 6.
- Statements 6, 7, and 8 are
executed.
- The execution sequence is 1 2 3 4 5 f1_9 f1_10 f2_13
f2_14 f2_15 f1_11 f1_12 6 7 8.
Points to Remember
- Functions or sub-programs
are implemented using a stack.
- When a function is called,
the address of the next instruction is pushed into the stack.
- When the function is
finished, the address for execution is taken by using the pop operation.