What is the difference between an argument and a parameter?
  John Mwaniki /   09 Dec 2021

What is the difference between an argument and a parameter?

Most beginner programmers often use the terms argument and parameter interchangeably to mean the same thing in computer programming.

But are they the same?

Here in this article, we dig deeper into these terms to find what they are, what they are used for, and how they differ from each other.

When programming, you most definitely will need to use functions in your code unless what you are writing are extremely simple programs.

A function is a named group of reusable code that performs a specific task, which can be called anywhere in your program.

It is also referred to as a subroutine or procedure in some programming languages, and methods in Object-Oriented Programming (OOP).

Functions usually take in data, process it, and return a result.

A function makes the code reusable and eliminates the need for writing the same code again and again in a program. Other advantages of using functions include subdividing the code cleanly into segments, encapsulation of tasks, easier testing and debugging, portability and shareability in other projects among others.

The code in the function does not get executed until the function is called.

Function Examples

Functions work the same irrespective of the programming languages. In these examples, we will use PHP.

<?php
//Example 1
function helloworld(){
   echo "Hello World!";
}
helloworld();
//Output: Hello World!


//Example 2
function greetings($name){
   $hour = date("H");
   if($hour >= 5 && $hour < 12){
    $greeting = "Good morning $name";
   }
   else if($hour >= 12 && $hour < 17){
    $greeting = "Good afternoon $name";
   }
   else if($hour >= 17 && $hour < 24){
    $greeting = "Good evening $name";
   }
   else{
    "Hello $name";
   }
   return $greeting;
}
echo greetings("John");
/*Output: Depends on the time of the day
eg: "Good morning John" if the code is executed in the morning
*/

//Example 3
function multiply($num1, $num2){
    $num3 = $num1 * $num2;
    return $num3;
}
$num1 = 113;
echo multiply($num1, 84);
//Output: 9492

From our examples above, we have created 3 functions by writing helloworld(), greetings($name), and multiply($num1, $num2) preceded by the keyword function and followed by some blocks of code enclosed within curly braces { }.

We then have called these functions by writing helloworld(), greetings("John"), and multiply($num1, 84) respectively. Calling a function simply means telling the computer to execute the code within that function.

The function in example 1 is different from the rest two, in that we put nothing inside the brackets () when creating or calling it. It can be referred to as a function without parameters. It will always give the same output without any customization when called.

In examples 2 and 3, we put some items within the brackets both when creating and calling them. These types of functions can be termed parameterized functions. The items within their brackets, either when creating/defining or calling them are what are called the parameters or arguments.

Arguments are the data we pass to a function at the time of calling it. These can be the actual values, variables, or expressions. For instance in our example 2, we are passing the argument "John". In example 3 we are passing the arguments $num1 (a variable whose value is 113) and 84 (a value) respectively.

Arguments are like the ingredients or raw materials which are taken in(when calling the function) and processed (inside the function) to give out an output/result.

Parameters, on the other hand, are the variables defined at the time of function definition (creation). The parameters act as the placeholders where values are initialized. For instance in example 2, $name is the parameter of the function greetings(). It acts as the placeholder of whichever name will be passed (as an argument) at the point of calling the function. In this case, it acts as the placeholder for the name "John".

While an argument can be anything such as a value, variable, pointer, an array, an expression, etc, a parameter is always a variable.

Let's summarize the differences between the parameters and arguments in a table.

Differences between parameters and arguments

Parameters Arguments
They are defined at the time of function definition. They are passed at the time of calling a function.
They are always variables. They can be values, variables, expressions, arrays, etc
They act as the placeholders where values are initialized. They are local variables that are assigned the values of the arguments when the function is called. They are the actual values to be processed by the function. They are assigned to the parameters when the function is called.
They are also referred to as formal parameters. They are referred to as actual parameters.