# Address of the function

Discussion in 'C' started by back from retirement, Dec 3, 2008.

1. ### back from retirementNew Member

Joined:
Nov 9, 2008
Messages:
72
Likes Received:
1
Trophy Points:
0
Occupation:
Student, UG 1st Yr., Jadavpur University
Location:
Uttarpara, West Bengal, India
I was reading about recursive functions....I found something not well answered in my text books....
In the recursive function of fibonacci numbers....I used the following code....

Code:
```int fib(int n)
{
if(n==1||n==0)
return(1);
else
return(fib(n-1)+fib(n-2));
}
```
So when I was trying to find the value of fib(4), I noticed that fib(4) is calling fib(3) and fib(2). Again fib(3) is calling fib(2) and fib(1). My query is, in case of the two calls, are both fib(2)'s being called from the same function address???

Last edited: Dec 3, 2008
2. ### shabbirAdministratorStaff Member

Joined:
Jul 12, 2004
Messages:
15,293
Likes Received:
365
Trophy Points:
83
Whenever any function is called ( inline exceptional ) the {program execution} address and memory location changes with stack pointer change.

Try looking at the assembly code for this and would see what is happening.

3. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
Likes Received:
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
That's not quite true Shabbir. The function address, i.e. the location of the code, remains constant. Each call to the function gets its own stack frame, whether the function is called from itself or from somewhere else. The two fib(2) calls are made from the same function address, but from different stack frames.

e.g.
fib(4) -> frame 1, calls:
- fib(3) -> frame 2, calls:
- - fib(2) -> frame 3, calls:
- - - fib(1) -> frame 4, doesn't call anyone, just returns 1
- - - fib(0) -> frame 4, doesn't call anyone, just returns 1
- - fib(1) -> frame 3 (reuse), doesn't call anyone, just returns 1
- fib(2) -> frame 2, calls:
- - fib(1) -> frame 3 (reuse), doesn't call anyone, just returns 1
- - fib(0) -> frame 3 (reuse), doesn't call anyone, just returns 1

This can be demonstrated by printing the address of n; this won't give the exact stack frame pointer, but it will show what's going on:

Code:
```int fib(int n)
{
printf("entering fib(n=%d); &n=%p\n",n,&n);
if(n==1||n==0)
return(1);
else
return(fib(n-1)+fib(n-2));
}

void go4e_15378()
{
printf("fib4=%d\n",fib(4));
}
```
Output (added comments with --):

entering fib(n=4); &n=0012FDC4
entering fib(n=3); &n=0012FCEC
entering fib(n=2); &n=0012FC14 --*
entering fib(n=1); &n=0012FB3C -- 2 frame 4's
entering fib(n=0); &n=0012FB3C
entering fib(n=1); &n=0012FC14 --*
entering fib(n=2); &n=0012FCEC
entering fib(n=1); &n=0012FC14 --*
entering fib(n=0); &n=0012FC14 -- this and *** are frame 3
fib4=5

So as you can see from the following two lines extracted from the above output:
entering fib(n=2); &n=0012FC14
entering fib(n=2); &n=0012FCEC

the two fib(2) calls have n at a different place in memory.

4. ### shabbirAdministratorStaff Member

Joined:
Jul 12, 2004
Messages:
15,293
Likes Received:
365
Trophy Points:
83
By Function address I meant the execution point and not the function address in the memory where it is loaded for execution.

I have corrected the by adding function execution address as I thought that was misleading.

5. ### back from retirementNew Member

Joined:
Nov 9, 2008
Messages:
72
Likes Received:
1
Trophy Points:
0
Occupation:
Student, UG 1st Yr., Jadavpur University
Location:
Uttarpara, West Bengal, India
What I wanted was the way xpi0t0s answered it....maybe there is something wrong in the language of my questuion....sorry for that anyway.....
In my compiler I ran the program designed by xpi0t0s, and it gave me the following output....

Code:
```entering fib(n=4); &n=10EF:2200
entering fib(n=3); &n=10EF:21F6
entering fib(n=2); &n=10EF:21EC
entering fib(n=1); &n=10EF:21E2
entering fib(n=0); &n=10EF:21E0
[b]entering fib(n=1); &n=10EF:21EA[/b]
entering fib(n=2); &n=10EF:21F4
[b]entering fib(n=1); &n=10EF:21EA[/b]
entering fib(n=0); &n=10EF:21E8
fib4=5
```
So it definitely comes from the same function address....with different frames....

Thanks immensely for answering the question.....it was urgently needed.....

Last edited: Dec 3, 2008
6. ### shabbirAdministratorStaff Member

Joined:
Jul 12, 2004
Messages:
15,293
Likes Received:
365
Trophy Points:
83
yeah some confusion from you and added something from me as well

But all is well that ends well