Pages

FUNCTION UNIT 10 PART 1


Introduction
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
  1. This function adds two integers and returns their sum.
  2. When defining the name of the function, its return data type and parameters must also be defined. For example, when you write
  3. 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.
  1. The body of a function is just like the body of main. That means you can have variable declarations and executable statements.
  2. A function should contain statements that return values compatible with the function's return type.
  3. The variables within the function are called local variables.
  4. After executing the return statement, no further statements in the function body are executed.
  5. The name of the function can come from the arguments that are compatible with the formal parameters, as indicated in statement D.
  6. The arguments that are used in the call of the function are called actual parameters.
  7. During the call, the value of the actual parameter is copied into the formal parameter and the function body is executed.
  8. After the return statement, control returns to the next statement which is after the call of the function.
Points to Remember
  1. A function provides modularity and readability to the software.
  2. To define the function, you have to define the function name, the return data type and the formal parameters.
  3. Functions do not require formal parameters.
  4. If the function does not return any value, then you have to set the return data type as void.
  5. A call to a function should be compatible with the function definition.
THE CONCEPT OF STACK
Introduction
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
  1. Initially, the stack is empty. When you start push A, A is placed in the stack.
  2. Similarly, push B and push C put these elements in the stack; the last element pushed is C.
  3. 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).
  4. The push D operation puts element D in the stack above B.
  5. 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.
THE SEQUENCE OF EXECUTION DURING A FUNCTION CALL
Introduction
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
}
void f2 (void)
{
    printf ("f2-13 \n");    // 13
    printf ("f2-14 \n");    // 14
    printf ("f3-15 \n");    // 15
}
Explanation
  1. Statements 1 to 5 are executed and function f1( ) is called.
  2. The address of the next instruction is pushed into the stack.
  3. Control goes to function f1( ), which starts executing.
  4. After the 10th statement, fuction f2 is called and address of the next instruction, 11, is pushed into the stack.
  5. Execution begins for function f2 and statements 13, 14, and 15 are executed.
  6. When f2 is finished, the address is popped from the stack. So address 11 is popped.
  7. Control resumes from statement 11.
  8. Statements 11 and 12 are executed.
  9. After finishing the f1 address is popped from the stack, i.e. 6.
  10. Statements 6, 7, and 8 are executed.
  11. 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
  1. Functions or sub-programs are implemented using a stack.
  2. When a function is called, the address of the next instruction is pushed into the stack.
  3. When the function is finished, the address for execution is taken by using the pop operation.