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
Variable a (value 5)
Variable b (value 10)
Variables sqr_a, sqr_b, sum
Variable x (value 5) Variable x (value 5)
Variable y (value 10) Variable y (value 10)
Variable z Variable z
Stack before call to func After func calls square first time
Variable a (value 5)
Variable b (value 10)
Variable sqr_a (value 25)
Variable sqr_b (value 100)
Variable sum (value 125)
Variable x (value 10)
Variable square_value (100) Variable x (value 5)
Variable y (value 10)
Variable a (value 5) Variable z
Variable b (value 10)
Just before func returns
Variable sqr_a (value 25)
Variables sqr_b, sum
Variable x (value 5) Variable x (value 5)
Variable y (value 10) Variable y (value 10)
Variable z Variable z (value 125)
After func calls square second time Stack after func returns
Draw the stack for various stages of execution of this recursive function
Scope = part of a program where a variable can be
referenced or used
Global variables declared outside of any function have file
scope, i.e., can be used anywhere in the file
Local variables declared inside a function (main or any other)
have function scope, i.e., can be used only within the function
Variables declared inside any block of code within { } have
block scope, i.e., can be used only within that block of code
If a variable with same name exists in outer and inner blocks,
the outer variable remains hidden until the inner block ends
Local variables in functions (or in any block of code) are
initialized every time the function is run
They are stored on stack, erased after function returns, do
not retain value when function called next time
Static variables are initialized only once, and retain value
even when out of scope
Note: static variable cannot be used when out of scope, but
value is retained for next use
How? Stored in different area, not on stack
Local variable initialized to 25 every time function is called
Static variable retains its value across function calls
Can use a global or static variable to count step number