Unit 11.5 Pseudocode - Procedures and Functions
Know what is meant by the terms procedure and function.
Know what is meant by the term parameter passing and the difference between parameter passing by value and by reference.
Be able to write pseudocode that uses procedures and functions.
Single block of code, when called from another point in the program, the procedure completes a task and then hands control back to line that called it.
May or may not have parameters - parameters are a variable or piece of data passed into the procedure to allow it to complete the task.
Syntax: (if there aren’t any parameters leave it as () )
E.g. Procedure - addition that will accept two numbers as parameters, add them and output total
PROCEDURE addition (num1 : INTEGER, num2 : INTEGER)
// DECLARE total : INTEGER
// total ← num1 + num2
// OUTPUT total
ENDPROCEDURE
Procedures can be called
CALL <identifier> (value1, value2,…)
e.g. to call procedure addition & pass in 2 numbers as parameters would be: (If no parameters leave as () )
CALL addition (2,5)
Parameter can be passed by reference or by value
By Reference - Procedure receives the memory location of where the data/variable is stored. Meaning that whilst the procedure is being executed, it will work with the original data stored in the variable and overwrite it, the changes remain after procedure ends.
By Value - Procedure receives a copy of the data/variable. Meaning that whilst the procedure is being executed it will work with the copy of the data, any changes made are lost as soon as procedure is not in use so it wont overwrite the original data.
to specify how the parameter is passed, BYREF/BYVAL precede the parameter in the definition of the procedure (if both passes are the same it only needs to mentioned once at the beginning) e.g. (BYREF num1:Integer,num2:INTEGER)
e.g. PROCEDURE swap (BYREF num1:Integer,BYVAL num2:INTEGER)
EXAM - IF PARAMETER NOT SPECIFIED ASSUME BYVAL!! (PARAMETERS CAN ONLY BE PASSED INTO A FUNCTION BY VALUE AND SHOULDNT BE BY REFERENCE)
Similar to procedure, block of code when called from another point in the program completes a task, once done it hands control back to the original line of code that called it but returns with a value
May or may not have parameters (variable or data passed into a function to allow it to complete task, can only be passed into a function by value and shouldn’t be by reference)
E.g. Function that will accept two numbers as parameters, add them and return the total
FUNCTION addition (num1 : INTEGER, num2 : INTEGER) RETURNS INTEGER
// DECLARE total : INTEGER
// total ← num1 + num2
RETURN total
ENDFUNCTION
Return is used within the body of the function to specify the value to be returned, usually last statement in function definition however if RETURN statement is earlier in the body of the function its execution is immediate and any subsequent lines are omitted
As functions return a value when called they are NOT complete program statements, DONT USE CALL IN FUNCTIONS,
Functions should only be called as part of an expression, when RETURN is executed the value returned replaces the function call in the expression and the expression is then evaluated.
<identifier> ←functionName (parameters)
the identifier here will store the return value from the function
e.g. to call function addition and pass in some numbers would be
result ← addition (2,5)
Know what is meant by the terms procedure and function.
Know what is meant by the term parameter passing and the difference between parameter passing by value and by reference.
Be able to write pseudocode that uses procedures and functions.
Single block of code, when called from another point in the program, the procedure completes a task and then hands control back to line that called it.
May or may not have parameters - parameters are a variable or piece of data passed into the procedure to allow it to complete the task.
Syntax: (if there aren’t any parameters leave it as () )
E.g. Procedure - addition that will accept two numbers as parameters, add them and output total
PROCEDURE addition (num1 : INTEGER, num2 : INTEGER)
// DECLARE total : INTEGER
// total ← num1 + num2
// OUTPUT total
ENDPROCEDURE
Procedures can be called
CALL <identifier> (value1, value2,…)
e.g. to call procedure addition & pass in 2 numbers as parameters would be: (If no parameters leave as () )
CALL addition (2,5)
Parameter can be passed by reference or by value
By Reference - Procedure receives the memory location of where the data/variable is stored. Meaning that whilst the procedure is being executed, it will work with the original data stored in the variable and overwrite it, the changes remain after procedure ends.
By Value - Procedure receives a copy of the data/variable. Meaning that whilst the procedure is being executed it will work with the copy of the data, any changes made are lost as soon as procedure is not in use so it wont overwrite the original data.
to specify how the parameter is passed, BYREF/BYVAL precede the parameter in the definition of the procedure (if both passes are the same it only needs to mentioned once at the beginning) e.g. (BYREF num1:Integer,num2:INTEGER)
e.g. PROCEDURE swap (BYREF num1:Integer,BYVAL num2:INTEGER)
EXAM - IF PARAMETER NOT SPECIFIED ASSUME BYVAL!! (PARAMETERS CAN ONLY BE PASSED INTO A FUNCTION BY VALUE AND SHOULDNT BE BY REFERENCE)
Similar to procedure, block of code when called from another point in the program completes a task, once done it hands control back to the original line of code that called it but returns with a value
May or may not have parameters (variable or data passed into a function to allow it to complete task, can only be passed into a function by value and shouldn’t be by reference)
E.g. Function that will accept two numbers as parameters, add them and return the total
FUNCTION addition (num1 : INTEGER, num2 : INTEGER) RETURNS INTEGER
// DECLARE total : INTEGER
// total ← num1 + num2
RETURN total
ENDFUNCTION
Return is used within the body of the function to specify the value to be returned, usually last statement in function definition however if RETURN statement is earlier in the body of the function its execution is immediate and any subsequent lines are omitted
As functions return a value when called they are NOT complete program statements, DONT USE CALL IN FUNCTIONS,
Functions should only be called as part of an expression, when RETURN is executed the value returned replaces the function call in the expression and the expression is then evaluated.
<identifier> ←functionName (parameters)
the identifier here will store the return value from the function
e.g. to call function addition and pass in some numbers would be
result ← addition (2,5)