ozgulker Posted October 31, 2003 Share Posted October 31, 2003 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 Quote Link to comment Share on other sites More sharing options...
nonevent99 Posted October 31, 2003 Share Posted October 31, 2003 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. Quote Link to comment Share on other sites More sharing options...
ozgulker Posted October 31, 2003 Author Share Posted October 31, 2003 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. Quote Link to comment Share on other sites More sharing options...
nonevent99 Posted October 31, 2003 Share Posted October 31, 2003 No, I mean x isn't initialized prior to the first time it is read (in the x = x+2 line). I guess in some languages this simply means its initial value is zero. In others it means it won't compile. In any case, if x and y are auto-initialized to 0, then (e) will print 3. Quote Link to comment Share on other sites More sharing options...
Laks Posted October 31, 2003 Share Posted October 31, 2003 PPl, Will not call by value result in 1 being printed ??? Quote Link to comment Share on other sites More sharing options...
ozgulker Posted October 31, 2003 Author Share Posted October 31, 2003 Will not call by value result in 1 being printed ??? Be careful. a is a global variable. Quote Link to comment Share on other sites More sharing options...
wood Posted October 31, 2003 Share Posted October 31, 2003 Since this is a C fragment, unless you specifically set your warning level high and treated them as errors, the code would compile. Wood Quote Link to comment Share on other sites More sharing options...
Laks Posted October 31, 2003 Share Posted October 31, 2003 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 Quote Link to comment Share on other sites More sharing options...
nonevent99 Posted October 31, 2003 Share Posted October 31, 2003 Somewhere I read that when you pass by name, if there's a local variable with the same name, then the system makes sure that they're kept distinct. Quote Link to comment Share on other sites More sharing options...
napoleon Posted December 8, 2003 Share Posted December 8, 2003 Hi, Could you explain to me how does call by name works. I know the logic behind it, but in a non recursive or non Scheme function this call will (by my opinion) issue the same result as call by reference. Thank you very much. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.