0% found this document useful (0 votes)
6 views3 pages

Function_part1

A function is a named unit of program statements that promotes modularity, readability, and reusability in programming. There are three types of functions: built-in, module-defined, and user-defined, with user-defined functions allowing programmers to create custom functionality. The document also explains function definitions, parameters, arguments, and default parameters, providing examples of calculating simple interest and discounted prices.

Uploaded by

MriNmoy Paul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Function_part1

A function is a named unit of program statements that promotes modularity, readability, and reusability in programming. There are three types of functions: built-in, module-defined, and user-defined, with user-defined functions allowing programmers to create custom functionality. The document also explains function definitions, parameters, arguments, and default parameters, providing examples of calculating simple interest and discounted prices.

Uploaded by

MriNmoy Paul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

FUNCTION

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.

User defined Function


#Program to find simple interest using function.

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

Enter discount percentage:15


ap=float(input("Enter actual price:")) Discounted price= 680.0

d=float(input("Enter discount percentage:")) Enter actual price:600

dis_price=discounted_price(ap,d) #Value for parameter b is being passed Discounted price= 540.0

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)

In the above program,


In function call discounted_price(ap,d), value for parameter b has been passed. So, the value of the
parameter b will be the value of d.
In function call discounted_price(ap), value for parameter b has not been passed. So, the value of the
parameter b will be the default value 10.
Note: The default parameters must be the trailing parameters in the function header that means if any
parameter is having default value then all the other parameters to its right must also have default values.
Let us consider few more function definition headers:
def calcInterest(principal = 1000, rate, time = 5): #Incorrect as the principal can not have default value
unless rate and time have default value.
def calcInterest(rate,principal = 1000, time = 5): #Correct

You might also like