Computer-science_A-level_Cie
-
computers-and-components6 主题
-
logic-gates-and-logic-circuits2 主题
-
central-processing-unit-cpu-architecture6 主题
-
assembly-language-4 主题
-
bit-manipulation1 主题
-
operating-systems3 主题
-
language-translators2 主题
-
data-security3 主题
-
data-integrity1 主题
-
ethics-and-ownership3 主题
-
database-concepts3 主题
-
database-management-systems-dbms-1 主题
-
data-definition-language-ddl-and-data-manipulation-language-dml1 主题
-
computational-thinking-skills1 主题
-
algorithms14 主题
-
data-types-and-records2 主题
-
arrays2 主题
-
files1 主题
-
introduction-to-abstract-data-types-adt1 主题
-
programming-basics1 主题
-
constructs2 主题
-
structured-programming1 主题
-
program-development-life-cycle2 主题
-
program-design-2 主题
-
program-testing-and-maintenance3 主题
-
user-defined-data-types1 主题
-
file-organisation-and-access-3 主题
-
floating-point-numbers-representation-and-manipulation3 主题
-
protocols2 主题
-
circuit-switching-packet-switching1 主题
-
processors-parallel-processing-and-virtual-machines5 主题
-
boolean-algebra-and-logic-circuits4 主题
-
purposes-of-an-operating-system-os3 主题
-
translation-software3 主题
-
encryption-encryption-protocols-and-digital-certificates3 主题
-
artificial-intelligence-ai4 主题
-
recursion1 主题
-
programming-paradigms4 主题
-
object-oriented-programming7 主题
-
file-processing-and-exception-handling2 主题
-
data-representation5 主题
-
multimedia3 主题
-
compression2 主题
-
networks-and-the-internet11 主题
procedures-and-functions
What are functions and procedures?
-
Functions and procedures are a type of sub program, a sequence of instructions that perform a specific task or set of tasks
-
Procedures and functions are defined at the start of the code
-
Sub programs are often used to simplify a program by breaking it into smaller, more manageable parts
-
Sub programs can be used to:
-
Avoid duplicating code and can be reused throughout a program
-
Improve the readability and maintainability of code
-
Perform calculations, to retrieve data, or to make decisions based on input
-
-
Parameters are values that are passed into a sub program
-
Parameters can be variables or values and they are located in brackets after the name of the sub program
-
Example:
FUNCTION TaxCalculator(pay,taxcode)ORPROCEDURE TaxCalculator(pay,taxcode)
-
-
Sub programs can have multiple parameters
-
To use a sub program you ‘call‘ it from the main program
What’s the difference between a function and procedure?
-
A Function returns a value whereas a procedure does not
Procedures
-
Procedures are defined using the PROCEDURE keyword in pseudocode
-
A procedure can be written as:
|
Pseudocode |
|
|---|---|
|
|
-
The identifier is used to call the procedure
-
To call a procedure, it can be written as:
CALL <identifier>()
CALL <identifier>(Value1,Value2,...)
-
When parameters are used,
value1, value2...must be of the correct data type and in the same sequence
Example code
PROCEDURE GreetUser(Name : STRING) OUTPUT "Hello, ", Name, "!"
ENDPROCEDURE DECLARE UserName : STRING OUTPUT "Enter your name: "
INPUT UserName CALL GreetUser(UserName)
-
PROCEDURE GreetUserdefines the procedure, which takes one parameter:Name -
The procedure prints a personalised greeting
-
The main program asks for the user’s name and then calls the procedure using
CALL
Functions
-
Functions are defined using the FUNCTION keyword in pseudocode or def keyword in Python
-
Functions always return a value so they do not use the
CALLfunction, instead they are called within an expression -
A function can be written as:
|
Pseudocode |
|
|---|---|
|
|
Example code
FUNCTION calculate_area(length: INTEGER, width: INTEGER) area ← length * width RETURN area
ENDFUNCTION # Output the value returned from the function
OUTPUT(calculate_area(5,3))
-
Defines a function called
calculate_area -
It takes two parameters:
lengthandwidth, both of typeINTEGER -
A local variable
areais declared and assigned the result oflength × width -
The function returns the result to wherever it was called from
Parameter passing
What is parameter passing?
-
Parameter passing is the method by which values or references are given to procedures or functions so they can use or modify data during execution
-
When writing procedures, you can control how parameters are passed using:
-
BYVAL– pass a copy of the value (default behaviour) -
BYREF– pass the actual variable, so changes inside the procedure affect the original
-
|
Keyword |
What it does |
|---|---|
|
|
Passes a copy of the value (no change to original) |
|
|
Passes a reference to the variable (can be modified) |
-
If no keyword is used, it is assumed to be
BYVAL -
Functions should not use
BYREFparameters – only procedures should
Example – Swapping two values (Procedure with BYREF)
PROCEDURE Swap(BYREF X : INTEGER, Y : INTEGER) DECLARE Temp : INTEGER Temp ← X X ← Y Y ← Temp
ENDPROCEDURE
-
Xis passed by reference, so it can be changed -
Yis passed by value, so any change toYinside the procedure does not affect the original value
Worked Example
A video-conferencing program supports up to six users. Speech from each user is sampled and digitised (converted from analogue to digital). Digitised values are stored in array Sample.
The array Sample consists of 6 rows by 128 columns and is of type integer. Each row contains 128 digitised sound samples from one user.
The digitised sound samples from each user are to be processed to produce a single value which will be stored in a 1D array Result of type integer. This process will be implemented by procedure Mix().
A procedure Mix() will:
-
calculate the average of each of the 6 sound samples in a column
-
ignore sound sample values of 10 or less
-
store the average value in the corresponding position in
Result -
repeat for each column in array
Sample
The diagram uses example values to illustrate the process:

Write pseudocode for procedure Mix().
Assume Sample and Result are global. [6]
Answer
PROCEDURE Mix() DECLARE Count, Total ThisNum : INTEGER DECLARE ThisUser, ThisSample : INTEGER FOR ThisSample ← 1 TO 128 Count ← 0 Total ← 0 FOR ThisUser ← 1 TO 6 IF Sample[ThisUser, ThisSample] > 10 THEN Count ← Count + 1 Total ← Total + Sample[ThisUser, ThisSample] ENDIF NEXT ThisUser Result[ThisSample] ← INT(Total / Count) NEXT ThisSample ENDPROCEDURE
-
Declaration and initialisation before inner loop of
CountandTotal[1 mark] -
Outer Loop for 128 iterations [1 mark]
-
Inner loop for six iterations [1 mark]
-
Test for sample > 10 in a loop [1 mark]
-
and if true sum
Totaland incrementCount[1 mark] -
Calculate average value and assign to
Resultarray after inner loop and within outer loop [1 mark] -
Use of
INT()/ DIVto convert average to integer [1 mark]
Responses