This resource is designed as a quick reference or revision guide. It has not been endorsed by any exam boards. If you spot any mistakes, please let me know and I'll fix them asap.
When writing a program it's often really useful to break down the problem you're trying to solve into smaller parts. This process is called decomposition and it helps you to solve each small part of the problem separately which can lead to more reliable and efficient code.
Choosing how to break up your program into smaller parts is one of the most important design decisions.
Anything your program has to calculate can be turned into a function. Any task that your program might have to repeat can be turned into a procedure.
Choose names that describe what the function or procedure does. e.g. MyFunction1() is not nearly as helpful as CalculateAverage()
You need to define a function / procedure once by writing out the lines of code that the function / procedure will follow when it runs. After you've defined it you can call it (make it run) as many times as you like just using its name.
Parameters work as variables that only exist inside the function or procedure. You can pass data to the function / procedure whenever you call it through one of these parameters.
A return value is the result of a calculation that gets passed back to the part of the code that called the function so that it can be saved or used later in the program.
A procedure is a section of code that has a name that you can re-use to do something useful.
A procedure definition is the code that tells the computer what to do whenever that procedure runs.
The procedure wont actually do anything until you call it.
This example defines a procedure that shows a random word then calls it twice.
A procedure is a section of code that has a name, that you can re-use to do something useful.
Parameters are variables that allow you to send data to the procedure to customise what it does or how it works.
A procedure definition is the code that tells the computer what to do whenever that procedure runs. The parameters are the variables named in brackets after the procedure name.
The procedure wont actually do anything until you call it.
This example defines a procedure which will display a random number. It has two parameters which allow you to set the minimum and maximum number the random number will be between.
This procedure is called to show a random number between 10 and 20 and then called again to choose a random number between 50 and 100.
A function is like a procedure except that it always returns a value.
This return value is calculated when the function is called and can be saved into a variable or used later in the program.
Both functions and procedures are sections of code that have been given a name, which can be re-used to do something useful.
This example defines a function which chooses a random word. This function is called twice with the return value being stored into separate variables.
A function is like a procedure except that it always returns a value.
Parameters are variables that allow you to send data to the function to customise what it does or how it works.
The parameters are the variables named in brackets after the function name.
This return value is calculated when the function is called and can be saved into a variable or used later in the program.
Both functions and procedures are sections of code that have been given a name, which can be re-used to do something useful.
This example defines a function which will generate and return a random number. It has two parameters which allow you to set the minimum and maximum number the random number will be between.
This function is called to choose a random number between 10 and 20 and then called again to choose a random number between 50 and 100. Both numbers are returned and saved into separate variables.
Loading...