Function in Python
Functions are block of executable statements. It designed to perform a specific task and
returns a value. It modularizes a program. It provides reusability of the code, which improves
clarity and efficiency.
The purpose of creating a function is to combine repetitive task in a function block, and
reuse this task whenever required, throughout the program.
Types of Functions
Python Built-in Function
1. Built -in : These are predefined functions, which are available for use without a need
to import any external libraries. These functions provide the basic essential
functionality for performing common task, such input/output operations,
manipulating data types, and many more. Python includes 60 built-in functions which
does not requires any external libraries.
Example of some of the built-in functions
print() – prints output on the console
input() – takes input from the console
type() – checks the data type of a variable and display it
len() – counts the number element present in an object.
max() – returns the largest element from the object.
min() – returns the smallest element from the object.
2. User defined Function – Function defined by user , based on the requirements are
called as User defined function.
a. User defined function syntax
def function_name(parameters):
function statements
parameter
Function name
Keyword
function_name (parameter)
def
Block of Statement
Return statement
--Priya Shetty
Breakdown of Function declaration
1. def – We use the keyword def to declare a function
2. function_name – This is an identifier that follows the same naming conventions as
Python variables. It is the name by which a function is referenced in the program.
The function name must be descriptive to indictive what the function does.
3. Parameters – These are input variables or values that are passed into the function.
Parameters are optional, so a function may not have a parameters.
4. Function Body – This where you write your function executable code which defines
the task of your function. It consist of statements, including return statements that
send the result back to the calling function. Every time a function is called, these
function body is executed.
5. Return – it is optional, to specify the return value using return statement. It is the
end of the function. It a function is not specified with return statement, it returns
none by default.
Creating a Function
Example 1.
The above example shows, a function names display() is created and within the function
a statement is defined which print(“Hello World”)
Calling a function
Once a function is created, to execute the function we need to call them. That process is
called a function. This step invokes the function definition.
--Priya Shetty
In the above code, you can see we have define the function, them we have called the
function with function name and parenthesis i.e., display()
Function with parameters
A function can be defined with parameters. These parameters will act as placeholder,
when the functions are called a value will be passed to these parameters. The function
will perform the specific task on these input values.
The above code you can see is defined with a parameter named a, so when we call the
fuction display we will pass a value directly or through a variable. Here we are passing
through a variable named a which is assigned “Hello World”
This type of function is called as Function with parameters, there are no restriction to the
number for parameters defined.
Example 3
--Priya Shetty
In the above example you can see we have defined a function with two parameters a and b,
the function is responsible to take values from function call and perform addition on those
values.
Also, we have called functions twice one where a function is passing a direct value to the
function definition i.e., add (4,5) here 4 will be assigned to a and 5 will be assigned b. and
the second function calling here we are passing values using the variables i.e., add (num1,
num2) as you can see num1 is assigned 7 and num2 is assigned 5. This value will respectively
be assigned to a and b i.e., a = num1 and b = num2.
Function Arguments/Parameter
There are four main types of Arguments
1. Default Argument
2. Keyword Argument
3. Positional Argument
1. Default Argument
The parameter will take the default defined during the definition, if no value is
passed for that particular arguments. Example given below display function is
defined with parameter(arguments) msg parameter is defined with a default value
“Hello”. When the function is called for the first time you can see the function is
passing only one variable that is name, this name is assigned to the name defined in
function definition, and the other parameter msg is not assigned any value during
function calling so it will take the default value ie., Hello.
During second execution we are passing two arguments both are being assigned to
name and msg respectively, as msg parameters is receiving a value from calling
function it will ignore the defined value “Hello” and accept the new value assigned.
--Priya Shetty
2. Keyword Argument
By default, when a function is passing value to the function definition, it passes the
values in format of positional argument, where value will be assigned in sequence by
default. But if you want to assign non positional argument, where position does not
matter, we use keyword argument. So here during function calling you are supposed
to pass the value with argument name.
In below example we are passing the values with arguments, so position does not
matter. It verifies the name and assign the value
Here, in the example you can see while passing the values the first parameter is age,
then name and role whereas in function definition the first parameter defined is
name, age and then role. So here the position doesn’t matter as during the function
call, we pass the variable with assigned values.
--Priya Shetty
3. Positional Argument
The Functions with arguments are called as Positional Argument. Here while
assigning the value, we have to follow the order for parameters.
Return Statement in Function
The Python return statement is used to exit a function and return to the function caller. It
optionally returns the specified value from the function to the caller. If there is no
expression, it returns None. We can include a variable, constant, or expression in the return
statement, which is returned at the end of execution.
--Priya Shetty
Advantages for Function
1. Code Reusability
2. Modularity
3. Flexibility
4. Code Clarity
5. Maintainability
6. Reduces Complexity
--Priya Shetty