6.5. Functions in Python: Returning Values

Página 31

In the world of programming, functions play a crucial role in structuring and organizing code. In Python, a versatile and widely used programming language, functions are defined using the "def" keyword followed by the function name and parentheses (). Inside these parentheses, you can define any parameters that the function should accept. Finally, you end the line with a colon (:), which signals the beginning of the block of code that makes up the body of the function.

An important aspect of functions in Python is returning values. This is done using the "return" keyword. When the Python interpreter encounters the "return" keyword in a function, it immediately exits the function and passes the value after the "return" back to the caller. For example:

def sum(a, b):
    return a + b

result = sum(3, 4)
print(result) # This will print 7

In this example, the "sum" function accepts two parameters, "a" and "b", and returns their sum. When we call the function and pass 3 and 4 as arguments, the function returns 7, which is then assigned to the "result" variable.

A function can return any type of data in Python, be it a number, a string, a list, a dictionary, or even another function. For example, the following function returns a list of numbers:

def generate_list(n):
    return list(range(n))

print(genera_lista(5)) # This will print [0, 1, 2, 3, 4]

Also, a function in Python can return multiple values. This is done by returning the values ​​as a tuple, which is an unchanging, ordered collection of elements. Here is an example:

def min_max(list):
    return min(list), max(list)

smallest, largest = min_max([3, 1, 4, 1, 5, 9])
print(minor) # This will print 1
print(biggest) # This will print 9

In this example, the "min_max" function accepts a list of numbers and returns a tuple containing the smallest and largest number in the list. When we call the function, we can unpack the tuple into two variables, "smallest" and "largest".

It is important to note that a Python function that does not have an explicit return statement will return None, which is a special Python value that represents the absence of a value. For example:

def print_message(message):
    print(message)

result = print_message("Hello world!")
print(result) # This will print None

In this example, the "print_message" function does not have a return statement, so it returns None by default.

In summary, returning values ​​is an essential part of functions in Python. It allows you to extract results from a function and use them in other parts of your code. Furthermore, by combining return values ​​with other Python features such as lists, dictionaries, and tuples, you can create powerful and flexible functions that can handle a wide variety of programming tasks.

Now answer the exercise about the content:

What is the role of the "return" keyword in a Python function?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

326.6. Functions in Python: Variable scope

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text