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

Watch
Announcements
#1
MAIN
X = 2
Y = 4
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
2 years 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
2 years 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
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
#4
thanks <3
0
X

new posts
Back
to top
Latest
My Feed

### Oops, nobody has postedin the last few hours.

Why not re-start the conversation?

see more

### See more of what you like onThe Student Room

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

### Poll

Join the discussion

#### Do you think receiving Teacher Assessed Grades will impact your future?

I'm worried it will negatively impact me getting into university/college (59)
37.34%
I'm worried that I’m not academically prepared for the next stage in my educational journey (16)
10.13%
I'm worried it will impact my future career (8)
5.06%
I'm worried that my grades will be seen as ‘lesser’ because I didn’t take exams (37)
23.42%
I don’t think that receiving these grades will impact my future (24)
15.19%
I think that receiving these grades will affect me in another way (let us know in the discussion!) (14)
8.86%