Operators, strings and subroutines
Introducido| English | Significado |
|---|---|
| operators/ˈɒpəreɪtəz/ | operadores |
| procedure/prəˈsiːdʒə/ | procedimiento |
| function/ˈfʌŋkʃn/ | función |
| relational/rɪˈleɪʃənl/ | relacional |
| logical/ˈlɒdʒɪkl/ | lógico |
| parameter/pəˈræmɪtə/ | parámetro |
| local variable/ˈləʊkl ˈveərɪəbl/ | variable local |
| global/ˈɡləʊbl/ | global |
The tools of a program
- Programs combine values with operators 运算符 and work with strings.
- To avoid repeating code, we use procedures 过程 and functions 函数.
- Good naming and comments make a program maintainable.
Operators
- Arithmetic:
+ - * /,^(power),MOD(remainder),DIV(whole-number part). e.g.17 MOD 5 = 2,17 DIV 5 = 3. - Relational 关系 (give a Boolean):
= < <= > >= <>(not equal). - Logical 逻辑:
AND(both true),OR(at least one),NOT(reverses).

What is 17 MOD 5?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
MOD gives the remainder: 17 ÷ 5 = 3 remainder 2.
What is 17 DIV 5?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
DIV gives the whole-number part: 17 ÷ 5 = 3 (remainder discarded).
String handling
- A string is made of characters. Useful operations:
- LENGTH (number of characters), SUBSTRING (a part), UCASE (capitals), LCASE (small).
name ← "Computer"
OUTPUT LENGTH(name) // 8
OUTPUT SUBSTRING(name, 1, 4) // "Comp"
SUBSTRING: take part of a string
The first character is at position 1. SUBSTRING(Word, Start, Length) takes Length characters beginning at Start — drag the two handles and read the call it builds.
What does LENGTH("Computer") return?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
"Computer" has 8 characters.
Match each operator or string function to what it gives.
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
MOD/DIV split a division into remainder and quotient; LENGTH and SUBSTRING work on strings.
Procedures, functions and good style
- A procedure does a task (run with
CALL); a function returns a value. - A parameter 参数 is a value passed in (up to three).
- A local variable 局部变量 exists only inside its subroutine (safer); a global 全局 is visible everywhere.
- Library routines:
MOD,DIV,ROUND,RANDOM. - A maintainable program uses meaningful names, comments, and subroutines.
A local variable:
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Locals are safer because other parts of the program cannot change them by mistake.
A function returns a value (used in an expression) while a procedure just performs a task, and a local variable can only be used inside the subroutine where it is declared.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Functions return values; locals keep variables safe inside their own subroutine — both make code easier to maintain.
Worked example: minutes to hours
A film lasts 135 minutes. Use DIV and MOD to output it in hours and minutes, then write it as a function.
DIV(135, 60)= 2 (the whole number of hours) andMOD(135, 60)= 15 (the remainder), so the film is 2 hours 15 minutes.- As a function with a parameter, it returns a value to the program that called it:
FUNCTION Hours(Mins : INTEGER) RETURNS INTEGER
RETURN DIV(Mins, 60)
ENDFUNCTION
What is 23 MOD 4?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
23 = 5 × 4 + 3, so the remainder is 3.
Name ← "Shanghai". What does SUBSTRING(Name, 1, 5) return?
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
It takes 5 characters starting at position 1.
You've got it
MOD= remainder,DIV= whole-number part; logicalAND/OR/NOT- strings: LENGTH, SUBSTRING, UCASE, LCASE
- a procedure does a task; a function returns a value; parameters pass values in
- local variables are safer than global; maintainable = meaningful names + comments + subroutines