### Thread: Need help understanding procedure in pascal.

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2014
Posts
2
Rep Power
0

#### Need help understanding procedure in pascal.

Hello I need help understanding this simple code :

Code:
```program ggg;
const num=10;
var i, j, k: integer;
procedure TestProc( var a:integer; b: integer) ;
var c: integer;
begin
i:=num div 2;
a:=b;
b:=a+1;
c:=a;
writeln('In proc', a, b,c);

end;
begin
i:=1; j:=2; k:=5;
TestProc(j,k);
writeln('After proc', i, j, k);
end.```
If I try to get results (by hand) I get
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 .
2. 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.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2014
Posts
2
Rep Power
0
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