Python Functions Tutorial for Beginners | Functions, Parameters & Return Values
As programs become larger and more complex, writing all the code in one place becomes difficult to manage. Functions help organize code into reusable blocks that perform specific tasks, making programs easier to read, maintain, and debug.
Almost every modern software application—from banking systems and e-commerce websites to Artificial Intelligence applications—uses functions extensively. Instead of writing the same code repeatedly, developers create functions that can be called whenever needed.
In this lesson, you’ll learn how to define functions, pass parameters, use arguments, return values, understand variable scope, explore recursive functions, and write modular Python programs using industry best practices.
By the end of this lesson, you’ll be able to create reusable functions that simplify your programs and improve code quality.
Learning Objectives
After completing this lesson, you will be able to:
- Understand the purpose of functions.
- Create user-defined functions.
- Call functions correctly.
- Work with parameters and arguments.
- Return values from functions.
- Understand local and global variables.
- Create recursive functions.
- Use lambda functions (basic introduction).
- Apply functions in real-world applications.
Topics Covered
- Introduction to Functions
- Creating Functions
- Calling Functions
- Parameters
- Arguments
- Default Parameters
- Keyword Arguments
- Return Statement
- Variable Scope
- Recursive Functions
- Lambda Functions (Introduction)
- Best Practices
Detailed Lesson Content
Introduction to Functions
Imagine you’re developing an Online Shopping Website.
Whenever a customer places an order, the system performs several common tasks:
- Calculate the total bill
- Apply discounts
- Calculate GST
- Generate an invoice
- Send an email notification
Instead of writing the same code every time an order is placed, developers create reusable functions.
A function is a reusable block of code designed to perform a specific task.
Functions make programs:
- Easier to read
- Easier to maintain
- Less repetitive
- More efficient
- More modular
Almost every programming language supports functions because they encourage clean and reusable code.
Why Do We Need Functions?
Without functions:
- Code becomes repetitive.
- Programs become difficult to maintain.
- Bugs are harder to fix.
- Development takes more time.
With functions:
- Write once, use multiple times.
- Reduce duplicate code.
- Improve readability.
- Simplify debugging.
- Make applications modular.
Creating a Function
Python uses the def keyword to define a function.
Syntax
def function_name():
# Code Block
Example:
def welcome():
print("Welcome to Mango Engineers")
This only defines the function.
Nothing happens until it is called.
Calling a Function
To execute a function, use its name followed by parentheses.
def welcome():
print("Welcome to Mango Engineers")
welcome()
Output
Welcome to Mango Engineers
A function can be called multiple times.
welcome()
welcome()
welcome()
Function with Parameters
Parameters allow functions to receive data.
Example:
def greet(name):
print("Welcome", name)
greet("Rahul")
Output
Welcome Rahul
Here,
nameis the parameter."Rahul"is the argument.
Multiple Parameters
Functions can receive multiple values.
def student(name, course):
print(name)
print(course)
student("Rahul","Python")
Output
Rahul
Python
Positional Arguments
Arguments are assigned based on their order.
def employee(name,salary):
print(name)
print(salary)
employee("Amit",50000)
Keyword Arguments
Arguments can also be passed by name.
employee(
salary=50000,
name="Amit"
)
This improves readability.
Default Parameters
Functions can define default values.
def course(name="Student"):
print("Welcome",name)
course()
Output
Welcome Student
Calling with a value:
course("Rahul")
Output
Welcome Rahul
The return Statement
Some functions calculate a value and return it.
Example:
def add(a,b):
return a+b
result = add(10,20)
print(result)
Output
30
Unlike print(), the return statement sends the result back to the caller so it can be stored or used later.
Difference Between print() and return
Using print():
def add(a,b):
print(a+b)
Using return:
def add(a,b):
return a+b
return is preferred because the returned value can be reused in other calculations.
Variable Scope
Variables have different scopes.
Local Variable
A local variable exists only inside a function.
def student():
name = "Rahul"
print(name)
student()
Trying to access name outside the function will produce an error.
Global Variable
A global variable can be accessed throughout the program.
course = "Python"
def display():
print(course)
display()
Output
Python
Recursive Functions
A recursive function calls itself.
Example:
def countdown(n):
if n == 0:
print("Completed")
else:
print(n)
countdown(n-1)
countdown(5)
Output
5
4
3
2
1
Completed
Recursion is commonly used in:
- Tree Traversal
- Searching Algorithms
- Mathematical Problems
- AI Algorithms
Introduction to Lambda Functions
A lambda function is a small anonymous function.
Syntax
lambda arguments : expression
Example
square = lambda x : x*x
print(square(5))
Output
25
Lambda functions are useful for:
- Sorting
- Filtering
- Functional Programming
- Data Science
You’ll study Lambda Functions in detail in the Advanced Python section.
Real-World Example 1: GST Calculator
def calculate_gst(price):
gst = price * 0.18
return gst
amount = 5000
print(calculate_gst(amount))
Output
900
Real-World Example 2: Student Result
def result(marks):
if marks >= 40:
return "Pass"
else:
return "Fail"
print(result(75))
Output
Pass
Real-World Example 3: Shopping Discount
def discount(amount):
if amount >= 5000:
return amount * 0.20
return 0
bill = 7000
print(discount(bill))
Output
1400
Common Mistakes
Forgetting Parentheses
Incorrect
welcome
Correct
welcome()
Missing Return
def add(a,b):
a+b
Correct
def add(a,b):
return a+b
Wrong Number of Arguments
Incorrect
student("Rahul")
Correct
student("Rahul","Python")
Best Practices
- Give functions meaningful names.
- Keep each function focused on one task.
- Avoid very long functions.
- Use parameters instead of global variables.
- Return values instead of printing whenever possible.
- Add comments or docstrings for clarity.
- Reuse functions to avoid duplicate code.
Key Takeaways
- Functions help organize and reuse code.
- Use the
defkeyword to define functions. - Parameters receive values from the caller.
- Arguments provide data to functions.
returnsends values back to the caller.- Variables can have local or global scope.
- Recursive functions call themselves.
- Lambda functions provide a concise way to write simple functions.



