In today’s video, we are going to learn about Functions, which is an important topic in O Level Computer Science 2210. Functions help us organize our programs, reduce repetition, and make code easier to understand. By the end of this video, you will clearly understand what a function is, why we use it, and how it works.
What is a Function?
A function is a named block of code that performs a specific task. Instead of writing the same instructions again and again, we place them inside a function and call the function whenever needed.
For example, if a program needs to calculate an average many times, we can create a function that calculates the average once and reuse it.
In simple words, a function is like a machine:
-
You give it input
-
It processes the input
-
It gives an output
Why Do We Use Functions?
Functions are used because they:
-
Reduce code repetition
-
Make programs easier to read and understand
-
Make programs easier to test and debug
-
Allow programmers to divide a big problem into smaller manageable parts
In exams, you should remember that functions improve program structure and efficiency.
Parts of a Function
A function usually has three main parts:
-
Function Definition
This is where the function is written and given a name. -
Parameters
These are the values passed into the function. Parameters are optional; some functions may not need them. -
Return Value
A function usually sends a value back to the main program using the keyword RETURN.
Not all functions must return a value, but in 2210 syllabus, functions are mainly used to return a result.
[Example Using Pseudocode – 1 minute 20 seconds]
Let us understand functions using a simple example in pseudocode, which is required in O Level exams.
Suppose we want a function that adds two numbers.
The function definition would be:
-
The function is named
AddNumbers -
It takes two parameters:
Num1andNum2 -
It calculates the sum
-
It returns the result
In the main program, we call the function and store the returned value in a variable.
Explain to students verbally that:
-
The main program sends values to the function
-
The function processes them
-
The result is returned to the main program
This separation makes the program neat and professional.
Function Call
Calling a function means using the function in the main program.
When a function is called:
-
Control moves from the main program to the function
-
The function executes its statements
-
The function returns a value
-
Control comes back to the main program
This flow is important for understanding exam questions.
[Functions vs Main Program – 30 seconds]
Always remember:
-
The main program controls the flow
-
Functions only run when they are called
-
A function can be called multiple times in the same program
Exam Tips
For exams:
-
Use clear function names
-
Match parameters and return values correctly
-
Write functions before or after the main program (both are acceptable)
-
Use RETURN correctly in pseudocode
That brings us to the end of today’s video on Functions for O Level Computer Science 2210. Practice writing small functions and calling them correctly, as this topic is frequently tested in exams.
