Parameters
What is a Parameter?
Without parameters, a subroutine always does exactly the same thing every time it runs. A parameter solves this by acting as a named placeholder inside the subroutine definition - a variable whose value is supplied each time the subroutine is called. The value that is supplied at the point of calling is called an argument. Parameters and arguments are closely related but refer to different things:
| Term | Where it appears | Example |
|---|---|---|
| Parameter | In the subroutine definition | SUBROUTINE greetUser(name) — name is the parameter |
| Argument | In the subroutine call | greetUser("Alice") — "Alice" is the argument |
When the call is made, the argument value is copied into the parameter variable, and the subroutine runs using that value. This means the same subroutine can produce different results on different calls simply by passing different arguments - making it far more flexible and reusable than a subroutine with no parameters at all.
Pseudocode Structure
In AQA pseudocode, a subroutine with parameters is written as:
SUBROUTINE printScore(playerName, score)
OUTPUT playerName & " scored " & score & " points."
ENDSUBROUTINE
printScore("Alice", 95) ← call: Alice and 95 are copied into the parameters
printScore("Bob", 82) ← call: Bob and 82 are copied in; same subroutine, different data
Parameters in Practice
The three examples below show the progression from a subroutine with no parameters (rigid) through to a subroutine with two parameters (flexible and reusable). Select a tab, then choose a language.
This subroutine has no parameters. It always outputs the same fixed message. To greet a different user, an entirely separate subroutine would be needed - an obvious problem once a program has dozens of users.
# Python -- no parameters: always greets the same person
def greetUser():
print("Hello, Alice!")
print("Welcome to the system.")
greetUser() # always outputs "Hello, Alice!" -- cannot be personalised
' VB.NET -- no parameters: always greets the same person
Sub greetUser()
Console.WriteLine("Hello, Alice!")
Console.WriteLine("Welcome to the system.")
End Sub
greetUser() ' always outputs "Hello, Alice!" -- cannot be personalised
// C# -- no parameters: always greets the same person
static void GreetUser()
{
Console.WriteLine("Hello, Alice!");
Console.WriteLine("Welcome to the system.");
}
GreetUser(); // always outputs "Hello, Alice!" -- cannot be personalised
Adding one parameter called name makes the subroutine flexible. Each call passes a different argument, and that value is used inside the subroutine body. One definition now handles any number of users.
# Python -- one parameter: name receives the argument on each call
def greetUser(name): # name is the parameter
print("Hello, " + name + "!")
print("Welcome to the system.")
greetUser("Alice") # argument "Alice" is copied into name
greetUser("Bob") # argument "Bob" is copied into name
greetUser("Chloe") # same subroutine, different data each time
' VB.NET -- one parameter: name receives the argument on each call
Sub greetUser(ByVal name As String) ' name is the parameter
Console.WriteLine("Hello, " & name & "!")
Console.WriteLine("Welcome to the system.")
End Sub
greetUser("Alice") ' argument "Alice" is copied into name
greetUser("Bob") ' argument "Bob" is copied into name
greetUser("Chloe") ' same subroutine, different data each time
// C# -- one parameter: name receives the argument on each call
static void GreetUser(string name) // name is the parameter
{
Console.WriteLine("Hello, " + name + "!");
Console.WriteLine("Welcome to the system.");
}
GreetUser("Alice"); // argument "Alice" is copied into name
GreetUser("Bob"); // argument "Bob" is copied into name
GreetUser("Chloe"); // same subroutine, different data each time
Subroutines can accept more than one parameter. Each parameter receives its corresponding argument in order, left to right. Here, playerName receives the name string and score receives the integer. Both values are available anywhere inside the subroutine body.
# Python -- two parameters: playerName and score
def printScore(playerName, score): # two parameters listed, separated by a comma
print(playerName + " scored " + str(score) + " points.")
printScore("Alice", 95) # "Alice" -> playerName, 95 -> score
printScore("Bob", 82) # "Bob" -> playerName, 82 -> score
printScore("Chloe", 74) # "Chloe" -> playerName, 74 -> score
' VB.NET -- two parameters: playerName and score
Sub printScore(ByVal playerName As String, ByVal score As Integer)
Console.WriteLine(playerName & " scored " & score & " points.")
End Sub
printScore("Alice", 95) ' "Alice" -> playerName, 95 -> score
printScore("Bob", 82) ' "Bob" -> playerName, 82 -> score
printScore("Chloe", 74) ' "Chloe" -> playerName, 74 -> score
// C# -- two parameters: playerName and score
static void PrintScore(string playerName, int score)
{
Console.WriteLine(playerName + " scored " + score + " points.");
}
PrintScore("Alice", 95); // "Alice" -> playerName, 95 -> score
PrintScore("Bob", 82); // "Bob" -> playerName, 82 -> score
PrintScore("Chloe", 74); // "Chloe" -> playerName, 74 -> score
Key Takeaways
- A parameter is a named variable in the subroutine definition that receives incoming data; an argument is the actual value supplied in the call.
- When a subroutine is called, each argument is copied into its corresponding parameter in left-to-right order.
- Parameters make subroutines flexible: the same subroutine can produce different results on different calls simply by passing different arguments.
- A subroutine can accept more than one parameter; each is listed in the definition, separated by commas.
- Without parameters, a subroutine always does the same thing - every different case would require its own separate subroutine, defeating the purpose of reusability.