Stack Concept :

  1. STACK is a part in RAM reserved by OS to keep account of program.

  2. OS manages program execution with the help of STACK.

  3. OS stores program execution in STACK.

  4. Top most stack section is always executed first.

  5. Once a function execution finishes STACK memory is cleaned for that specific function (removal is called as pop)

Concept of STACK with function calls :

Best way to understand the concept is to see it in action.

Example :

    int max(int a, int b){
        return (a >b) ? a : b;
    }

    int main(){
        int firstNumber  = 0;
        int SecondNumber = 0;

        printf("Enter First Number: ");
        scanf("%d",&firstNumber);

        printf("Enter Second Number: ");
        scanf("%d",&secondNumber);

        int result = max(firstNumber, secondNumber);

        printf("Maximum of %d and %d is : %d\n",firstNumber,secondNumber,result);

        return 0;
    }

Steps of Execution are :

  1. Main function is loaded in STACK.

  2. Local variables are created in STACK.

  3. Value is assigned to local variables.

  4. Value of address from where program left execution is stored in register.

  5. For printf seperate STACK section is created, because it is a function.

  6. Now printf execution completed, so STACK memory for printf is popped.

  7. Again program continues execution from main function.

  8. scanf is a function so, seperate STACK section is created for it.

  9. scanf execution completes with some value assigned to variable, so STACK memory is popped again.

  10. Steps 4 to 9 repeats again for second number input.

  11. Program Execution leaving address is again stored in STACK or register.(but for now say stack)

  12. Now max function is created in STACK, with two variables a and b.

  13. Values from main function are copied in max function.

  14. Program calculates result based on condition.

  15. Value is returned to main function and STACK is popped.

  16. and so on...... the process continues till end of program.

Note : Only USER-DEFINED functions are shown in images.

//Add image here.

Why is main function declared with int return type?

  • Every program has to return some value back to Operating System

  • This information is stored in a variable, named errorlevel (in case of windows only)

  • View info of this variable using : echo %errorlevel% (on windows)

The above case can also be detected by using debugger because improper termination of program creates a dump file.

Call by Reference functions :

The functions that takes pointer as formal argument and address as actual argument are called as call by reference functions.

    int sum(const int * first, const int * second){
        return first + second;
    }

    int main(int argc, char const *argv[])
    {
        int num1 = 10 , num2 = 20;

        int result = sum(&num1, &num2);
        printf("%d + %d = %d\n", num1, num2, result);

        return 0;
    }

Notable points about call by reference :

  1. There is formation of one variable as pointer in stack.

  2. Size of pointer depends on bits of compiler .

  3. (16bit - 1 byte), (32bit - 4 bytes), (64bit - 8 bytes).

  4. In case of passing arrays to functions this method saves lot of memory space and processor computing power.

Sample 1 :

    void print(char name[], int salary){
        printf("Your name is %s and monthly pay is %d", name, salary);
    }

    int main(int argc, char const *argv[])
    {
        char name[20] = "Jayant Malik";
        print(name, 20000);

        return 0;
    }

    // Memory Usage by print : 20 bytes for name + 4 bytes for int(64 bit compiler).

Sample 2 :

    void print(const char *name, int salary){
        printf("Your name is %s and monthly pay is %d", name, salary);
    }

    int main(int argc, char const *argv[])
    {
        print("Jayant Malik", 20000);
        return 0;
    }
    //memory usage by print : 8 bytes for name + 4 bytes for int.

Call by Value Functions :

Functions that takes original datatype as actual argument and direct values as actual argument.

    int sum(int first, int second){
        return first + second;
    }

    int main(int argc, char const *argv[])
    {
        int result = sum(10, 20);
        printf("10 + 20 = %d\n", result);

        return 0;
    }

Notable points about Call by value functions :

  1. Actual value of variable is copied in stack.

  2. Not access to change value of variable in local scope of parent calling function.

  3. In case of array extra memory usage takes place.

Nested function declaration :

when a function is declared inside function then it is called as nested function declaration.

    int main(int argc, char const *argv[])
    {
        int sum(int a, int b){
            return a + b;
        }

        int result = sum(10, 20);
        printf("10 + 20 = %d\n",result);

        return 0;
    }


Recursive function :

when same function again from same calling function then it is called recursion.

Output of recursion is determined using stack.

    void temp(int number){
        number--;

        if(number > 0){
            temp(num);
        }
    }
    int main(int argc, char const *argv[])
    {
        temp(3);
        return 0;
    }