Jump to content
Urch Forums

Parameter passing


ozgulker

Recommended Posts

This is an easy question, I am just posting for the people who are tired of OS concepts.

 

int a, b;

 

void foo (int x, int y)

{

x = x + 2;

a = x * y;

x = x + 1;

}

 

void main ()

{

a = 1;

b = 2;

foo(a,b);

printf("%d", a);

}

 

What will the program above will print if the parameters are passed

a) Call by value

b) Call by value-result

c) Call by name

d) Call by reference

e) Call by result

f) Any other call methods you know

 

Link to comment
Share on other sites

 

 

a) 6

b) 4

c) 7

d) 7

e)

 

Call by result is used in Ada to implement out mode parameter passing. The formal parameter acts as an uninitialised local variable which is given a value during execution of the procedure. The value of the formal parameter is then assigned to the actual parameter on returning from the routine.

 

http://www.csc.liv.ac.uk/~frans/OldLectures/2CS45/paramPassing/paramPassing.html#callByResult

 

Does that mean the program above won't compile w/ pass by result?

 

Thanks, Ozgulker.

 

Link to comment
Share on other sites

This is what my book says (Principles of Programming Languages, Pratt, Zelkowits)

 

A parameter transmitted by result is used only to transmit a result back from a subprogram. The initial value of the actual parameter data object makes no difference and cannot be used by the program. The formal parameter is a local variable (data object) with no initial value (or with the usual initializaton provided for local variables). Then the subprogram terminates, the final value of the formal parameter is assigned as the new value of the actual parameter, just as in call by value-result.

 

Thus, I dont think there is any reason for the code not to compile. The values of the actual parameters are not taken into account, that is all.

Link to comment
Share on other sites

Be careful. a is a global variable.

 

 

God..... Sorry..... I thought they were defined again in main() scope........ (thought the ques woul be trickier with same namein multiple scopes.....

 

Ok this has spurred my thought.... If we had a local variable a too and we pass a by name, what would happen???.... I believe the local a would have precedence.... but again I have my doubts.....

 

Cheers'

Laks

Link to comment
Share on other sites

  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...