Procedures and Functions: Another Exam question (Computer Science) Watch

Aadilkharl123
Badges: 15
Rep:
?
#1
Report Thread starter 5 months 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
  •  Official Rep
Badges: 16
Rep:
?
#2
Report 5 months 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: 18
Rep:
?
#3
Report 5 months 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.
Last edited by winterscoming; 5 months ago
1
reply
Aadilkharl123
Badges: 15
Rep:
?
#4
Report Thread starter 5 months ago
#4
thanks <3
Posted on the TSR App. Download from Apple or Google Play
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
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

University open days

  • University of Birmingham
    Postgraduate Open Day Postgraduate
    Wed, 20 Mar '19
  • King's College London
    Postgraduate Taught Courses - Arts & Sciences - Strand Campus Postgraduate
    Wed, 20 Mar '19
  • University of East Anglia
    All Departments Open 13:00-17:00. Find out more about our diverse range of subject areas and career progression in the Arts & Humanities, Social Sciences, Medicine & Health Sciences, and the Sciences. Postgraduate
    Wed, 20 Mar '19

Where do you need more help?

Which Uni should I go to? (34)
14.11%
How successful will I become if I take my planned subjects? (21)
8.71%
How happy will I be if I take this career? (50)
20.75%
How do I achieve my dream Uni placement? (34)
14.11%
What should I study to achieve my dream career? (30)
12.45%
How can I be the best version of myself? (72)
29.88%

Watched Threads

View All