The Student Room Group

Procedures and Functions: Another Exam question (Computer Science)

MAIN
X = 2
Y = 4
ADD(X,Y)
PRINT X,Y
Y=Y*3
PRINT MAX(X,Y)
END MAIN

FUNCTION MAX (A,B)
IF A > B THEN
Return A
ELSE
Return B
ENDIF
END FUNC

PROCEDURE ADD (C by ref,D by val)
C = C + D + MAX(C,D)
D = D - 3
END PROC

The answer = Output : 10,4
12

However i dont understand how my teacher got their. Can someone go through this step by step, so i get a better explanation.
Sorry you've not had any responses about this. :frown: Are you sure you've posted in the right place? :smile: Here's a link to our subject forum which should help get you more responses if you post there. :redface:
This line is extremely important:





PROCEDURE ADD (C by ref,D by val)





Your teacher's code contains a procedure which has two parameters - 'C' is a reference parameter, and 'D' is a value

Your program starts out with two variables: X = 2 and Y = 4 respectively. These variables occupy their own areas of memory -- i.e. there's a series of bytes in memory which contain the numbers 2 and 4. These areas of memory are passed into the procedure here:





ADD(X, Y)





This means 'X' is passed into ADD by-reference. 'Y' is passed by-value.

When a program calls a procedure, that procedure has its own memory space which can have its own variables that do not affect the rest of the program.

Look again at the ADD procedure:





PROCEDURE ADD (C by ref,D by val)





When a variable is passed by-value, that procedure takes a copy of the variable into its own memory space. This means that any modifications to a value parameter will only affect the memory space belonging to that procedure. In other words, the ADD procedure has a memory space for D which the rest of the program cannot see.

On the other hand, 'C' is a reference variable - this means the memory for that variable does not belong to the ADD procedure's memory space, it's some memory from from outside of the procedure -- in other words C is nothing more than a signpost over towards another area of memory which exists outside of the ADD procedure. (hence the term "reference" -- in plain English terms a reference variable is a variable which "refers to" some other bit of memory rather than being its own area of memory)

So the implications are:





X = 2
Y = 4
ADD(X,Y)





the ADD procedure is able to see (and therefore modify) the physical memory for X.

On the other hand, Y is "by-value" - it takes a copy of the data stored in Y, therefore Y will not change, but X can.

You can see from the body of the procedure that X is indeed modified:





PROCEDURE ADD (C by ref,D by val)
C = C + D + MAX(C,D)
D = D - 3
END PROC





So, when the ADD function is finished, the area of memory being referenced by C (Which is an area of memory outside of the function) will have a value of C+D+MAX(C,D)

So to make it easier to see what's happening - try "unrolling" the ADD function to see its effect on the X and Y variables, again remembering that "Y" is not affected.





X = 2
Y = 4
X = X + Y + MAX(X, Y)
PRINT X, Y





So "unrolling" add causes X to change, but not Y. This is how X ends up being given a value of 10. Y does not change, so it stays at a value of 4.


The rest of it, including the MAX function should be self-explanatory.
(edited 5 years ago)
thanks <3

Quick Reply

Latest