Functions in Python Class 11 Notes
Functions in Python Class 11 Notes
Once defined, a function can be used again throughout the programme without
requiring the user to write out the entire code for it each time. It can also be used
inside of other functions by simply writing the function name and the necessary
parameters.
A function definition begins with def (short for define). The syntax for creating
a user defined function is as follows –
Syntax –
The items enclosed in “[ ]” are called parameters and they are optional.
Hence, a function may or may not have parameters. Also, a function may
or may not return a value.
Function header always ends with a colon (:).
Function name should be unique. Rules for naming identifiers also applies
for function naming.
The statements outside the function indentation are not considered as part
of the function.
Q. Write a user defined function to add 2 numbers and display their sum.
#Program
#Function to add two numbers
def addnum():
fnum = int(input(“Enter first number: “))
snum = int(input(“Enter second number: “))
sum = fnum + snum
print(“The sum of “,fnum,”and “,snum,”is “,sum)
#function call
addnum()
Output:
Enter first number: 5
Enter second number: 6
The sum of 5 and 6 is 11
Q. Write a program using a user defined function that displays sum of first
n natural numbers, where n is passed as an argument.
#Program
#Program to find the sum of first n natural numbers
def sumSquares(n):
sum = 0
for i in range(1,n+1):
sum = sum + i
print(“The sum of first”,n,”natural numbers is: “,sum)
num = int(input(“Enter the value for n: “))
sumSquares(num) #function call
#Program 7-6
#Function to calculate mean
def myMean(myList):
total = 0
count = 0
for i in myList:
total = total + i
count = count + 1
mean = total/count
print(“The calculated mean is:”,mean)
myList = [1.3,2.4,3.5,6.9]
myMean(myList)
Output:
The calculated mean is: 3.5250000000000004
#Program 7-7
#Function to calculate factorial
def calcFact(num):
fact = 1
for i in range(num,0,-1):
fact = fact * i
print(“Factorial of”,num,”is”,fact)
num = int(input(“Enter the number: “))
calcFact(num)
Output:
Enter the number: 5
Factorial of 5 is 120
String as Parameters
Some programmes may require the user to supply string values as an argument.
Q. Write a program using a user defined function that accepts the first
name and lastname as arguments, concatenate them to get full name and
displays the output as:
#Program
#Function to display full name
def fullname(first,last):
fullname = first + ” ” + last
print(“Hello”,fullname)
first = input(“Enter first name: “)
last = input(“Enter last name: “)
fullname(first,last)
Output:
Enter first name: Gyan
Enter last name: Vardhan
Hello Gyan Vardhan
Default Parameter
The argument can be given a default value in Python. When a function call
doesn’t have its appropriate argument, a default value is chosen in advance and
given to the parameter.
#Program
#Function to display mixed fraction for an improper fraction
def mixedFraction(num,deno = 1):
remainder = num % deno
if remainder!= 0:
quotient = int(num/deno)
print(“The mixed fraction=”, quotient,”(“,remainder, “/”,deno,”)”)
else:
print(“The given fraction evaluates to a whole number”)
num = int(input(“Enter the numerator: “))
deno = int(input(“Enter the denominator: “))
print(“You entered:”,num,”/”,deno)
if num > deno:
mixedFraction(num,deno)
else:
print(“It is a proper fraction”)
Output:
Enter the numerator: 17
Enter the denominator: 2
You entered: 17 / 2
The mixed fraction = 8 ( 1 / 2 )
The function’s values are returned using the return statement. A function that
has finished its duty will return a value to the script or function that called it.
#Program
#Function to calculate and display base raised to the power exponent
def calcpow(number,power):
result = 1
for i in range(1,power+1):
result = result * number
return result
base = int(input(“Enter the value for the Base: “))
expo = int(input(“Enter the value for the Exponent: “))
answer = calcpow(base,expo)
print(base,”raised to the power”,expo,”is”,answer)
Output:
Enter the value for the Base: 5
Enter the value for the Exponent: 4
5 raised to the power 4 is 625
Flow of Execution
Scope of a Variable
An internal function variable can’t be accessed from the outside. There is a well
defined accessibility for each variable. The scope of a variable is the area of the
programme that the variable is accessible from. A variable may fall under either
of the two scopes listed below:
A variable with a global scope is referred to as a global variable, whereas one
with a local scope is referred to as a local variable.
Local Variable – A local variable is one that is declared inside any function or
block. Only the function or block where it is defined can access it.
Built-in functions
The pre-made Python functions that are widely used in programmes are known
as built-in functions. Let’s examine the subsequent Python programme –
#Program to calculate square of a number
a = int(input(“Enter a number: “)
b=a*a
print(” The square of “,a ,”is”, b)
In the programme mentioned above, the built-in functions input(), int(), and
print() are used. The Python interpreter already defines the set of instructions
that must be followed in order to use these built-in functions.
Module
The Python standard library includes a number of modules. A module is a
collection of functions, whereas a function is a collection of instructions.
suppose we have created some functions in a program and we want to
reuse them in another program. In that case, we can save those functions under a
module and reuse them. A module is created as a python (.py) file containing a
collection of function definitions.
Built-in Modules
Let’s examine a few widely used modules and the functions that are contained
in such modules: