0% found this document useful (0 votes)
7 views3 pages

Function Calling Seminar

Function calling is a key concept in programming and compiler design, utilizing a stack to manage execution flow, parameters, and return values. The document explains the process of function calls, including the roles of caller and callee functions, and provides an example of calculating the factorial of a number using recursion. Key takeaways highlight the importance of function calls for modular code and efficient execution management.

Uploaded by

acervseriestv33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Function Calling Seminar

Function calling is a key concept in programming and compiler design, utilizing a stack to manage execution flow, parameters, and return values. The document explains the process of function calls, including the roles of caller and callee functions, and provides an example of calculating the factorial of a number using recursion. Key takeaways highlight the importance of function calls for modular code and efficient execution management.

Uploaded by

acervseriestv33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Function Calling in Compiler Design

Introduction
Function calling is a fundamental concept in programming where one function invokes
another to perform a specific task. In compiler design, function calls are managed using a
stack to track execution flow, parameters, and return values.

How Function Calling Works?


When a function is called, the following steps occur:

1. Caller Function:

- Saves the return address (location to resume execution after the call).

- Passes arguments to the called function.

- Transfers control to the called function.

2. Callee Function:

- Receives parameters and executes its logic.

- Stores the return value (if any).

- Returns control to the caller.

3. Returning from the Function:

- The return value is stored in memory/registers.

- Control goes back to the caller function.

- The function's stack frame is removed.

Example: Factorial of 5 Using Function Calling


Below is a C program that calculates the factorial of a number using function calling
(recursion).

#include <stdio.h>

// Function to calculate factorial using recursion


int factorial(int n) {
if (n == 0)
return 1; // Base case: 0! = 1
else
return n * factorial(n - 1); // Recursive call
}
int main() {
int num = 5;

// Function call
int result = factorial(num);

// Printing the result


printf("Factorial of %d is %d\n", num, result);

return 0;
}

Execution Flow of the Program


The function `factorial(n)` calls itself recursively until `n == 0` (base case). The execution
follows this pattern:

- `factorial(5) = 5 * factorial(4)`

- `factorial(4) = 4 * factorial(3)`

- `factorial(3) = 3 * factorial(2)`

- `factorial(2) = 2 * factorial(1)`

- `factorial(1) = 1 * factorial(0)`

- `factorial(0) = 1` (Base case reached)

Stack Representation During Execution


Each function call creates a new stack frame until the base case is reached. The stack
representation is:

| Stack (Top to Bottom) |

|-----------------------|

| factorial(5) |

| factorial(4) |

| factorial(3) |

| factorial(2) |

| factorial(1) |

| factorial(0) (Base) |
Program Output
When executed, the program produces the following output:

Factorial of 5 is 120

Key Takeaways
- Function calls help in modular and reusable code.

- The stack manages function execution efficiently.

- Recursion breaks a complex problem into smaller subproblems.

- Each recursive call creates a new stack frame until the base case is reached.

- Function calling plays a crucial role in compiler design for managing execution flow.

You might also like