Language: EN

python-funciones

Functions in Python

Functions are blocks of code that perform a specific task and can be reused in different parts of a program.

Structuring our code into functions helps us make it more organized, so that it is more modular, easier to maintain, and easier to reuse.

Definition of a function

In Python, a function is defined with the keyword def, followed by the function name and parentheses that may contain input parameters. Thus,

def function_name(parameters):
    # Function code block
    # May contain multiple statements
    return result
  • function_name: This is the name we give to our function.
  • parameters: These are the parameters that our function can receive. These are optional and can be used to pass information to the function.
  • return result: The return statement allows us to return a result from the function. It is optional and can return any data type.

Calling a function

Once we have a defined function, it can be called from anywhere in the program (this is also called invoking the function).

To call a function, its name is used followed by parentheses. If the function requires arguments, we will need to put them inside the parentheses.

For example, if we have this function:

def greet():
    print("Hello Luis")

We can invoke it like this:

greet()

In this case, the greet function is called, which will display the message “Hello Luis”.

Parameters and arguments

Parameters are variables that we can pass to our function when invoking it. This allows us to create much more flexible and reusable functions.

For example, let’s imagine we have a function that simply adds two numbers. We can make it receive two parameters and perform the addition.

In this case, a and b are parameters of the sum function (we could have chosen any other name for a and b, we choose the names)

def sum(a, b):
    print(a + b) # Output: 8

sum(3, 5) # we call the function

In the example, we called the function passing 3 and 5 as arguments. We could call it with any other numbers, which allows our function to be reusable.

Parameters are the variables that the function defines. In the example a and b
On the other hand, arguments are the concrete values that we pass to the function in an invocation. In the example 3 and 5
But don’t worry too much about this detail

Return values

Functions in Python can return a value using the keyword return. This value can be assigned to a variable when calling the function.

def giveNumber():
    return 8

result = giveNumber()
print(result) # Output: 8

For example, here the giveNumber() function returns 8.

Basic example

If we combine the above, we can create a very simple function that adds two numbers and returns the result:

def add(a, b):
    result = a + b
    return result

In this example

  • The add function receives two parameters a and b
  • Adds these values
  • Returns the result of the addition

Now we can call this function and use it in our program:

sum_result = add(5, 3)
print("The result of the addition is:", sum_result)  # Output: The result of the addition is: 8