- #1
doktorwho
- 181
- 6
Homework Statement
This is a problem of the 10-minute test we had today in school.
Its written in Pascal and it is asked of us to analyze it and figure out what is the output when it runs without writing it down.
Homework Equations
3. The Attempt at a Solution [/B]
I wrote it in the editor and it looks like this:
Code:
program jan2015(output);
var
a,b,c,d:integer;
function f1(var a:integer; b:integer; var c:integer):integer;
function f2(var a,b:integer):integer;
begin
c:=a+d;
b:=a+1;
d:=d+1;
f2:=b
end;
begin
c:=a+b;
d:=c+a;
f1:=f2(d, b)
end;
begin
a:=1; b:=1; c:=1; d:=1;
writeln(output, f1(c, a, b), a, b, c, d);
readln();
end.
The purpose of readln() is to maintain the window open rather that it closing. The result is given as the answer (C) but i do not understand how. I tried putting a writeln function it between to see how it evolves to that answer but am still confused.
Code:
program jan2015(output);
var
a,b,c,d:integer;
function f1(var a:integer; b:integer; var c:integer):integer;
function f2(var a,b:integer):integer;
begin
c:=a+d;
writeln('This is C ',c); {it is 6! when i run it, how is it 6? when the starting values are 1-s?}
readln();
b:=a+1;
d:=d+1;
f2:=b
end;
begin
c:=a+b;
d:=c+a;
f1:=f2(d, b)
end;
begin
a:=1; b:=1; c:=1; d:=1;
writeln(output, f1(c, a, b), a, b, c, d);
readln();
end.
Since it is on purpose written to confuse us i rewrote it so that the functions contain real given values:
Code:
program jan2015(output);
var
a,b,c,d:integer;
function f1(var c:integer; a:integer; var b:integer):integer;
function f2(var c,a:integer):integer;
begin
b:=c+d;
writeln('This is B ',b); {still 6}
readln();
a:=c+1;
d:=d+1;
f2:=a
end;
begin
b:=c+a;
d:=b+c;
f1:=f2(d, a)
end;
begin
a:=1; b:=1; c:=1; d:=1;
writeln(output, f1(c, a, b), a, b, c, d);
readln();
end.
[/code
How does one analyze these codes given like this and more important, how is it 6?