📁 Topic 2 · 2.3 Data Types and Structures
2.3c String manipulation and variable scope
Edexcel 4CP0 · iGCSE Computer Science · ~11 min read
Notes
──
Video
──
Slides
──
Worksheet
──
Quiz

String Functions

Strings can be manipulated using built-in functions. These are commonly tested in Edexcel 4CP0 Paper 1 exams.

FunctionDescriptionExampleResult
LENGTH(s)Returns the number of characters in string sLENGTH("Hello")5
SUBSTRING(s, start, len)Returns len characters from s starting at position start (0-indexed)SUBSTRING("Computer", 0, 4)"Comp"
UPPER(s)Converts all characters to upper caseUPPER("hello")"HELLO"
LOWER(s)Converts all characters to lower caseLOWER("HELLO")"hello"

String Concatenation

Strings can be joined together using the & operator (not +):

SET firstName TO "Alice"
SET lastName TO "Smith"
SET fullName TO firstName & " " & lastName
SEND fullName TO DISPLAY # outputs "Alice Smith"

# LENGTH example
SEND LENGTH(fullName) TO DISPLAY # outputs 11

SUBSTRING Example

⚠️ Note: SUBSTRING, UPPER, and LOWER are not listed in the Edexcel 4CP0 Appendix 5 pseudocode reference. Only LENGTH and RANDOM appear there. These functions are included here as additional context — exams may not require them, and you will not be penalised for not knowing them.

SET word TO "Computer"
# Extract "Comp" — start at 0, take 4 characters
SEND SUBSTRING(word, 0, 4) TO DISPLAY # "Comp"

# Extract "ter" — start at 5, take 3 characters
SEND SUBSTRING(word, 5, 3) TO DISPLAY # "ter"

Local Variables

A local variable is declared inside a procedure or function and only exists while that procedure/function is running. It cannot be accessed from outside the subprogram.

PROCEDURE calculateArea()
BEGIN PROCEDURE
SET width TO 5 // local variable — only exists here
SET height TO 10 // local variable
SET area TO width * height
SEND area TO DISPLAY
END PROCEDURE

// width, height and area do NOT exist here

Global Variables

A global variable is declared in the main program and can be accessed from anywhere — including inside procedures and functions. Overusing global variables is considered poor practice as it makes programs harder to debug.

SET highScore TO 0 // global variable — accessible everywhere

PROCEDURE updateScore(newScore)
BEGIN PROCEDURE
IF newScore > highScore THEN
SET highScore TO newScore // accessing global
END IF
END PROCEDURE

Local vs Global Variables

FeatureLocal VariableGlobal Variable
Where declaredInside a procedure/functionIn the main program body
Accessible fromOnly within that procedure/functionAnywhere in the program
LifetimeCreated when subprogram runs; destroyed on exitExists for the whole program
RiskLow — cannot accidentally affect other partsHigher — any part of program can change it
📝 Exam Tip: SUBSTRING is 0-indexed. SUBSTRING("Hello", 1, 3) returns "ell" — starts at position 1 (not 0) and takes 3 characters. Trace through carefully.
⚠️ Common Mistakes
  • Forgetting SUBSTRING is 0-indexed — position 0 is the first character
  • Using + to add numbers stored as strings — "5" + "3" = "53" not 8; cast to integer first
  • Thinking local variables persist after the subprogram ends — they are destroyed
← 2.3b Data Structures Topic 2 · 2.3 Data Types and Structures Next: 2.4a Input, Output and Validation →
🔒
Pro Content
Subscribe to access all 47 Edexcel iGCSE lessons.
£7.99/month
or £59/year