0% found this document useful (0 votes)
2 views6 pages

Functions in Python

This document provides an overview of functions in Python, including their definition, syntax, and types. It explains the importance of functions for code reusability and organization, and details user-defined functions based on arguments and return values. Additionally, it covers parameters, types of parameters, and includes small interview questions related to function implementation.

Uploaded by

snehitha02468
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Functions in Python

This document provides an overview of functions in Python, including their definition, syntax, and types. It explains the importance of functions for code reusability and organization, and details user-defined functions based on arguments and return values. Additionally, it covers parameters, types of parameters, and includes small interview questions related to function implementation.

Uploaded by

snehitha02468
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

⭐ FUNCTIONS IN PYTHON

✅ Definition
A function in Python is a reusable block of code that performs a specific task.
Functions help in code reusability, readability, and modular programming.

✔️Why use functions?


 Avoid repeating code
 Better structure
 Easy debugging
 Reusability
 Clean and organized program
 Break large problems into small parts

⭐ SYNTAX OF A FUNCTION
def function_name(parameters):
# body
return value

 def → keyword to define a function


 function_name → name of the function
 parameters → input values (optional)
 return → output (optional)

TYPES OF FUNCTIONS
Python has 2 basic types:
🔹 1. Built-in Functions
Built-in functions are functions that are already available in Python.
You can use them directly without importing any module.
Examples:
print()
len()
type()
range()
sum()
max(), min()
input()
sorted()
What is return in Python?
The return statement is used to send a value from a function back to the caller and end the
function execution.
👉 A function with return gives output that can be stored, reused, or processed further.

Return vs Print (Very Important ⭐)


return print
Sends value to caller Displays value
Can be stored in variable Cannot be reused
Ends function Does not end function

Types of User-Defined Functions


Based on arguments and return values, user-defined functions are classified into 4 types:
🔹 1. Function with NO Arguments and NO Return Value
🔹 Definition
 Does not take any input
 Does not return any output
Example:
def greet():
print("Welcome to Python")
greet()

Function with Arguments and NO Return Value


🔹 Definition
 Takes input values
 Does not return a value
 Performs an action only
Example:
def add(a, b):
print(a + b)
add(10, 20)

Function with NO Arguments and WITH Return Value


🔹 Definition
 Does not take input
 Returns an output using return
Example:
def get_pi():
return 3.14
value = get_pi()
print(value)

4. Function with Arguments and WITH Return Value (MOST IMPORTANT ⭐)


🔹 Definition
 Takes input
 Returns output
 Most commonly used in real projects
Example:
def square(num):
return num * num
print(square(5))

⭐ PARAMETERS vs ARGUMENTS
🔹 Parameters
Variables inside the function definition.
def add(a, b): # a, b → parameters
🔹 Arguments
Actual values passed to function.
add(10, 20) # 10, 20 → arguments

Parameters in Functions?
Parameters are variables written inside the function definition that receive values when the
function is called.
👉 They act like placeholders for data.
def add(a, b): # a and b are PARAMETERS
return a + b
add(10, 20) # 10 and 20 are ARGUMENTS

🔹 Types of Parameters in Python


Python supports 5 types of parameters:
1️⃣ Positional Parameters
2️⃣ Default Parameters
3️⃣ Keyword Parameters
4️⃣ Variable-Length Parameters
• *args
• **kwargs
5️⃣ Positional-only & Keyword-only (advanced)

1️⃣ Positional Parameters


Values are passed based on position/order.
def student(name, age):
print(name, age)
student("Jaya", 21)
Output:
Jaya 21

2️⃣ Default Parameters


A default value is assigned to a parameter.
If no argument is passed → default value is used.
Example:
def greet(name="User"):
print("Hello", name)
greet()
greet("Jaya") Hello User
Hello Jaya

3️⃣ Keyword Parameters


Arguments passed using parameter names.
Order does NOT matter.
Example:
def marks(math, science):
print(math, science)
marks(science=85, math=90)
Variable-Length Parameters
Used when number of arguments is unknown.
*args (Non-keyword variable length)
Accepts multiple values as a tuple.
Multiple positional arguments

Example:
def total(*nums):
print(nums)

total(10, 20, 30, 40) #(10,20,30,40)

**kwargs (Keyword variable length)


Accepts key-value pairs as a dictionary.
→ Multiple keyword arguments
Example:
def details(**info):
print(info)
details(name="Jaya", age=21, course="Python") {'name': 'Jaya', 'age': 21, 'course': 'Python'}

⭐ SMALL INTERVIEW QUESTIONS


✔️1. Write a function to check even or odd
def check(n):
return "Even" if n % 2 == 0 else "Odd"
✔️2. Function to return factorial
def fact(n):
res = 1
for i in range(1, n+1):
res *= i
return res
✔️3. Function to count vowels
def vowels(s):
v = "aeiouAEIOU"
return sum(1 for ch in s if ch in v)

You might also like