January 21st, 2014, 01:42 PM
Need help understanding procedure in pascal.
Hello I need help understanding this simple code :
If I try to get results (by hand) I get
var i, j, k: integer;
procedure TestProc( var a:integer; b: integer) ;
var c: integer;
i:=num div 2;
writeln('In proc', a, b,c);
i:=1; j:=2; k:=5;
writeln('After proc', i, j, k);
In proc 343
After proc 534
The the correct must be :
In proc 343
After proc 533 !!!
Why there is 533 after proc instead of 534 ?
It seems that both j, k values get value of a somehow ? Why does k don't have value of b, what is 4 ?
Can someone please explain this to me ? I really don't get it.
I am quite new to programming and this is one example task I have, I seem to understand first part but dont get what happens in result of procedure .
January 21st, 2014, 02:32 PM
How did you get to those number.
How do you know what the correct numbers must be, have you executed the code?
When I manually calculated the "in proc" numbers I got: 565.
Using this pascal compiler for verification, it turned out my calculation was correct.
About that the numbers are different between "in proc" and "after proc", I suggest you take a look at how the function arguments are defined and read up on Pass by Value vs. Pass by Reference
Last edited by MrFujin; January 21st, 2014 at 02:36 PM.
January 21st, 2014, 02:42 PM
K must have been 3 that is why numbers do not match.
I found solution, it seems the only variable what changes after process is a, what after process is j, so it mean that after process is done the third value do not change at all it just remains the same. So only variables what have var in front of them change and it is var a only, so as c is local variable.
That seems to do the trick.
And yea, the link below proves the same