Back to 课程

Computer-science_A-level_Cie

0% Complete
0/0 Steps
  1. computers-and-components
    6 主题
  2. logic-gates-and-logic-circuits
    2 主题
  3. central-processing-unit-cpu-architecture
    6 主题
  4. assembly-language-
    4 主题
  5. bit-manipulation
    1 主题
  6. operating-systems
    3 主题
  7. language-translators
    2 主题
  8. data-security
    3 主题
  9. data-integrity
    1 主题
  10. ethics-and-ownership
    3 主题
  11. database-concepts
    3 主题
  12. database-management-systems-dbms-
    1 主题
  13. data-definition-language-ddl-and-data-manipulation-language-dml
    1 主题
  14. computational-thinking-skills
    1 主题
  15. algorithms
    14 主题
  16. data-types-and-records
    2 主题
  17. arrays
    2 主题
  18. files
    1 主题
  19. introduction-to-abstract-data-types-adt
    1 主题
  20. programming-basics
    1 主题
  21. constructs
    2 主题
  22. structured-programming
    1 主题
  23. program-development-life-cycle
    2 主题
  24. program-design-
    2 主题
  25. program-testing-and-maintenance
    3 主题
  26. user-defined-data-types
    1 主题
  27. file-organisation-and-access-
    3 主题
  28. floating-point-numbers-representation-and-manipulation
    3 主题
  29. protocols
    2 主题
  30. circuit-switching-packet-switching
    1 主题
  31. processors-parallel-processing-and-virtual-machines
    5 主题
  32. boolean-algebra-and-logic-circuits
    4 主题
  33. purposes-of-an-operating-system-os
    3 主题
  34. translation-software
    3 主题
  35. encryption-encryption-protocols-and-digital-certificates
    3 主题
  36. artificial-intelligence-ai
    4 主题
  37. recursion
    1 主题
  38. programming-paradigms
    4 主题
  39. object-oriented-programming
    7 主题
  40. file-processing-and-exception-handling
    2 主题
  41. data-representation
    5 主题
  42. multimedia
    3 主题
  43. compression
    2 主题
  44. networks-and-the-internet
    11 主题
课 Progress
0% Complete

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) OR PROCEDURE 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

PROCEDURE <identifier>() <statement(s)>
ENDPROCEDURE
PROCEDURE <identifier>(<param1>:<data type>, <param2>:<data type>...) <statement(s)>
ENDPROCEDURE
  • 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 GreetUser defines 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 CALL function, instead they are called within an expression

  • A function can be written as:

Pseudocode

FUNCTION <identifier> RETURNS <data type> <statement(s)>
ENDFUNCTION
FUNCTION <identifier>(<param1>:<data type>, <param2>:<data type>...) RETURNS <data type> <statement(s)>
ENDFUNCTION

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: length and width, both of type INTEGER

  • A local variable area is declared and assigned the result of length × 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

BYVAL

Passes a copy of the value (no change to original)

BYREF

Passes a reference to the variable (can be modified)

  • If no keyword is used, it is assumed to be BYVAL

  • Functions should not use BYREF parameters – 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
  • X is passed by reference, so it can be changed

  • Y is passed by value, so any change to Y inside 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:

Table with six rows and 128 columns, each filled with numbers. A row at the bottom shows the sum of selected columns, resulting in 20, 20, 35, 40, 46, 25.

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 Count and Total [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 Total and increment Count [1 mark]

  • Calculate average value and assign to Result array after inner loop and within outer loop [1 mark]

  • Use of INT()/ DIV to convert average to integer [1 mark]

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注