7 Replies - 1061 Views - Last Post: 17 March 2011 - 03:37 PM Rate Topic: -----

#1 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

questions on function prototypes; scope; return values

Posted 17 March 2011 - 12:40 PM

hi everyone,
i have some really specific/deatiled questions of which I am really confused by.

here is some sample code:
#include <stdio.h>
int mystery(int a, int B)/>;
main ()
{
	int x = 2, y = 5;
	y = mystery(x*3, y-2) + 4; //function caller
	printf("(%d, %d)\n", x+3, y+2);
}

//function definition
int mystery(int a, int B)/>
{
	int c;
	c = a + b;
	return c + 2; //takes value of c adds 2 and returns it to mystery
}



I totally understand this code, but the questions are a little confusing (my responses are in italics and my doubts/questions are in courier new font.

please correct me if i'm wrong or add more detail, i'd really like to have a solid foundation in C before I proceed onto C++ and other languages.

1. During the execution of the program above, what variables exist in memory and under which scope just before the statement
" return c + 2 " executes, and what are their values?


the variables that exist in memory during execution are "x", "y", "a", "b", "c" (all of type integer). "x" and "y" exist under the main function's scope while a, b, and c exist under the scope of the function definition "mystery." The value of x is 2, y is 5. The value of a is 6, b is 3 and c is 9.

im not sure if this is correct. do variables a, b, c really exist or are they there just to generate a value to return to the function caller located within function main? is the value of y really 5? or is it 15 because y = mystery(x*3, y-2) + 4 ? when i comment out the " return c + 2 " the output is (5, 15)?

2. During the execution of the program above, what variables exist in memory and under which scope just before the statement
" printf(" (%d, %d)\n", x+3, y+2); " executes, and what are their values?


the variables that exist just before the printf statement are x and y and c, and they exist under the scope of the main function. their values are x = 2, y = 15


as I was saying above, do variables a, b, c really exist or are they there just to generate a value to return to the main function? and am I using the term "scope" correctly? I know that the identifier's scope is where the identifier can be reference in a program. like i can't reference variable x within the funtion definition mystery because its not within it's scope?

3. Exactly what will be displayed when this program executes?

(5, 17)



Any help you guys can give me will be much appreciated, thanks in advance everyone :)

This post has been edited by iqbalmmz: 17 March 2011 - 12:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: questions on function prototypes; scope; return values

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: questions on function prototypes; scope; return values

Posted 17 March 2011 - 01:04 PM

1) your correct, all of those variables exist on the stack, arguments and locals included
2) i would say 'c' is wrong, it's value should be stored EAX not really on the stack. 'a' and 'b' do not exist, they where deallocated just before the function returns. still 'c' is kinda iffy, my reason for saying 'no' is because it doesn't exist on the stack, you can reference it any more.
3) i bet your compiler cant do this one for you ;)

This post has been edited by ishkabible: 17 March 2011 - 02:02 PM

Was This Post Helpful? 1
  • +
  • -

#3 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

Re: questions on function prototypes; scope; return values

Posted 17 March 2011 - 01:22 PM

View Postishkabible, on 17 March 2011 - 12:04 PM, said:

1) your correct, all of those variables exist on the stack, arguments and locals included
2) i would say 'c' is wrong, it's value should be stored EAX not really on the stack. 'a' and 'b' do not exist, they where deallocated just before the function returns. still 'c' is kinda iffy, my reason for saying 'no' is because it doesn't exist on the stack, you can reference it any more.
3) i bet your compiler can do this one for you ;)


i see, i had my doubts about the existence of variable c within function main.
for #2, what does EAX mean? so you'd say the only values that exist are x = 2 (why not 6 because its being multiplied by 3 inside function mystery?) and y = 15?
Was This Post Helpful? 0
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: questions on function prototypes; scope; return values

Posted 17 March 2011 - 01:31 PM

x is not 6 ever, x is always 2. EAX is a register, you don't need to worry about it though. values like x*3 are stored somewhere weather that be in a register or on the stack. if you say that the value of 'c' still exists in a register so therefore 'c' still exists then it complicates the definition of a variables existence. so a better definition is if that variable can be referenced then it exists, because 'c' cannot be referenced without undefined behavior it doesn't exist. the reason i say it's iffy is because there is not much of a way to know that only the value remains unless you know the details of the calling convention.

This post has been edited by ishkabible: 17 March 2011 - 01:33 PM

Was This Post Helpful? 2
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: questions on function prototypes; scope; return values

Posted 17 March 2011 - 01:44 PM

EAX is a CPU register on Intel based CPU's. Generally the in calling convention used by C the return value of a function will be placed in EAX. since variable 'c' is not really used for anything that would require it to be stored in memory, and it just need to be put in EAX anyway the compiler would probably just put it there.

HOWEVER -- from a "analytical" point of view (i.e. "chalkboard execution") we have to consider c as a variable and assume that it exits "in memory". The technical reason for this is because c is an lvalue and therefore technically the compiler has to be able to give us an address if we ask for it.

The compiler can however make any variable a "register" if it wants to. For example when I compile the above code x and y are kept as registers ESI and EBX respectively.

But we know that the variable x, y, a, b, c are all "addressable and assignable" meaning that if we choose you use the "address of" operator & or if we try to assign them a value we can -- so they are lvalues and we can think of them as having some address (even if the compiler optimizes it away because it is never used).

now there are a lot of "temporary values" (rvalues) that exist such as x*3 -- these are not addressable or assignable and they only exist until the end of the current line (i.e. they live until the semi-colon -- after that point they don't exist.)

don't get bogged down one what lvalues and rvalues are -- from what I can see you thinking seem correct (what was in italics above).
Was This Post Helpful? 2
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: questions on function prototypes; scope; return values

Posted 17 March 2011 - 01:59 PM

Quote

2. During the execution of the program above, what variables exist in memory and under which scope just before the statement
" printf(" (%d, %d)\n", x+3, y+2); " executes, and what are their values?

the variables that exist just before the printf statement are x and y and c, and they exist under the scope of the main function. their values are x = 2, y = 15


OH!!! yea -- c does not belong on this list and you should realize that since you correctly assessed what y was. The value of (c + 2) was returned and the variable 'c' is no longer in scope (it was part of the function).

Note that return values of functions are "temporary values" until after they are assigned to something more permenant. So the value of returned from 'mystery' was assigned to y but that value never existed as a variable. It was never in c.
Was This Post Helpful? 1
  • +
  • -

#7 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: questions on function prototypes; scope; return values

Posted 17 March 2011 - 02:05 PM

i don't really think that was a fair question unless your teacher or book explicitly stated that values that are returned cease exist upon return. you would have to know about the calling convention used otherwise.
Was This Post Helpful? 1
  • +
  • -

#8 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: questions on function prototypes; scope; return values

Posted 17 March 2011 - 03:37 PM

View Postishkabible, on 17 March 2011 - 09:05 PM, said:

i don't really think that was a fair question unless your teacher or book explicitly stated that values that are returned cease exist upon return. you would have to know about the calling convention used otherwise.


The calling convention has nothing to do with the scope of variables! The calling convention determines the order in which parameters are pushed onto the stack, and whether it is the caller or the callee that cleans up the stack on exit.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1