Lecture5 Functions
Lecture5 Functions
IIT Bombay
Function call
Main also has int return value but return 0 done here automatically
Function definition / implementation
Function call to
recursive fibonacci
At each level, the number of function calls doubles, rises exponentially (2^n)
During recursion, multiple copies of variables will be present on the stack,
consuming memory
Recursion and iteration are both valid ways to solve problems
Any problem that can be solved recursively can also be solved iteratively
But, recursion may be more intuitive to solve some problems than iteration
Note: main is also like any other function with integer return value
Why do we not have a return statement? Compiler automatically
inserts return 0 at end of main
Variables related to a function are stored in a region of
program memory called a stack
What are the other regions in program memory? More later
When function is called, function data is pushed on to stack
Local variables and arguments
Return address, that is, where a function should return to