TOPIC: PYTHON FUNCTIONS &
ORGANIZING CODE USING FUNCTIONS
Introduction to Functions:- A function is a block of code which only runs when it
is called.
You can pass data, known as parameters, into a function.
A function can return data as a result.
Why Use Functions?
• Improves code readability and organization
• Promotes reusability (write once, use many times)
• Helps in modular programming
• Makes testing and debugging easier
Types of Functions in Python:-
Type Description
Built-in Functions Predefined (e.g., len(), sum())
Created by users using def
User-defined Functions
Lambda Functions Anonymous, single-expression functions
Creating a Function:-In Python a function is defined using the def keyword
# Code
def my_function():
print("Hello from a function")
Calling a Function:-To call a function, use the function name followed by
parenthesis
#Code
def my_function():
print("Hello from a function")
my_function()
#code
def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
Parameter:-
A parameter is a variable defined inside the parentheses in the function definition.
It acts as a placeholder for the value you pass when you call the function.
In your code:
fname is the parameter.
Argument:-
An argument is the actual value passed to the function when it is called.
Arguments are assigned to parameters when the function is executed.
In your code:
"Emil"
# code
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Emil", "Refsnes")
Arbitrary Arguments, *args:- If you do not know how many arguments that will
be passed into your function, add a * before the parameter name in the
function definition.
This way the function will receive a tuple of arguments, and can access the
items accordingly:
If the number of arguments is unknown, add a * before the parameter name:
def my_function(*kids):
print("The youngest child is " + kids[2])
my_function("Emil", "Tobias", "Linus")
Keyword Arguments:- You can also send arguments with
the key = value syntax.
This way the order of the arguments does not matter.
#code
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus")
Default Parameter Value:- If we call the function without argument, it uses the
default value.
#code
def my_function(country = "Norway"):
print("I am from " + country)
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
Passing a List as an Argument:- You can send any data types of argument to a
function (string, number, list, dictionary etc.), and it will be treated as the
same data type inside the function.
E.g. if you send a List as an argument, it will still be a List when it reaches
the function:
#code
def my_function(food):
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
Return Values:- To let a function return a value, use the return statement.
#code
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))