Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Address of the function (http://www.go4expert.com/forums/address-function-t15378/)

back from retirement 3Dec2008 14:12

Address of the function
 
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: CPP

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???

shabbir 3Dec2008 14:27

Re: Address of the function
 
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.

xpi0t0s 3Dec2008 17:22

Re: Address of the function
 
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.

shabbir 3Dec2008 18:10

Re: Address of the function
 
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.

back from retirement 3Dec2008 20:45

Re: Address of the function
 
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
entering fib(n=1); &n=10EF:21EA
entering fib(n=2); &n=10EF:21F4
entering fib(n=1); &n=10EF:21EA
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..... :D

shabbir 3Dec2008 21:01

Re: Address of the function
 
Quote:

Originally Posted by back from retirement (Post 39779)
My query is, in case of the two calls, are both fib(2)'s being called from the same function address???

yeah some confusion from you and added something from me as well

Quote:

Originally Posted by shabbir (Post 39781)
Whenever any function is called ( inline exceptional ) the {program execution} address and memory location changes with stack pointer change.

But all is well that ends well


All times are GMT +5.5. The time now is 19:56.