Chapter-3 Working With Functions
Chapter-3 Working With Functions
COMPUTER SCIENCE
CHAPTER 3: WORKING WITH FUNCTIONS
Functions:
It is a group of statements that exist within a program for the purpose of performing a
specific task.
Function consists of:
• Arguments, i.e., values if needed
• Set of statements to do task
• Result which is returned.
e.g. def calculate(x):
r=2*x**2
return r
where def is the keyword which indicates a function definition.
calculate is the function name
x is the argument
return statement returns the result
Invoking or Calling a function:
Syntax:
functionname(argument)
a=int(input(“Enter a number:”))
print(calculate(a)) # function call
Function Types:
Functions are classified as 3 types:
i. Built-in functions:
They are pre-defined functions that are already available in Python.
e.g. len(), type(), input(), int(), eval(), abs() etc…
ii. Modules:
They are small segments that splits lengthy program to reduce complexity. A module is
file containing functions and variables, defined in separate files and this module is imported.
They are imported from the libraries.
e.g. sin(), sqrt() are imported from math
import math
math.sqrt(4)
1
iii. User-defined functions:
These are functions which are defined by the programmer.
e.g. calculate()
Defining Functions:
Syntax:
def <function name> (<parameters>):
<statements>
e.g. def area(x,y):
a=x*y
return a
Passing Parameters:
There are 3 types of arguments:
i. Positional arguments (Required argument)
ii. Default arguments
iii. Keyword or named arguments.
➢ Positional arguments:
Are arguments passed to a function in correct positional order.
The number and position of arguments must be matched. If we change, the result will be changed.
e.g. def sum(a,b,c):
__________
__________
sum(x,y,z) or sum(2,3,4)
➢ Default arguments:
Is an argument that assumes a default value if a value is not provided in the function call.
e.g. def S1(p,t,r=5.2):
_____________
2
Using Multiple Argument types together
e. g. interest (5000, time=5)
# 5000 represent positional argument as it will be assigned to first parameter on the basis
of its position
# time=5 represent keyword argument or named argument
Python states that in a function call statement following:
• An argument list must first contain positional argument then keyword argument
• Keyword arguments should be taken from the required arguments
• Cannot specify a value for an argument more than once.
e.g., def Interest (prin, cc, time=2, rate=0.09):
here prin and cc are positional argument and time and rate are keyword argument.
Having positional argument after keyword arguments will result into error.
e.g., Interest (5000, 3, rate=0.05) # valid
Interest (rate=0.05, 5000, 3) # invalid (keyword argument came before positional argument).
NOTE: Go through the table given in Page no. 109
3
NOTE: The return statement terminates a function execution.
i.e. def sum(x,y)
s=x+y
return s
print(s) #won’t work
sum(x,y)
4
Scope of Variables:
All variables in a program cannot be accessible at all location in that program. This depends
on where you have declared a variable.
There are two kinds of scope in python. They are:
1. Global Scope
• Names assigned at the top level of a module.
• Usable inside the whole program in all blocks.
2. Local Scope:
• Names assigned inside a function definition or loop.
• Can be used only within the function where it declared.
e.g. def sub(x,y)
s=x-y #x,y,z are local
return s
n1=int(input(“Enter a no:”)) #n1, n2 are local
n2=int(input(“Enter a no:”))
s1=sub(n1,n2)
print(“Subtraction=”,s1)
NOTE: Go through the example given in textbook page no. 116 to 119
Name Resolution:
When we access a variable from within a program or function, python, follows name
resolution rule known as LEGB rule (Local
Global Built-in environment)
For every name reference, python does:
i. It check with local environment if it has a variable with the same name.
ii. Checks the enclosing environment
iii. Checks the global environment
iv. Check Built in environment
NOTE: Go through the example given in textbook page no. 121
5
randint(a,b)
Returns a random number in given range, Both range limits are inclusive
Returns integer number
e.g. import random
print(random.randint(4,20)) Output 8
import random
S1=random.randint(1,100) Output
S2= random.randint(1,100) 95 75 3
S3=random.randint(1,100)
print(S1, S2, S3)
randrange()
This function generate random numbers from a specified range .
Eg random.randrange(90,100) will generate values from 90 to 99