The parameters or arguments in programming functions are values that are passed to a function from outside of it and that the function receives and can use and process.
Let’s remember that a function is an independent piece of code that performs a task. To do this, it can receive a series of inputs and have an output.
The parameters are the inputs that the function can receive. They are a very powerful tool that allows functions to be much more reusable (than if they didn’t exist).
In general, the parameters of a function look like this.
function myFunction(parameter1, parameter2, parameter3)
{
// function body
}
Parameters can be of different data types depending on the language we are using, such as numbers, strings, objects, or collections.
Examples of parameters in functions in different languages
Let’s see examples of how to define a function in different languages that receives parameters. For the example let’s create an addition function.
function add(a, b)
Basically, we want a function that takes two numbers as parameters, calculates their sum, and then does something with them (the “something to do” we omit for the example, because what is important now are the parameters).
It’s a very simple function, which probably doesn’t make much sense to define in a real program. But it helps us illustrate the syntax of parameters.
Here is how an example would look in C, C++, C# or Java. In this case, the parameters are placed in parentheses in the function definition, separated by commas. Since it is a strongly typed language, it is necessary to specify the type of each parameter, which in our example is int
.
// Function definition
void add(int a, int b) {
int result = a + b;
// here we would do something with result, like displaying it on screen
}
// call the function
add(3, 5);
The syntax in JavaScript is similar; the parameters are indicated in parentheses in the definition, separated by commas. However, since it is a dynamically typed language, it is not necessary to indicate the types of the parameters.
// Function definition
function add(a, b) {
let result = a + b;
// here we would do something with result, like displaying it on screen
}
// call the function
add(3, 5);
In the case, for example, of TypeScript the syntax is similar to JavaScript. But, TypeScript is typed, so it is again necessary to indicate the types. In this case, they are placed after the variable name, separated by :
.
// Function definition
function add(a: number, b: number) {
let result = a + b;
// here we would do something with result, like displaying it on screen
}
// call the function
add(3, 5);
In the case of Python, we see that it is very similar to the previous ones. Python is a dynamically typed language, so again it is not necessary to indicate the types of the parameters.
# Function definition
def add(a, b):
result = a + b
# here we would do something with result, like displaying it on screen
# call the function
add(3, 5)
To give an example of a slightly more “bizarre” language, here’s how a function would look in SQL. Besides the fact that the syntax is somewhat horrible, basically, the parameters of the function are again indicated in parentheses, specifying the variable name and its type.
CREATE FUNCTION add(@a INT, @b INT)
BEGIN
DECLARE @result INT;
SET @result = @a + @b;
-- here we would do something with result, like displaying it on screen
END;
As we can see, apart from the differences in syntax between languages, and that some require indicating the type of parameters while others do not, the definition and the concept are the same in all languages.
Difference between argument and parameter
The concept of argument and parameter are very related. So much so that in your daily life, you will use both terms almost synonymously.
However, in reality, they are not exactly the same. Let’s see:
- The parameters are the variables defined by the function (in the example
a
andb
) - The arguments are the concrete values that we pass to the function in an invocation (in the example
3
and5
)
That is, parameters are “the slots” provided by the function, and arguments are “what we put in those slots” in each of the invocations.
It’s not that it’s very important. But speaking correctly never hurts 😉
Best practices Tips
It is important to keep in mind that a function should not have an excessive number of parameters. If a function has too many parameters, it can become difficult to understand and maintain.
It is recommended that if a function has more than three or four parameters, it is better to consider passing a grouping of elements as a parameter.
For example, if we imagine a function that prints a document in PDF, and that receives a lot of options
function PrintPDF(document, landscape, papersize, orientation, singleface, ...) {
}
It would probably look cleaner if we wrap all the options in a grouping with all the options
function PrintPDF(document, options) {
}