- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why would you use the return statement in Python?
The return statement in Python is used to return a value or a set of values from a function. When a return statement is encountered in a function, the function execution is stopped and the value specified in the return statement is returned to the caller.
The main purpose of the return statement is to pass data from a function back to the caller. The caller can then use this data for further processing.
Why use the return statement?
The return statement is useful in many situations. Some common use cases include −
Passing data back to the caller: As mentioned earlier, the main purpose of the return statement is to pass data from a function back to the caller. This is useful when the function performs some computation or manipulation on the input data and needs to pass the result back to the caller.
Exiting a function early: In some cases, a function may need to exit early based on certain conditions. This can be achieved using the return statement. When the return statement is encountered, the function execution is stopped and control is returned to the caller.
Returning multiple values: In Python, it is possible to return multiple values from a function using the return statement. This is useful when the function needs to return multiple values that are related to each other.
Here are some examples of using the return statement in Python −
Example 1: Returning a value from a function
In this example, the add_numbers function takes two arguments and returns their sum using the return statement. The returned value is then stored in the result variable, which is then printed to the console.
def add_numbers(num1, num2): sum = num1 + num2 return sum result = add_numbers(5, 10) print(result)
Output
15
Example 2: Exiting a function early
In this example, the check_number function takes a number as input and returns a string indicating whether the number is negative, zero, or positive. If the number is negative, the function exits early using the return statement and returns the string "Number is negative". The same approach is used for zero and positive numbers.
def check_number(num): if num < 0: return "Number is negative" elif num == 0: return "Number is zero" else: return "Number is positive" result = check_number(-5) print(result)
Output
Number is negative
Example 3: Returning multiple values from a function
In this example, we define a function called get_student_info that takes three arguments: name, age, and grade. The function returns these three values using a tuple. We then call the get_student_info function with arguments "John", 15, and "9th" and store the returned tuple in a variable called info. Finally, we print the value of info, which is ('John', 15, '9th').
def get_student_info(name, age, grade): return name, age, grade info = get_student_info("John", 15, "9th") print(info)
Output
('John', 15, '9th')
In this example, we are returning multiple values from a function using a tuple. This is useful when we need to return more than one value from a function. In this case, we return the name, age, and grade of a student as a tuple. We can then unpack this tuple into separate variables if we need to use these values later in our code. For example −
In this example, we unpack the tuple returned by the get_student_info function into separate variables called name, age, and grade. We can then use these variables as needed in our code.
Example 3A:
def get_student_info(name, age, grade): return name, age, grade name, age, grade = get_student_info("John", 15, "9th") print(name) print(age) print(grade)
Output
John 15 9th
Example 4: Using the return statement to return a value from a function:
In this example, we define a function called add_numbers that takes two arguments a and b. The function returns the sum of a and b using the return statement. We then call the add_numbers function with arguments 5 and 7 and store the result in a variable called result. Finally, we print the value of result, which is 12.
def add_numbers(a, b): return a + b result = add_numbers(5, 7) print(result)
Output
12
Example5: Used to exit a function early
The return statement can also be used to exit a function early. Here's an example −
In this example, we define a function called check_password that takes one argument password. The function checks if the password is at least 8 characters long and contains both letters and digits. If any of these conditions are not met, the function returns False using the return statement. If all the conditions are met, the function returns True. We then call the check_password function with three different passwords and store the results in variables called result1,result2, and result3. Finally, we print the values of these variables to verify that the function is working correctly.
def check_password(password): if len(password) < 8: return False if password.isalpha(): return False if password.isnumeric(): return False return True result1 = check_password("password") result2 = check_password("12345678") result3 = check_password("password123") print(result1) print(result2) print(result3)
Output
False False True