Python Functions
Functions are reusable blocks of code that perform a specific task. Functions help programmers avoid writing the same code again and again.
Why Functions are Important?
- Functions reduce repeated code.
- Functions make programs organized.
- Functions improve readability.
- Functions make debugging easier.
- Functions are reusable in multiple places.
Creating a Function
In Python, functions are created using the def keyword.
def greet():
print("Welcome to CodeVyro")
Here:
- def is used to define a function.
- greet is the function name.
- Parentheses () are required.
- Indented code belongs to the function.
Calling a Function
A function runs only when it is called.
def greet():
print("Welcome to CodeVyro")
greet()
Output
Welcome to CodeVyro
When Python sees greet(), it executes the function code.
Function with Parameters
Parameters allow functions to receive data.
def greet(name):
print("Hello", name)
greet("Rahul")
How this Works?
Step 1: Function accepts parameter name.
Step 2: Value "Rahul" is passed into the function.
Step 3: Python prints the message.
Output
Hello Rahul
Function with Multiple Parameters
def student(name, marks):
print("Name:", name)
print("Marks:", marks)
student("Ali", 95)
Output
Name: Ali Marks: 95
Return Statement
The return keyword sends a value back from a function.
def add(a, b):
return a + b
result = add(10, 20)
print(result)
How this Program Works?
- Function receives values 10 and 20.
- Python adds both numbers.
- return sends result back.
- Returned value gets stored in variable result.
Output
30
Difference Between print() and return
- print() displays output on screen.
- return sends value back to the caller.
- Returned values can be stored and reused.
Default Parameters
Functions can have default values.
def greet(name = "Guest"):
print("Hello", name)
greet()
greet("Rahul")
Output
Hello Guest Hello Rahul
If no value is passed, Python uses the default value.
Keyword Arguments
Arguments can be passed using parameter names.
def student(name, marks):
print(name)
print(marks)
student(marks = 90, name = "Ali")
Output
Ali 90
Arbitrary Arguments (*args)
*args allows multiple values to be passed into a function.
def numbers(*data):
print(data)
numbers(10, 20, 30, 40)
Output
(10, 20, 30, 40)
Variable Scope
Variables created inside functions are local variables.
def test():
x = 100
print(x)
test()
Variable x exists only inside the function.
Global Variables
Variables created outside functions are global variables.
name = "Python"
def show():
print(name)
show()
Output
Python
Recursion
Recursion happens when a function calls itself.
def countdown(n):
if n == 0:
print("Finished")
else:
print(n)
countdown(n - 1)
countdown(5)
Output
5 4 3 2 1 Finished
Built-in Functions
Python already provides many built-in functions.
print(len("Python"))
print(max(10, 50, 20))
print(min(10, 50, 20))
Output
6 50 10
Real Life Example
This example creates a simple calculator function.
def calculator(a, b):
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
calculator(10, 5)
Output
Addition: 15 Subtraction: 5 Multiplication: 50
Summary
- Functions are reusable blocks of code.
- Functions are created using def keyword.
- Functions run only when called.
- Parameters allow passing data.
- return sends values back.
- Functions improve readability and reusability.
- Python supports recursion and built-in functions.