User Defined Functions in Python Chapter 2 (1)
User Defined Functions in Python Chapter 2 (1)
Python Functions is a block of statements that return the specific task. The idea is to put
some commonly or repeatedly done tasks together and make a function so that instead of
writing the same code again and again for different inputs, we can do the function calls to
reuse code contained in it over and over again.
def fun():
print("Welcome to GFG")
In Python Arbitrary Keyword Arguments, *args, and **kwargs can pass a variable number
of arguments to a function using special symbols. There are two special symbols:
*args in Python (Non-Keyword Arguments)
**kwargs in Python (Keyword Arguments)
Example 1: Variable length non-keywords argument
Docstring
The first string after the function is called the Document string or Docstring in short. This is
used to describe the functionality of the function. The use of docstring in functions is
optional but it is considered a good practice.
The below syntax can be used to print out the docstring of a function.
Syntax: print(function_name.__doc__)
return [expression_list]
Risk of Unintended Low, as it’s confined to its Higher, as any function can modify
Modification function. it.
Function is a sub program which consists of set of instructions used to perform a specific
task. A large program is divided into basic building blocks called function.
when the program is too complex and large they are divided into parts. Each part is
separately coded and combined into single program. Each subprogram is called as function.
Debugging, Testing and maintenance becomes easy when the program is divided into
subprograms.
Functions are used to avoid rewriting same code again and again in a program.
Types of function:
i) Built in functions
Built in functions are the functions that are already created and stored in python.
These built in functions are always available for usage and accessed by a programmer. It
cannot be modified.
User defined functions are the functions that programmers create for their requirement
and use.
These functions can then be combined to form module which can be used in other
programs by importing them.
If repeated code occurs in a program, function can be used to include those codes
and execute when needed by calling that function.
Give the function name after def keyword followed by parentheses in which arguments
are given.
Syntax:
def fun_name(Parameter1,Parameter2…Parameter n):
statement1
statement2…
statement n
return[expression]
Example:
def my_add(a,b):
c=a+b
return c
Once we have defined a function, we can call it from another function, program or even
the Python prompt.
arguments.
Example:
x=5
Flow of Execution:
The order in which statements are executed is called the flow of execution
Function definitions do not alter the flow of execution of the program, but remember
that statements inside the function are not executed until the function is called.
Function calls are like a bypass in the flow of execution. Instead of going to the next
statement, the flow jumps to the first line of the called function, executes all the statements
there, and then comes back to pick up where it left off.
Note: When you read a program, don’t read from top to bottom. Instead, follow the flow of
execution. This means that you will read the def statements as you are scanning from top to
bottom, but you should skip the statements of the function definition until you reach a point
where that function is called.
Function Prototypes:
o The sub function will read the input values perform the operation and print the result
in the same block
o Arguments are passed through the function call but output is not return to the main
function
o In this type no argument is passed through the function call but output is return to the
main function.
In this type arguments are passed through the function call and output is return to the main
function
Without argument
def add():
a=int(input("enter a"))
b=int(input("enter b"))
c=a+b
print(c)
add()
With argument
def add(a,b):
c=a+b
print(c)
a=int(input("enter a"))
b=int(input("enter b"))
add(a,b)
OUTPUT:
enter a 5
enter b 10
15
Without argument
def add():
a=int(input("enter a"))
b=int(input("enter b"))
c=a+b
return c
c=add()
print(c)
With argument
def add(a,b):
c=a+b
return c
a=int(input("enter a"))
b=int(input("enter b"))
c=add(a,b)
print(c)
OUTPUT:
enter a 5
enter b 10
15
Parameters:
Parameters are the value(s) provided in the parenthesis when we write function
header.
If there is more than one value required, all of them will be listed in parameter
list separated by comma.
Example: my_add(x,y)
RETURN STATEMENT:
The return statement is used to exit a function and go back to the place from
where it was called.
If the return statement has no arguments, then it will not return any values. But
exits from function.
Syntax:
return[expression]
Example:
def my_add(a,b):
c=a+b
return c
x=5
y=4
print(my_add(x,y))
Output:
9
1. Required Arguments
2. Keyword Arguments
3. Default Arguments
4. Variable length Arguments
1. Required Arguments:
The number of arguments in the function call should match exactly with the function
definition.
def my_details( name, age ):
print("Name: ", name)
print("Age ", age)
return
my_details("george",56)
Output:
Name: george
Age 56
2. Keyword Arguments:
Python interpreter is able to use the keywords provided to match the values with
parameters even though if they are arranged in out of order.
def my_details( name, age ):
print("Name: ", name)
print("Age ", age)
return
my_details(age=56,name="george")
Output:
Name: george
3. Default Arguments:
Assumes a default value if a value is not provided in the function call for that argument.
def my_details( name, age=40 ):
print("Name: ", name)
print("Age ", age)
return
my_details(name="george")
Output:
Name: george
Age 40
If we want to specify more arguments than specified while defining the function, variable
length arguments are used. It is denoted by * symbol before parameter.
def my_details(*name ):
print(*name)
my_details("rajan","rahul","micheal",
ärjun")
Output:
rajan rahul micheal ärjun