Pseudocode Core Structures (Cambridge Standard): Function Return Values And Common Syntax Errors (Copy)
Function Return Values And Common Syntax Errors (Cambridge Standard – O Level 2210 + IGCSE 0478)
Purpose Of Functions In Cambridge Pseudocode
- Functions are used to:
- Perform a calculation
- Return a single value to the calling program
- Cambridge assesses functions to test:
- Correct use of RETURN
- Data flow from function to main program
- Clear distinction between procedures and functions
- Functions are:
- Expression-based
- Used where a value is required for further processing
What Cambridge Means By A Function
- A function is:
- A named block of pseudocode
- That performs calculations
- And returns exactly one value
- Functions:
- Must include a RETURN statement
- Are often used in assignments or conditions
Core Function Definition Structure (Cambridge Standard)
- FUNCTION functionName(parameterList)
- statements
- RETURN value
- ENDFUNCTION
Key Rules Examiners Enforce
- FUNCTION keyword must be used
- ENDFUNCTION is mandatory
- RETURN must:
- Appear exactly once logically
- Return a single value
- RETURN must be:
- Inside the function
- Not in the main program
Example Function Definition
- FUNCTION calculateAverage(total, count)
- average ← total / count
- RETURN average
- ENDFUNCTION
Where Functions Are Defined
- Functions are usually written:
- Before the main program
- Best practice:
- Define all functions first
- Call them later
- Cambridge accepts:
- Any order
- Examiner preference:
- Clear structure and separation
Function Calls And Usage
Standard Function Call In Assignment
- result ← functionName(arguments)
Example:
- avg ← calculateAverage(sum, number)
Function Call Inside Expressions
- IF calculateAverage(total, count) >= 50 THEN
- OUTPUT “Pass”
- ENDIF
Examiner Expectations
- Returned value must:
- Be used immediately
- Function calls:
- Must not stand alone
Incorrect:
- calculateAverage(total, count)
Correct:
- avg ← calculateAverage(total, count)
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change O Level And IGCSE Computer Science Full Scale Course
RETURN Statement Rules (High-Risk Area)
Rule 1: RETURN Ends Function Execution
- Once RETURN executes:
- Function stops immediately
- Any code after RETURN:
- Is ignored logically
Rule 2: RETURN Must Return A Value
Incorrect:
- RETURN
Correct:
- RETURN total
Rule 3: Only One Value Returned
Incorrect:
- RETURN total, average
Correct:
- RETURN average
Rule 4: RETURN Belongs Only In Functions
- Procedures must NOT use RETURN
- RETURN inside procedure:
- Guaranteed mark loss
Parameters And Return Values (Data Flow)
- Parameters:
- Pass data into the function
- RETURN:
- Sends data out of the function
- Functions should:
- Not rely on global variables
Example Data Flow
- FUNCTION square(n)
- RETURN n * n
- ENDFUNCTION
Call:
- result ← square(5)
- n receives 5
- RETURN sends 25
- result becomes 25
Pass-By-Value Understanding (Cambridge Level)
- Parameters in functions:
- Are treated as local copies
- Changing parameter inside function:
- Does not affect original variable
Example
- FUNCTION addTen(x)
- x ← x + 10
- RETURN x
- ENDFUNCTION
Main:
- num ← 5
- num ← addTen(num)
- num becomes:
- 15 (due to assignment, not direct modification)
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change O Level And IGCSE Computer Science Full Scale Course
Functions Vs Procedures (Critical Examiner Distinction)
| Function | Procedure |
|---|---|
| Returns a value | No return value |
| Uses RETURN | No RETURN |
| Used in expressions | Called as statement |
| Assigns result | Performs action |
Examiner Trap
- Writing a procedure when a function is required
- Or writing a function but never using RETURN value
Common Function Syntax Errors (Guaranteed Mark Loss)
Error 1: Missing RETURN
- FUNCTION calculateSum(a, b)
- total ← a + b
- ENDFUNCTION
Why it fails:
- No value returned
Error 2: RETURN Outside Function
- RETURN result
- Illegal unless inside FUNCTION block
Error 3: Multiple RETURN Statements With No Logic
- RETURN x
- RETURN y
- Causes ambiguity
- Not accepted unless conditional and logically exclusive
Error 4: Using FUNCTION Like PROCEDURE
- calculateAverage(total, count)
- No assignment
- Returned value wasted
Error 5: Returning Wrong Variable
- FUNCTION calculateTotal(a, b)
- sum ← a + b
- RETURN total
- ENDFUNCTION
- total not defined
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change O Level And IGCSE Computer Science Full Scale Course
Functions And Selection Logic
- Functions are commonly used:
- Inside IF conditions
- Example:
- IF calculateScore(mark) >= passMark THEN
- OUTPUT “Pass”
- ELSE
- OUTPUT “Fail”
- ENDIF
Examiner Focus
- Function must:
- Return compatible data type
- Comparison must:
- Make logical sense
Functions And Iteration
- Functions may be called:
- Inside loops
Example:
- FOR i ← 1 TO n
- total ← total + square(values[i])
- ENDFOR
Examiner Expectations
- Function call:
- Does not alter loop control
- Returned value:
- Used in accumulation
Functions And Trace Tables
- During tracing:
- Treat function as inline calculation
- Track:
- Parameter values
- Returned value
- Do NOT:
- Ignore function body
Common Trace Errors
- Forgetting to substitute parameter values
- Ignoring RETURN
- Assuming function modifies external variables
Functions In Section B Modifications
- Section B may ask:
- Write a function
- Modify a function’s logic
- Use a function in new condition
- Safe approach:
- Keep function simple
- Use clear RETURN
Illegal Function Practices (Guaranteed Mark Loss)
- Using OUTPUT instead of RETURN
- Using RETURN in a procedure
- Missing ENDFUNCTION
- Returning multiple values
- Using undeclared variables in RETURN
- Calling function without using return value
Best-Practice Function Strategy For Paper 2
- Use function when:
- A value is needed
- Use procedure when:
- An action is needed
- Always:
- Include RETURN
- Assign returned value
- Keep functions:
- Short
- Single-purpose
Final Quality Checklist
- FUNCTION and ENDFUNCTION present
- RETURN present and correct
- Single value returned
- Returned value used
- Parameters match arguments
- No OUTPUT inside function (unless explicitly required)
Final Lock-In Rules
- Functions return values
- RETURN is mandatory
- Returned values must be used
- Functions are not procedures
- Clean function syntax = guaranteed Paper 2 marks
