Categories: All Variables and Constants  String manipulation  Builtin functions  Input and output  Arrays  Conditional logic  Repetition / Iteration  Functions / Procedures 

Show:

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.

Functions / Procedures

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.

Top tips:

  • Functions and procedures both have names, which make your code easier to understand.
    Choose names that describe what the function or procedure does. e.g. MyFunction1() is not nearly as helpful as CalculateAverage()
  • Both functions and procedures are sections of code that can be repeated.
    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.
  • Both functions and procedures can have parameters which can customise what they do and how they work.
    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.
  • The only difference between functions and procedures is that functions return a value but procedures don't.
    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.
Skill
Description
VB.NET
Python
C#
Create and use a procedure
   

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.

import random # define show_random_word procedure def show_random_word(): words = ["wibble", "wobble", "plop"] print(random.choice(words)) # call show_random_words twice show_random_word() show_random_word()
// define showRandomWord procedure void showRandomWord() { Random r = new Random(); string[] words = {"wibble", "wobble", "plop"}; int position = r.Next(words.Length); Console.WriteLine(words[position]); } // call show_random_words twice showRandomWord(); showRandomWord();
Create and use a procedure with parameters
   

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.

import random # define the procedure def show_random_between(min, max): print(random.randint(min, max)) # call the procedure show_random_between(10, 20) show_random_between(50, 100)
// define the procedure void showRandomBetween(int min, int max) { Random r = new Random(); int number = r.Next(min, max); Console.WriteLine(number); } // call the procedure showRandomBetween(10, 20); showRandomBetween(50, 100);
Create and use a function
   

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.

import random # define choose_random_word function def choose_random_word(): words = ["wibble", "wobble", "plop"] return random.choice(words) # call choose_random_words twice first_word = choose_random_word() second_word = choose_random_word()
// define chooseRandomWord function string chooseRandomWord() { Random r = new Random(); string[] words = {"wibble", "wobble", "plop"}; int position = r.Next(words.Length); return words[position]; } // call choose_random_words twice firstWord = chooseRandomWord() secondWord = chooseRandomWord()
Create and use a function with parameters
   

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.

import random # define the function def choose_random_between(min, max): print(random.randint(min, max)) # call the function first_number = choose_random_between(10, 20) second_number = choose_random_between(50, 100)
// define the function int showRandomBetween(int min, int max) { Random r = new Random(); int number = r.Next(min, max); return number; } // call the function int firstNumber = showRandomBetween(10, 20); int secondNumber = showRandomBetween(50, 100);

Loading...