Functions (return values) vs procedures (no return). Parameters and arguments. Scope. Why use subprograms.
| Term | Description |
|---|---|
| Procedure | |
| Function | |
| Procedure example | |
| Function example | |
| Edexcel pseudocode syntax |
| Term | Description |
|---|---|
| Parameter | |
| Argument | |
| FUNCTION add(a, b) | |
| result = add(3, 7) | |
| Purpose |
| Term | Description |
|---|---|
| Local variable | |
| Global variable | |
| Best practice | |
| Why scope matters | |
| Name clash rule |
PROCEDURE greet(name)
PRINT "Hello, " + name + "!"
PRINT "Welcome to the program."
END PROCEDURE
// Calling the procedure:
CALL greet("Ander")
// Output:
// Hello, Ander!
// Welcome to the program.FUNCTION calculateArea(length, width)
area = length * width
RETURN area
END FUNCTION
// Calling the function:
roomArea = calculateArea(5, 3)
PRINT roomArea // Output: 15
// Can use directly in expression:
PRINT "Area = " + STR(calculateArea(4, 7))FUNCTION isPrime(n)
// n is a parameter — local to this function
IF n < 2 THEN
RETURN False
END IF
FOR i = 2 TO n - 1
IF n MOD i == 0 THEN
RETURN False
END IF
END FOR
RETURN True
END FUNCTION
// The variable i is local — does not exist outside isPrimeWhat is the key difference between a function and a procedure?
Write a function called maxValue that takes two parameters, a and b, and returns the larger of the two values. Show how you would call it to find the larger of 15 and 23. (4 marks)
A variable called total is declared inside a function. What happens to it when the function finishes?
Study this pseudocode: FUNCTION double(n) result = n * 2 END FUNCTION total = double(5) PRINT total Explain what is wrong and write the corrected version. (3 marks)
In the definition: FUNCTION power(base, exponent), what are "base" and "exponent"?