What is a Function?
A function is a reusable block of code that performs a specific task.
def greet():
print("Hello, World!")
greet()
Once defined, a function can be called multiple times:
greet()
greet()
This avoids repetition and makes your code cleaner.
Function Syntax
def func_name(val1, val2): # parameters
# code
pass
- def → keyword to define function
- func_name → function name
- val1, val2 → parameters
- pass → placeholder
Parameters vs Arguments
def greet(name):
print("Hello,", name)
greet("Piyush")
- name → parameter (definition time)
- "Piyush" → argument (calling time)
Important rule:
Number of parameters = Number of arguments (unless defaults are used)
Functions with Return Value
def add(a, b):
return a + b
result = add(5, 3)
print(result)
Key difference:
- print() → displays output
- return → sends value back for reuse
Types of Arguments
1. Positional Arguments
Order matters.
def add(b, a):
print(a)
print(b)
return a + b
add(5, 3)
Output:
3
5
8
2. Keyword Arguments
Order does not matter.
def example(name, age):
print(f"My name is {name} and my age is {age}")
example(age=30, name="Piyush")
3. Default Arguments
Used when values are not provided.
def example(name="Not Known", age="I Don't know"):
print(f"My name is {name} and my age is {age}")
example("Rafish")
example("Rahul", 45)
example()
4. Variable-Length Arguments (*args)
Used when number of inputs is unknown.
def total(*nums):
return sum(nums)
print(total(1, 2, 3, 4, 5))
5. Keyword Variable-Length Arguments (**kwargs)
Accepts multiple key-value pairs.
def details(**data):
print(data)
details(name="Piyush", age=30, phone=12345)
Key Observations
- Functions can accept any data type (even True, strings, etc.)
- Flexible argument handling makes functions powerful
- Widely used in APIs, backend systems, automation, and ML pipelines
Assignments (Based on Today’s Concepts)
Basic Level
- Create a function that prints "Hello Python"
- Write a function that takes a name and prints a greeting
- Create a function that adds two numbers and returns the result
- Call a function multiple times and observe output
- Pass different data types (int, string, boolean) to a function
Intermediate Level
- Create a function using positional arguments and observe order impact
- Create a function and call it using keyword arguments
- Write a function with default parameters and test all cases
- Create a function using *args to find the sum of numbers
- Modify *args function to return maximum value
Advanced Level
- Create a function using **kwargs and print all key-value pairs
- Build a function that accepts both *args and **kwargs
- Create a function that validates input before processing
- Write a function that returns multiple values (sum, average)
- Implement a mini user profile system using **kwargs
Example idea:
def profile(**data):
for key, value in data.items():
print(f"{key} : {value}")
Summary
- Functions make code reusable and structured
- return is essential for real-world applications
- Argument types provide flexibility (*args, **kwargs)
- Understanding parameter behavior is critical for debugging
.png)

0 Comments:
Post a Comment