Language: EN

programacion-parametros-funcion

What are function parameters

Parameters or arguments in programming functions are values that are passed to a function to be processed.

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.

programacion-funcion

The parameters are the inputs that the function can receive. They are a very powerful tool that allows us to reuse much more than if we didn’t have them.

In general, the parameters of a function have the following appearance.

function myFunction(parameter1, parameter2, parameter3) 
{
   // function body
}

The 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, we will create a sum function.

function sum(a, b) 

Basically we want a function that receives two numbers as parameters, calculates their sum, and then does something with them (we omit the “something to do” for the example, because the important thing now is the parameters).

It is a very simple function, which probably doesn’t make sense to define in a real program. But it is useful to illustrate the syntax of the parameters.

This is how an example would be done in C, C++, C# or Java. In this case, the parameters are placed in parentheses in the function definition separated by commas. Being a strongly typed language, it is necessary to specify the type of each parameter, which in our example is int.

// Function definition
void sum(int a, int b) {
  int result = a + b;
  // here we would do something with the result, like displaying it on the screen
}

// calling the function
sum(3, 5);

The syntax in JavaScript is similar, the parameters are indicated in parentheses in the definition, separated by commas. However, being a dynamically typed language, it is not necessary to indicate the types of the parameters.

// Function definition
function sum(a, b) {
  let result = a + b;
  // here we would do something with the result, like displaying it on the screen
}

// calling the function
sum(3, 5);

In the case, for example, of TypeScript the syntax is similar to JavaScript. But, TypeScript is typed, so it is necessary to indicate the types again. In this case, they are placed after the variable name, separated by : .

// Function definition
function sum(a: number, b: number) {
  let result = a + b;
  // here we would do something with the result, like displaying it on the screen
}

// calling the function
sum(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 it is not necessary to indicate the types of the parameters again.

# Function definition
def sum(a, b):
  result = a + b
  # here we would do something with the result, like displaying it on the screen

# calling the function
sum(3, 5)

As an example of a somewhat “bizarre” language, this is how a function in SQL would look. Besides the fact that the syntax is somewhat horrible, basically, the function parameters are indicated again in parentheses, indicating the variable name and its type.

CREATE FUNCTION sum(@a INT, @b INT)
BEGIN
    DECLARE @result INT;
    SET @result = @a + @b;
	' here we would do something with the result, like displaying it on the screen  
END;

As we can see, apart from the differences in syntax between languages, and that some require indicating the type of the parameters and others do not, the definition and concept are the same in all languages.

Best practices and cleanliness tips Tips

It is important to consider that a function should not have an excessive number of parameters. If a function has too many parameters, it can be 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 is probably cleaner if we wrap all the options in a grouping with all the options

function PrintPDF(document, options) {
}