coding180 icon
Coding180
Python Functions: Using Return Statements to Get Desired Outputs

Python Functions: Using Return Statements to Get Desired Outputs

Home > Tutorials > Python for beginners > Functions


Python is one of the most popular programming languages today, thanks to its simplicity and versatility. One of the essential features of Python is its ability to define and use functions. Functions are reusable blocks of code that perform specific tasks. They take inputs, process them, and produce outputs. In this article, we will focus on the use of return statements in Python functions to get desired outputs.

Returning a Value from a Function

A return statement is used to send a value or values back to the caller of a function. The syntax for defining a simple function that returns a value is as follows:

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

In this example, the add_numbers function takes two inputs, a and b, adds them together, and then returns the result of the addition. To call this function, you would simply pass in two numbers as arguments, like this:

result = add_numbers(2, 3)
print(result) # Output: 5

Here, we passed in 2 and 3 as arguments to the add_numbers function, which returned 5, and we stored the result in the result variable. We then printed the value of result, which was 5.

Multiple Return Statements in a Function

Functions can have multiple return statements, which allow you to return different values based on certain conditions. Here's an example:

def check_number(num):
    if num > 0:
        return "Positive"
    elif num == 0:
        return "Zero"
    else:
        return "Negative"

In this example, the check_number function takes a single input, num, and returns a string based on whether num is positive, zero, or negative. To call this function, you would pass in a number as an argument, like this:

result = check_number(-5)
print(result) # Output: Negative

Here, we passed in -5 as an argument to the check_number function, which returned "Negative", and we stored the result in the result variable. We then printed the value of result, which was "Negative".

Using the Return Value of a Function

Once a function has returned a value, you can use that value in various ways. Here are a few examples:

Assigning the return value to a variable

def square(num):
    return num**2

result = square(5)
print(result) # Output: 25

In this example, we defined a function called square that takes a single input, num, and returns the square of num. We then called the function with an argument of 5, which returned 25. We stored the returned value in the result variable and printed it.

Using the return value in an expression

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

result = add_numbers(2, 3) * 2
print(result) # Output: 10

In this example, we defined a function called add_numbers that takes two inputs, a and b, adds them together, and returns the result of the addition. We then called the function with arguments of 2 and 3, which returned 5. We multiplied the returned value by 2 and stored it in the result variable, which we then printed.

Using the return value as an argument to another function

def square(num):
    return num**2

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

result = add_numbers(square(2), square(3))
print(result) # Output: 13

In this example, we defined two functions called square and add_numbers. The square function takes a single input, num, and returns the square of num, while the add_numbers function takes two inputs, a and b, adds them together, and returns the result of the addition. We then called the add_numbers function with arguments of the square of 2 and the square of 3, which returned 13. We stored the returned value in the result variable, which we then printed.

Conclusion

In this article, we've covered the basics of using return statements in Python functions to get desired 

outputs. We've learned how to return a value from a function, use multiple return statements in a function, and use the return value of a function in various ways.

Return statements are an essential part of Python functions and programming in general. They allow us to encapsulate complex logic into reusable blocks of code that can be easily called from other parts of our program. As you continue to learn Python, you will encounter more complex uses of return statements, such as returning lists, dictionaries, and even objects.

We hope that this article has helped you understand the basics of using return statements in Python functions. Keep practicing and experimenting with different types of functions and return statements, and soon you'll be writing your own Python programs like a pro!


user

Robort Gabriel

Lagos, Nigeria

Freelance Web Developer, Native Android Developer, and Coding Tutor.

Skills
  • UI / UX
  • JavaScript
  • Python
  • PHP
  • Kotlin
  • Java
  • Bootrap
  • Android
  • Laravel