Unable to open this exercise
Common exam pitfall
Students often claim functions and procedures are interchangeable, forgetting that functions must return a value.
In structured programming, complex programs are broken down into manageable subroutines. While procedures perform tasks without sending back a value, functions are specialized subroutines designed specifically to compute and return a single result.
1. What is a Function?
A function is a self-contained block of code that:
- Accepts zero or more input parameters.
- Performs a calculation or data manipulation.
- Returns exactly one value of a declared data type back to the calling statement.
Because a function yields a value, it is invoked wherever an expression is valid—such as the right-hand side of an assignment statement, inside an OUTPUT statement, or within a condition.
2. Functions vs. Procedures: Key Differences
Understanding the distinction between functions and procedures is a frequent exam requirement:
Feature | Procedure | Function |
|---|---|---|
Return Value | Does not return a value. | Returns exactly one value. |
Header Keyword |
|
|
Termination Keyword |
|
|
Return Mechanism | None (or updates |
|
How it is Called | Standalone statement with | Part of an expression or assignment: <br> |
Can use | Yes (mandatory in Cambridge pseudocode). | No (syntax error). |
3. Function Declaration Syntax
The standard Cambridge pseudocode template for defining a function is:
FUNCTION FunctionIdentifier(Parameter1 : DataType, Parameter2 : DataType) RETURNS ReturnDataType
// Local declarations and computation
RETURN CalculatedValue
ENDFUNCTION
Key Elements:
FUNCTION: The opening keyword declaring the subroutine.RETURNS ReturnDataType: Declares the data type of the value that the function will yield (e.g.,RETURNS REAL,RETURNS BOOLEAN).RETURN Value: Halts function execution and transmits the value back to the caller.ENDFUNCTION: Closes the subroutine definition.
4. Worked Example: Currency Conversion Function
Suppose an e-commerce platform needs a function that converts an amount in US Dollars to British Pounds given an exchange rate:
FUNCTION ConvertUSDToGBP(AmountUSD : REAL, Rate : REAL) RETURNS REAL
DECLARE ConvertedAmount : REAL
ConvertedAmount <- AmountUSD * Rate
RETURN ConvertedAmount
ENDFUNCTION
// Calling the function:
DECLARE PriceInUSD : REAL
DECLARE PriceInGBP : REAL
PriceInUSD <- 49.99
PriceInGBP <- ConvertUSDToGBP(PriceInUSD, 0.79)
OUTPUT "Price in GBP: £", PriceInGBP
Trace of Execution:
- The main program assigns
49.99toPriceInUSD. - The assignment
PriceInGBP <- ConvertUSDToGBP(PriceInUSD, 0.79)callsConvertUSDToGBP. - Parameter
AmountUSDreceives49.99, andRatereceives0.79. ConvertedAmountis calculated as49.99 * 0.79 = 39.4921.RETURN ConvertedAmountpasses39.4921back to the caller.- The calling assignment receives
39.4921and stores it intoPriceInGBP.
5. Parameter Passing: By Value vs By Reference
- By Value (
BYVAL): The default passing mechanism for functions. A copy of the caller's value is passed into the subroutine. Any modifications to the parameter inside the function do not affect the caller's variable. Functions should avoid altering parameter variables to maintain predictability (no side effects). - By Reference (
BYREF): Passes a pointer to the original variable in memory. While valid in procedures, functions should strictly useRETURNto pass information out.
6. Common Pseudocode Pitfalls
- Calling a function with
CALL:
// INCORRECT
CALL CalculateArea(10, 5)
// CORRECT
Area <- CalculateArea(10, 5)
- Missing
RETURNS DataType: Omitting the return type in the function header is a syntax violation. - Missing
RETURN: Failing to provide aRETURNstatement leaves the calling expression undefined. - Returning multiple values: In Cambridge pseudocode, a function cannot return a tuple or list of values; it must return exactly one value.
AI Socratic Tutor
Need guidance? Ask a question or use the hint below.
AI Socratic Tutor is an All-Access Benefit
Unlock step-by-step Socratic hints, algorithm trace table walkthroughs, and error diagnostics for every Cambridge exercise.
You're chatting with an AI tutor, not a person. AI can make mistakes, so double-check its answers.
Exam-Style Check: Core Characteristics of Functions vs Procedures
• Multiple ChoiceWhich statement correctly describes a key difference between a function and a procedure in Cambridge pseudocode?
Interactive exercises for Module 2 and beyond require an active plan.