Function_part1
Function_part1
Function: A function is a named unit of a group of program statements. This unit can be invoked from other
parts of the program.
Using function, one can achieve modularity (dividing program into modules) in programming.
Advantages of Function
• Increases readability, particularly for longer code as by using functions, the program is better organized
and easy to understand.
• Reduces code length as same code is not required to be written at multiple places in a program. This also
makes debugging easier.
• Increases reusability, as function can be called from another function or another program. Thus, we can
reuse or build upon already defined functions and avoid repetitions of writing the same piece of code.
Types of Functions
• Built-in Functions: These are pre-defined functions and are always available for use. Programmers do
not need to write the definitions of these functions.
Example- len(), type(), int(), input(), etc.
• Functions defined in modules: These functions are pre-defined in particular modules and can only be
used when the corresponding module is imported. For example, if you want to use pre-defined functions
inside a module, say sin(), you need to first import the module math (that contains definition of sin()) in
your program.
• User-defined functions: These are defined by the programmer. Programmers write the definitions of
these functions.
Parameters
def fn (a,b,c): Function header
d=(a*b*c)/100 Function Definition
Function Body
return d
p=float(input("Enter Principal:")) Output
t=float(input("Enter Time:")) Enter Principal:800
r=float(input("Enter Rate of Interest:")) Enter Time:4
Arguments Enter Rate of Interest:10
si=fn (p,t,r) Function Call Simple Interest= 320.0
print("Simple Interest=",si)
A function definition begins with def (short for define).
• The first line of function definition that begins with keyword def and ends with a colon (:) is called the
function header. It specifies the name of the function and its parameters
• Variables listed within the parenthesis of a function header are called parameters. They are optional.
Hence, a function may or may not have parameters.
• The block of indented-statements beneath function header that defines the action performed by the
function is called the function body.
• A function may return or may not return any value. A function not returning any value can still have a
return statement without any expression or value.
• The values that are passed through a function call are called arguments. They are optional, that is, a
function call may or may not have arguments.
Default Parameter: Python allows assigning a default value to the parameter. A default value is a value
that is pre-decided and assigned to the parameter when the function call does not have its corresponding
argument.
#Program to calculate discounted price
def discounted_price(a,b=10):
dp=a*(100-b)/100 Output
return dp Enter actual price:800
print("Discounted price=",dis_price)
ap=float(input("Enter actual price:"))
dis_price=discounted_price(ap) #Value for parameter b is not being passed
print("Discounted price=",dis_price)