í PYTHON
FUNCTIONS 3
COMPLETE
NOTES
(CO4 3 MUST STUDY TOPIC)
à 1. What is a Function?
A function is a block of statements which executes only when it is called.
Purposes:
Reusability
Modular code
Avoid rewriting
Easy debugging
Logical grouping
Syntax:
def function_name(parameters):
statements
Example:
def greet():
print("Hello")
Calling:
greet()
à 2. Types of Functions
Type Meaning
Built-in Functions Provided by Python
User-defined Functions Created by programmer
Lambda Functions Anonymous small functions
Recursive Functions Function calling itself
à 3. Built-in Functions
MOST IMPORTANT ONES for exam:
I/O & Type Functions Sequence Functions
print() len()
input() range()
type() sorted()
int(), float(), str() enumerate()
zip()
Numeric & Utility Functions Functional Programming
max() map()
min() filter()
sum() id()
abs()
round()
à 4. User-Defined Function Syntax
A user-defined function is created by the programmer to perform specific tasks.
Basic Syntax:
def function_name():
statements
The def keyword is used to define a function. The function name should be descriptive and follow naming conventions. The
colon : indicates the start of the function body, which must be indented.
à 5. Function with Parameters
Functions can accept parameters (also called arguments) to make them more flexible and reusable.
Example:
def add(a, b):
return a+b
In this example, a and b are parameters. When you call the function, you pass values for these parameters. The function then
uses these values to perform the calculation and return the result.
à 6. Function Returning a Value
Functions can return values using the return statement. This allows the function to send a result back to the caller.
Example:
def square(x):
return x*x
When this function is called with an argument, it calculates the square of that number and returns the result. The returned
value can be stored in a variable or used directly in expressions.
à 7. Function Without Return
Not all functions need to return a value. Some functions perform actions or display output without returning anything.
Example:
def hello():
print("Hello")
This function simply prints a message when called. It does not use a return statement, so it returns None by default. Such
functions are useful for performing side effects like printing, writing to files, or modifying global state.
à 8. Function Without Arguments
Functions do not always require parameters. A function can be defined without any arguments and still perform useful tasks.
Example:
def msg():
print("Python")
This function takes no arguments and simply executes its statements whenever it is called. Each time you call msg(), it will
print "Python" to the console. Functions without arguments are useful for repetitive tasks that do not require varying input.
à 9. Function With Arguments
Functions with arguments allow you to pass data to the function for processing. The arguments are used within the function
body to perform operations.
Example:
def add(a,b):
print(a+b)
This function accepts two arguments, a and b, and prints their sum. When you call add(5, 3), the function will print 8. Unlike
the function that returns a value, this function performs an action (printing) directly rather than returning the result for further
use.