Procedures and Functions: Another Exam question (Computer Science)

Watch
Aadilkharl123
Badges: 17
Rep:
?
#1
Report Thread starter 1 year ago
#1
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.
0
reply
TSR Jessica
Badges: 19
Rep:
?
#2
Report 1 year ago
#2
Sorry you've not had any responses about this. Are you sure you've posted in the right place? Here's a link to our subject forum which should help get you more responses if you post there.
0
reply
winterscoming
Badges: 19
Rep:
?
#3
Report 1 year ago
#3
This line is extremely important:
Code:
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:
Code:
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:
Code:
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:
Code:
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:
Code:
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.
Code:
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.
1
reply
Aadilkharl123
Badges: 17
Rep:
?
#4
Report Thread starter 1 year ago
#4
thanks <3
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

How are you feeling ahead of results day?

Very Confident (33)
8.07%
Confident (55)
13.45%
Indifferent (58)
14.18%
Unsure (104)
25.43%
Worried (159)
38.88%

Watched Threads

View All
Latest
My Feed