In the previous lessons, you learned how to create Python functions and pass data to them using parameters and arguments. While many functions simply display output using the print() function, professional Python programs usually need functions to send results back to the caller for further processing.
Python provides the return statement for this purpose.
Another important concept when working with functions is variable scope. Variables created inside a function behave differently from variables created outside a function. Understanding where variables can be accessed helps you avoid programming errors and write cleaner, more maintainable code.
In this lesson, you will learn how the return statement works, the difference between print() and return, local and global variables, and how Python manages variable scope.
After completing this lesson, you will be able to:
return statement.print() and return.return Statement?The return statement is used to send a value from a function back to the place where the function was called.
When Python encounters a return statement, the function immediately stops executing and returns the specified value.
def function_name():
return value
def square(number):
return number * number
result = square(5)
print(result)
Output
25
The value 25 is returned from the function and stored in the variable result.
return Statement?Without the return statement, a function can only display output but cannot send results back for reuse.
The return statement allows the returned value to be stored, modified, or used in other calculations.
def addition(a, b):
return a + b
total = addition(15, 25)
print(total * 2)
Output
80
Because the function returns a value, it can be used in further calculations.
print() and returnprint() |
return |
|---|---|
| Displays output on the screen. | Sends a value back to the caller. |
| Cannot be reused directly. | Returned value can be reused. |
| Does not end the function. | Immediately terminates the function. |
| Mainly used for displaying information. | Mainly used for calculations and reusable results. |
print()def add(a, b):
print(a + b)
result = add(10, 20)
print(result)
Output
30
None
The function prints the result but does not return anything, so result becomes None.
returndef add(a, b):
return a + b
result = add(10, 20)
print(result)
Output
30
The returned value is stored and can be reused.
A function can return one value.
def cube(number):
return number ** 3
print(cube(4))
Output
64
Python allows multiple values to be returned using commas.
def calculate(a, b):
return a + b, a - b, a * b
addition, subtraction, multiplication = calculate(12, 6)
print(addition)
print(subtraction)
print(multiplication)
Output
18
6
72
Python automatically packs the returned values into a tuple.
Variable scope refers to the region of a program where a variable can be accessed.
Python mainly uses two types of variable scope:
Understanding scope helps prevent naming conflicts and unexpected behavior.
A local variable is created inside a function and can only be accessed within that function.
def student():
name = "Rahul"
print(name)
student()
Output
Rahul
The variable name exists only while the function executes.
def student():
name = "Rahul"
student()
print(name)
This produces a NameError because name is outside its scope.
A global variable is created outside every function.
Global variables can be accessed both inside and outside functions.
course = "Python"
def display():
print(course)
display()
print(course)
Output
Python
Python
The variable course is available throughout the program.
Consider the following program.
def multiply(a, b):
return a * b
answer = multiply(8, 5)
print(answer)
Execution Steps
8 and 5.a and b.return statement sends the result back.answer.Output
40
print() Instead of returnIf a result must be reused, return it instead of printing it.
return StatementA function without a return statement returns None by default.
Local variables cannot be used outside the function in which they are created.
This may lead to confusion and unintended behavior.
return to ExecuteOnce Python executes a return statement, the function ends immediately.
In this section, you learned how the Python return statement sends values back to the caller and why it is preferred over print() when results need to be reused. You explored returning single and multiple values, learned the concept of variable scope, and differentiated between local and global variables. You also followed the execution flow of a function with a return statement and reviewed common beginner mistakes. In the next section, you will explore local scope, global scope, the global keyword, variable shadowing, nested function scope, an introduction to the LEGB rule, and best practices for managing variable scope.
In the previous section, you learned how the return statement sends values back to the caller and how Python uses local and global variables. In this section, you will explore variable scope in greater detail, including the global keyword, variable shadowing, nested functions, the LEGB rule, and returning values from nested functions.
A variable created inside a function belongs to the local scope. It exists only while the function is executing.
def display():
message = "Welcome to Python"
print(message)
display()
Output
Welcome to Python
The variable message is accessible only inside the display() function.
def display():
message = "Python"
display()
print(message)
Output
NameError: name 'message' is not defined
The variable is destroyed after the function finishes executing.
A variable declared outside every function belongs to the global scope.
Global variables can be accessed from anywhere in the program unless a local variable with the same name hides them.
course = "Python Programming"
def show():
print(course)
show()
print(course)
Output
Python Programming
Python Programming
The global variable course is available both inside and outside the function.
global KeywordNormally, assigning a value to a variable inside a function creates a new local variable.
If you want to modify a global variable inside a function, use the global keyword.
globalcount = 10
def update():
count = 20
update()
print(count)
Output
10
The assignment creates a new local variable instead of modifying the global variable.
globalcount = 10
def update():
global count
count = 20
update()
print(count)
Output
20
The global keyword tells Python to modify the global variable instead of creating a local one.
Variable shadowing occurs when a local variable has the same name as a global variable.
Inside the function, the local variable hides (or shadows) the global variable.
name = "Rahul"
def display():
name = "Neha"
print(name)
display()
print(name)
Output
Neha
Rahul
The local variable is used only inside the function, while the global variable remains unchanged.
Python allows one function to be defined inside another function.
These are called nested functions.
def outer():
message = "Python"
def inner():
print(message)
inner()
outer()
Output
Python
The inner function can access variables from the enclosing (outer) function.
Python follows the LEGB rule to search for variables.
| Scope | Description |
|---|---|
| Local (L) | Variables inside the current function. |
| Enclosing (E) | Variables in the outer function of a nested function. |
| Global (G) | Variables defined outside all functions. |
| Built-in (B) | Names provided by Python, such as print() and len(). |
Python searches these scopes in the following order:
Local → Enclosing → Global → Built-in
A nested function can return a value to its enclosing function.
def outer():
def inner():
return "Hello Python"
return inner()
print(outer())
Output
Hello Python
The value returned by the inner function is returned again by the outer function.
Consider the following program.
value = 100
def display():
value = 50
print(value)
display()
print(value)
Execution Steps
value is assigned the value 100.display() is called.value is created with the value 50.Output
50
100
globalPython creates a local variable unless the global keyword is used.
Local variables exist only while their function is executing.
Always remember where a variable is declared.
Excessive global variables make programs harder to maintain and debug.
Python always searches for variable names in the order: Local → Enclosing → Global → Built-in.
In this section, you learned how Python manages variable scope through local and global variables, explored the global keyword, understood variable shadowing, worked with nested functions, and received an introduction to the LEGB rule. You also learned how values can be returned from nested functions, followed the execution flow of scoped variables, and reviewed common beginner mistakes. In the next section, you will apply these concepts through practical programs such as calculator functions, student grade systems, banking balance management, temperature conversion, global counters, and nested function examples.
The return statement and variable scope are used in almost every Python application. Whether you are performing calculations, managing account balances, converting units, or processing student results, functions often return values that are used later in the program.
In this section, you will build practical examples using the return statement, local variables, global variables, and nested functions. These examples demonstrate how to write clean, reusable, and well-organized Python programs.
This function returns the sum of two numbers.
def addition(a, b):
return a + b
result = addition(35, 15)
print("Sum =", result)
Output
Sum = 50
The returned value is stored in the variable result and can be used later.
This function returns a grade based on marks.
def calculate_grade(marks):
if marks >= 90:
return "A"
elif marks >= 75:
return "B"
elif marks >= 60:
return "C"
else:
return "D"
grade = calculate_grade(82)
print("Grade:", grade)
Output
Grade: B
This function returns the area of a circle.
def area_of_circle(radius):
pi = 3.14159
return pi * radius * radius
print(area_of_circle(7))
Output
153.93791
The variable pi is local to the function.
This function updates an account balance after a deposit.
def deposit(balance, amount):
return balance + amount
balance = 5000
balance = deposit(balance, 1500)
print("Current Balance:", balance)
Output
Current Balance: 6500
The function returns the updated balance instead of modifying the variable directly.
This function converts Celsius to Fahrenheit.
def celsius_to_fahrenheit(celsius):
return (celsius * 9 / 5) + 32
temperature = celsius_to_fahrenheit(30)
print(temperature)
Output
86.0
This example demonstrates how the global keyword modifies a global variable.
counter = 0
def increment():
global counter
counter += 1
increment()
increment()
increment()
print(counter)
Output
3
The global variable counter is updated each time the function is called.
This example demonstrates that local variables exist only inside the function.
def welcome():
message = "Welcome to Python"
return message
print(welcome())
Output
Welcome to Python
The variable message cannot be accessed outside the function.
Functions can be defined inside other functions.
def outer():
def inner():
return "Python Programming"
return inner()
print(outer())
Output
Python Programming
The inner function returns a value to the outer function, which then returns it to the caller.
A function can return multiple values.
def calculate(a, b):
return a + b, a - b, a * b
addition, subtraction, multiplication = calculate(12, 8)
print(addition)
print(subtraction)
print(multiplication)
Output
20
4
96
The result returned by one function can be passed to another function.
def square(number):
return number * number
def display(value):
print(value)
result = square(9)
display(result)
Output
81
This demonstrates how functions can work together to build larger programs.
Consider the following program.
def cube(number):
return number ** 3
result = cube(4)
print(result)
Execution Steps
cube() is defined.4.4 is assigned to the parameter number.return statement sends the result back.result.Output
64
Functions become more reusable when they return values instead of displaying them directly.
Use local variables unless a global variable is genuinely required.
Too many global variables make programs harder to understand and maintain.
Names such as balance, grade, and temperature improve readability.
Each function should perform one clearly defined task and return the required result.
print() instead of return when a value needs to be reused.global keyword.return statement to execute.In this section, you applied the Python return statement and variable scope concepts through practical programs such as calculator functions, student grade evaluation, circle area calculation, banking balance management, temperature conversion, global counters, nested functions, and multiple return values. You also learned best practices for writing reusable functions, reviewed common beginner mistakes, and followed the execution flow of returned values. In the final section, you will review the complete lesson through a summary, key takeaways, frequently asked questions, coding exercises, interview questions, a mini project, and a preview of the next lesson on Python Lambda Functions.
In this lesson, you learned how the Python return statement allows functions to send values back to the caller, making functions reusable and suitable for building larger programs. You explored the difference between print() and return, learned how to return single and multiple values, and understood why returned values are preferred in professional Python programming.
You also studied variable scope, which determines where variables can be accessed within a program. You learned the difference between local and global variables, how the global keyword works, how variable shadowing occurs, and how nested functions access variables using Python’s LEGB rule.
Through practical examples such as calculator functions, banking systems, temperature converters, student grading programs, and nested functions, you learned how to write cleaner, modular, and maintainable Python code.
Understanding the return statement and variable scope is essential before learning advanced Python concepts such as lambda functions, recursion, decorators, and object-oriented programming.
return statement sends values back to the caller.return statement.print() displays output, while return makes values reusable.global keyword allows a function to modify a global variable.return statement?The return statement sends a value from a function back to the caller and ends the function immediately.
print() and return?print() displays information on the screen, while return provides a value that can be stored and reused.
Yes. Python allows functions to return multiple values separated by commas.
A local variable is created inside a function and can only be accessed within that function.
A global variable is defined outside all functions and is accessible throughout the program.
global keyword used?The global keyword allows a function to modify a global variable instead of creating a new local variable.
Variable shadowing occurs when a local variable has the same name as a global variable, temporarily hiding the global variable inside the function.
LEGB stands for Local, Enclosing, Global, and Built-in, which is the order Python uses to search for variable names.
print() and return.global keyword.return statement in Python?print() and return?global keyword?Create a Python program that uses functions and variable scope to simulate a simple banking system.
Your program should:
balance.deposit(amount) that updates the balance.withdraw(amount) that deducts money if sufficient balance is available.check_balance() that returns the current balance.return statement wherever appropriate.Current Balance : 10000
Deposit : 2500
Current Balance : 12500
Withdraw : 3000
Current Balance : 9500
Congratulations! You have successfully learned how Python functions return values and how variable scope controls the visibility of variables. You now understand local variables, global variables, the global keyword, nested functions, variable shadowing, and the LEGB rule. These concepts are fundamental for writing modular, reusable, and maintainable Python applications.
In the next lesson, you will learn Python Lambda Functions: Complete Guide for Beginners. You will discover anonymous functions, the lambda keyword, lambda expressions, using lambda functions with map(), filter(), and sorted(), along with practical examples and real-world applications.