Subtopic Notes

I - Subroutine

9-11. Data Types and Structures, Programming

Subroutine

  • Subroutine is a reusable block of code that is given a name
  • Designed to perform a specific task
  • Often called within a program to avoid repetition.
  • Can be tested/improved independently of program
  • Easy to share procedures/functions with other programs
  • Create routines that can be called like built-in command
  • Header: The first statement in subroutine definition
    • Includes name of the subroutine
    • Includes parameter passed along with data type
    • Includes return data type if it exists

Procedure

  • Subroutine that does not return a value
  • Procedure calls are standalone statements

Function

  • Subroutine that returns a value.
  • Function calls are always made as part of an expression
  • The keyword RETURN is used as one of the statements within the body of the function to specify the value to be returned.
  • Normally, this will be the last statement in the function definition, however, if the RETURN statement is in the body of the function its execution is immediate and any subsequent lines of code are omitted.
  • Because a function returns a value that is used when the function is called, function calls are not complete program statements
  • When the RETURN statement is executed, the value returned replaces the function call in the expression and the expression is then evaluated

Parameter

  • Variables used in a subroutine to accept input values, allowing data to be passed into the subroutine.
  • A subroutine may or may not have parameters.
  • When calling a function, the parameters used must be of correct data type and of the same sequence as the definition
  • Parameters should not be passed by reference to a function.
  • Unless otherwise stated, it should be assumed that parameters are passed by value.
  • Parameters can be passed either by reference or by value (not supported in python)
  • Pass by value: Data copied into procedure so variable not changed outside procedure
  • Pass by reference: Link to variable provided so variable changed after going through procedure

Argument: The actual value or data passed to the subroutine when it is called

Local Variable: Variables defined within a function or block, accessible only within that specific scope. These variables are not accessible outside the subroutine

Global Variable: Variables defined outside all functions or blocks, accessible throughout the entire program.

Procedure/Function Interface: The way a procedure or function interacts with the rest of the program. It includes its name, parameters (inputs), and, for functions, the return value.

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

To specify whether a parameter is passed by value or by reference, the keywords BYVAL and BYREF precede the parameter in the definition of the procedure.

If there are several parameters passed by the same method, the BYVAL or BYREF keyword need not be repeated.

Example – passing parameters by reference

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Library Routines

  • A library routine is a pre-written, reusable function or procedure provided by a programming library to perform common tasks, such as mathematical operations or input/output handling.
  • IDEs typically include a standard library of functions and procedures
  • Programming language development systems often offer library routines that can be easily integrated into programs
  • Some examples of library routines in pseudocode are: MOD, DIV, ROUND, RANDOM

Best Coding Practices

  • Use meaningful identifiers name for variables, constants, arrays, procedure, functions
  • Use commenting feature wisely
  • Divide the program into different modules using procedures and functions

String Manipulation

Strings are conceptually like arrays of characters. This means many operations done in arrays can be done in string. In pseudocode, the first character index may be 0 or 1, but for the functions of the insert it is not zero indexed.

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Slicing/Substring: Returns a range of characters by specifying a start and end index (exclusive)
Concatenation: Combining two or more strings together

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Pseudocode String Functions

The Cambridge International AS & A Level syllabus (9618) specifically requires candidates to know string manipulation functions in their chosen programming language. Pseudocode string manipulation functions will always be provided in examinations (In Insert). Some basic string manipulation functions are given here.

An error will be generated if a function call is not properly formed or if the parameters are of an incorrect type or an incorrect value.

String and character functions

  • A string of length 1 may be considered to be either of type CHAR or STRING
  • A CHAR may be assigned to, or concatenated with, a STRING
  • A STRING of length greater than 1 cannot be assigned to a CHAR
  • These functions are not zero-indexed
String and character functions (These will be given in insert)
LEFT(ThisString : STRING, x : INTEGER) RETURNS STRING returns leftmost x characters from ThisString
RIGHT(ThisString : STRING, x : INTEGER) RETURNS STRING returns rightmost x characters from ThisString
LENGTH(ThisString : STRING) RETURNS INTEGER returns the integer value representing the length of ThisString
MID(ThisString : STRING, x : INTEGER, y : INTEGER) RETURNS STRING returns a string of length y starting at position x from ThisString
UCASE(ThisChar : CHAR) RETURNS CHAR returns the character value representing the upper-case equivalent of ThisChar
TO_UPPER(ThisString : STRING) RETURNS STRING returns a string formed by converting all characters of ThisString to uppercase
LCASE(ThisChar : CHAR) RETURNS CHAR returns the character value representing the lower-case equivalent of ThisChar
TO_LOWER(ThisString : STRING) RETURNS STRING returns a string formed by converting all characters of ThisString to lowercase
NUM_TO_STR(x : <datatype1>) RETURNS <datatype2> returns a string representation of a numeric value (supports -ve). <datatype1> may be REAL or INTEGER, <datatype2> may be CHAR or STRING
STR_TO_NUM(x : <datatype1>) RETURNS <datatype2> returns a numeric representation of a string. <datatype1> may be CHAR or STRING, <datatype2> may be REAL or INTEGER
IS_NUM(ThisString : <datatype>) RETURNS BOOLEAN returns TRUE if ThisString represents a valid numeric value. <datatype> may be CHAR or STRING
ASC(ThisChar : CHAR) RETURNS INTEGER returns an integer value (the ASCII value) of character ThisChar
CHR(x : INTEGER) RETURNS CHAR returns the character whose integer value (the ASCII value) is x
In pseudocode, the operator & is used to concatenate (join) two strings. Example: "Summer" & " " & "Pudding" produces "Summer Pudding"

Where string operations (such as concatenation, searching and splitting) are used in a programming language, these should be explained clearly, as they vary considerably between systems.

Where functions in programming languages are used to format numbers as strings for output, their use should also be explained.

Numeric Functions

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Pseudocode Numeric Functions

Pseudocode Numeric Functions
INT(x : REAL) RETURNS INTEGER returns the integer part of x
RAND(x : INTEGER) RETURNS REAL returns a real number in the range 0 to x (not inclusive of x). Example: RAND(87) could return 35.430729

Date Functions

  • There is not built in data type of date, however we can import a module to make it work
  • Date format pseudocode is assumed to be DD/MM/YYYY unless otherwise stated.

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Pseudocode Date Functions

Pseudocode Date Functions
DAY(ThisDate : DATE) RETURNS INTEGER returns the day number from ThisDate
MONTH(ThisDate : DATE) RETURNS INTEGER returns the month number from ThisDate
YEAR(ThisDate : DATE) RETURNS INTEGER returns the year number from ThisDate
DAYINDEX(ThisDate : DATE) RETURNS INTEGER returns the day index number from ThisDate where Sunday = 1, Monday = 2 etc.
SETDATE(Day, Month, Year : INTEGER) RETURNS DATE returns a value of type DATE with the value of <Day>/<Month>/<Year>
TODAY() RETURNS DATE returns a value of type DATE corresponding to the current date.