Parameter passing

From Higher Computing Science
Revision as of 15:12, 18 January 2016 by Davidferguson (Talk | contribs) (When to use pass by value or reference: Added more information about when to use each, and also included specific examples)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This article is unfinished. Please consider joining and adding to this article. Read about Page layout beforehand.

Key points

  • Parameters are items of data that can be passed between the subprograms
  • To pass data into a subprogram, a list of parameters is given
  • To make it easy for different people to write subprograms that work together, the name of a parameter inside the subprogram can be different from its name outside of the subprogram.
  • The name given to the parameter within a subprogram is a formal parameter
  • The name given to the parameter when it is called is the actual parameter.
  • Actual parameters may also be numbers rather than variables, such as in the function call ROUND(30.423,1)
  • If a copy of the variable passed into a subprogram is used, it will not change the original variable. This is called pass by value.
  • If a reference (the address in memory) of a variable is passed into a subprogram, the subprogram can change the original variable. This is called pass by reference.

Information

A parameter is a way of passing a value to a sub program. A simple example would be a subprogram called add:
SUB add
  LET total = a+b  
  PRINT total
END SUB
The code above will work for any program that has stored values in variables a and b. No other variables can be used with this code. This is highly inflexible and requires
SUB add(a,b)
  LET total = a+b
  PRINT total
END SUB
This version has parameters. The parameters are called a and b. The code of this subprogram still uses the variables a and b, but the rest of the program does not have to, because the parameters can act as a channel to pass on variables for use in the subprogram.

Param1.png


In the example above, the add subprogram is used three times. It should be apparent that it is reused with different numbers. Parameters make a sub very flexible, because data can be sent from any part of the program.

Here is an example in python using one simple parameter to reuse code.

Actual and formal parameters

In the code above, the parameter's name is consistent within the subprogram. This is what we would expect in a program - that a variable is identified by its name. When the CALL statement is used to access the subprogram, the parameters are not consistently named.

The name given to the data placed into CALL statements is an actual parameter. The name given to the parameter names used in a subprogram is a formal parameter.

Param2.png

An analogy that may help to memorise this would be prisoners. All prisoners have actual names in the real world. Inside a prison, they are given formal names to identify themselves. These formal names do not relate to the actual names, but to the requirements of the prison system.

Actual parameters might be values, rather than variables.

Pass by reference and pass by value

When a value/variable is passed into a subprogram, one of three things will happen:

  • The value may be used but not changed
  • The value may be updated
  • The value may go from 0/null to a set value

Pass by value

When a variable is passed into a program but will not be changed, it can be passed by value. This means that only the contents of a variable (the number, string or Boolean value) is sent to the subprogram. The subprogram will not have access to the original variable in the CALL statement. Different languages show this in different ways. The example below uses TrueBASIC.

LET a = 56
LET b = 3
CALL add((a),b) ! a is passed by value

Pass by reference

When a variable is going to be changed within a subprogram, it must be passed by reference. The reference is the location of the variable in memory. The subprogram can then change the contents of the variable by updating the contents of the location in memory.

LET a = 56
LET b = 3
CALL add((a),b) ! b is passed by reference

When to use pass by value or reference

You should pass by value when a variable is not going to be changed in a subprogram. This is because if the variable is passed by reference, there is a chance that a mistake in the subprogram could cause the variable's contents to be changed, which would affect the entire program. However, if it was passed by value, the original variable's contents could not be changed by the subprogram, and therefore any mistakes in the subprogram would not affect the rest of the program.

Passing by reference should be used when a variable must be updated by a program. For example, a subprogram could take in one input, multiply this number by 10, and then pass it out as an output. In this case the parameter would be passed by reference, as then when the variable is changed by the subprogram, the rest of the program can see the change.

Data flow: In,out,in/out

Another way of describing data flow in a subprogram is to use the phrases in, out and in/out.

In: Data is flowing into the subprogram but not back out (passed by value) Out: A parameter will be identified in a subprogram that has no meaningful value. It will be given a value in the subprogram. (pass by reference) In/Out: A parameter will have a value when entering a subprogram, but the value will be changed within the subprogram (pass by reference)

Videos

Further information

Test yourself

Teaching resources