17 Working with Functions Part 1
17 Working with Functions Part 1
By Chirag Parikh
Introduction
By Chirag Parikh
Understanding Functions
Identifier following ‘def’ is the name of the function, i.e., here the
function name is calcSomething
By Chirag Parikh
Cont. Understanding Functions
The statements indented below the function, (i.e., block below def
line) defines the functionality of the function. This block is also called
body of the function.
By Chirag Parikh
Cont. Understanding Functions
E.g., calcSomething(5)
1. Built-in Functions
These are pre-defined functions and are always available for use. E.g., len(
), type( ), int( ), input( ) Etc.
2. Functions defined in modules
These functions are pre-defined in particular modules and can only be used
when the corresponding module is imported.
3. User defined functions
These are defined by the programmer.
By Chirag Parikh
Top-level statements
The statements which are not part of any functions, are not indented at all
and are called top-level statements.
The top level statements are part of the main program. Internally Python
gives a special name to top-level statements as __main__.
Python stores this name in a built-in variable called __name__.
By Chirag Parikh
Program to add two numbers through a function
By Chirag Parikh
Arguments and Parameters
By Chirag Parikh
Cont. Arguments and Parameters
But if we are passing the values of mutable types (e.g., list or dictionaries)
then called function would be able to make changes in them. (Call By
Reference)
By Chirag Parikh
Types of Arguments
2. Default arguments
By Chirag Parikh
Default Arguments
The default values are specified in the function header of function definition.
E.g.
def interest (prin, time = 2, rate) : is invalid
def interest (prin, time = 2, rate = 0.10) : is valid
By Chirag Parikh
Keyword (Named) Arguments
Python offers a way of writing function calls where you can write any
argument in any order provided you name the arguments when calling the
function.
E.g.,
interest (time = 4, prin = 2600, rate = 0.09)
interest (time = 2, rate = 0.12, prin = 2000)
This way of specifying names for the values being passed, in the function call
is known as keyword arguments.
By Chirag Parikh
Using multiple argument types together
Having a positional arguments after keyword arguments will result into error.
By Chirag Parikh
Program
def interest(principal, time=2, rate=0.10):
return principal * rate * time
prin = float(input("Enter principal amount:"))
print("Simple interest with default ROI and time value is: ")
sil = interest(prin)
print("Rs. ",sil)
roi = float(input("Enter rate of interest (ROI):"))
time = int(input("Enter time in years:"))
print ("Simple interest with your provided ROI and time value is: ")
si2 = interest(prin,time, roi/100) Output:
print("Rs.",si2) Enter principal amount:5000
Simple interest with default ROI and time value is:
Rs. 1000.0
Enter rate of interest (ROI):5
Enter time in years:3
Simple interest with your provided ROI and time value is:
By Chirag Parikh
Rs. 750.0