0% found this document useful (0 votes)
157 views

Computer Science Class-Xii Code No. 083 2021-22: Functions in Python

1. The general format of user defined functions in Python is defined by the keyword 'def' followed by the function name and parameters in parentheses, a colon, and indented code block for the function body. 2. Parameters are the variable names listed in the function definition while arguments are the actual values passed to the function when calling it. 3. The different types of parameters Python supports are positional arguments, default arguments, and keyword (named) arguments. Positional arguments match values to parameters by position, default arguments have preset values if not passed, and keyword arguments specify names for values.

Uploaded by

Ahmed Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
157 views

Computer Science Class-Xii Code No. 083 2021-22: Functions in Python

1. The general format of user defined functions in Python is defined by the keyword 'def' followed by the function name and parameters in parentheses, a colon, and indented code block for the function body. 2. Parameters are the variable names listed in the function definition while arguments are the actual values passed to the function when calling it. 3. The different types of parameters Python supports are positional arguments, default arguments, and keyword (named) arguments. Positional arguments match values to parameters by position, default arguments have preset values if not passed, and keyword arguments specify names for values.

Uploaded by

Ahmed Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Computer Science

CLASS-XII
Code No. 083
2021-22

FUNCTIONS IN PYTHON

Topics of Functions:
Types of function (built-in functions, functions
defined in module, user defined functions), creating
user defined function, arguments and parameters,
default parameters, positional parameters, function
returning value(s), flow of execution, scope of a
variable (global scope, local scope)

Functions are the subprograms that perform specific task. Functions are the
small modules.

Types of Functions:
There are three types of functions in python:
1. Library Functions (Built in functions)
2. Functions defined in modules
3. User Defined Functions
1. Library Functions: These functions are already built in the python library.
For example: type( ), len( ), input( ) etc.

2 Functions defined in modules: These functions defined in particular modules.


When you want to use these functions in program, you have to import the
corresponding moduleof that function.

(a) Functions of math module:


To work with the functions of math module, we must import math module in
program.

import math
S. No. Function Description Example
1 sqrt( ) Returns the square root of a number >>>math.sqrt(49)
7.0
2 ceil( ) Returns the upper integer >>>math.ceil(81.3)
82
3 floor( ) Returns the lower integer >>>math.floor(81.3)
81
4 pow( ) Calculate the power of a number >>>math.pow(2,3)
8.0
5 fabs( ) Returns the absolute value of a number >>>math.fabs(-5.6)
5.6
6 exp( ) Returns the e raised to the power i.e. e3 >>>math.exp(3)
20.085536923187668

(b) Function in random module:


random module has function random ( ) ,randint( ).

randint( ) function generates the random integer values including start and
end values.
Syntax: randint(start, end)
It has two parameters. Both parameters must have integer values.

Example:
import random n=random.randint(3,7)
*The value of n will be 3 to 7.
3 User Defined Functions: The functions those are defined by the user are called
user defined functions.

The syntax to define a function is:

def function-name ( parameters) :


#statement(s)

Where:

 Keyword def marks the start of function header.


 A function name to uniquely identify it. Function naming follows the same rules of writing
identifiers in Python.
 Parameters (arguments) through which we pass values to a function. They are optional.
 A colon (:) to mark the end of function header.
 One or more valid python statements that make up the function body. Statements must have same
indentation level.
 An optional return statement to return a value from the function.

Example:

def display(name):
print("Hello " + name + " How are you?")

display(“Ravi”)
output- HelloRaviHow are you?

e.g def add(a,b ) :


c=a+b
print(“Sum of 2 numbers=”, c)

x = int(input (“Enter first number”))


y= int(input(“Enter second number”))
add(x, y )
Output if x=2, y=3
Sum of 2 numbers= 5

1 What is the general format of user defined functions in python?

def <function name> ([Parameter]) :


<stm1>
<stm2>
<stm3>
Function header : The first line of a function definition that begins
def add(a,b ) :
c=a+b
print(“Sum of 2 numbers=”, c)

x = int(input (“Enter first number”))


y= int(input(“Enter second number”))
add(x, y )

Function header- def add(a,b ) :


Parameters – a,b
Function body – c= a+b
print(“Sum of 2 numbers=”, c)

2 What is the difference between parameters ( Formal Parameters )and


arguments( Actual Parameters)?

Parameters( Formal Parameters ) Arguments( Actual Parameters)


Values provided in function header Values provided in function call
Eg;) def add(a,b ) :
C=a+b
print(“Sum of 2 numbers=”, C)

x = int(input (“Enter first number”))


y= int(input(“Enter second number”))
add(x, y )
a,b are parameters.
x,y are arguments.

3 What are the different types of parameters / Formal arguments Python


support?

1.)Positional arguments
2.)Default arguments
3.)Keyword (or named ) arguments

1) Positional arguments
The values of arguments are matched with parameters position wise.
Eg.) If the function header is
def check(a, b, c):

if the function call is


check(x, y, z)
Then a will get value of x, b will get value of y, c will get value of z

2) Default Arguments
A parameter having default value in the function header is known as a default
parameter.

Eg 2)
If the function header is
def interest(P, T, R =10):
If the function call is
SI = interest (5400,2) #Third argument is missing. Here P gets value
# of 5400, T gets value
#of 2 and R takes the default value 10
If the function call is
SI = interest(6100,3,15) #Here P gets value of 6100,T gets value of 3
#and R gets value of 15

3) Keyword (Named Arguments)


Specifying names for the values being passed in the function call is called
keyword arguments.

Eg)
def interest (P, R,T ) :
SI = (P*R*T) /100
print(“Simple Interest =”, SI)

interest(P=2000,T=2,R=10)
interest(T=4,P=2600,R=9)
interest(T=2,R=12,P=2000)
In a function call statement:
*An argument list must first contain positional arguments followed by any
keyword arguments.(If positional arguments comes after keyword arguments
then it will result into error)

4 Consider the given function definition


def Interest(princ, cc , time = 2, rate = 9):
SI = (princ *time*rate)/100
return SI
Identify the valid function call statements given below:

Function call statement Legal/ Reason


Illegal
Interest(princ=3000,cc=5) Legal Non default values provided as
named arguments
Interest(rate=0.12,princ=5000,cc=4) Legal Keyword arguments can be used in
any order and for the argument
skipped, there is a default value
Interest(cc=4,rate=0.12,princ=5000) Legal With keyword arguments, we can
give values in any order
Interest(5000,3,rate=0.12) Legal Positional arguments before
keyword arguments: for skipped
argument there is a default value
Interest(rate=0.05, 5000, 3) Illegal Keyword arguments before
positional arguments
Interest(5000,princ=300,cc=2) Illegal Multiple values provided for princ;
once as positional argument and
again as keyword argument
Interest(5000,principal=300,cc=2) Illegal Undefined name principal is used
Interest(500, time = 2, rate = 0.05) Illegal A required argument cc is missing

5 What are the different types of functions based on returning values?

There are two types of functions


1) Functions returning some value (non void functions / fruitful
functions)
2) Functions not returning any value ( void functions / non fruitful
functions)
6 Define Scope of Variables
Parts of a program within which a variable is legal and accessible is called the
scope of the variable.

7 What are the different kinds of scopes in Python?

Global Scope and Local Scope


Global Scope : A name declared in top level segment ( _ main _ ) of a
program is said to have a global scope and is usable inside the whole
program and all blocks ( functions , other blocks) contained within the
program.
Local Scope : A name declared in a function body is said to have local
scope. It can be used only
within this function and the other blocks contained under it.

8 What do you mean by Lifetime of a variable?


The time for which a variable remains in memory is called Lifetime of a
variable.

9 Differentiate between Local variable and Global variable


Local Variable Global Variable
It is a variable declared within a It is a variable declared outside all the
function or within a block. functions
It is accessible only within a function / It is accessible throughout the program
block in which it is declared.
Life time of a local variable is their Life time of a global variable is entire
function’s run. program run.
Eg) Eg)
def calsum(x , y ) : def calsum(x , y ) :
z=x+y z=x+y
return z return z

num1 = int(input (“Enter first number”)) num1 = int(input (“Enter first number”))
num2 = int(input(“Enter second num2 = int(input(“Enter second
number”)) number”))
sum = calsum(num1, num2 ) sum = calsum(num1, num2 )
print(“Sum of 2 numbers=”, sum) print(“Sum of 2 numbers=”, sum)

Local variables are x, y, z Global variables are num1,num2 ,sum


10 What is the scope resolving rule of Python?
Python resolves the scope of a variable using LEGB rule.

11 How to use the global variable inside local scope when the same variable
name is having local scope as well as global scope?/ How can you access a
global variable inside the function , if function has a variable with same
name?

The global statement is used to do it.


Eg)
def state1( ) :
global tigers #This is an indication that not to create local variable
#with the name tigers
tigers = 15 #rather use global variable tigers
print(tigers)

tigers = 95
print(tigers)
state1( )
print(tigers)

o/p
95
15
15

12 Differentiate between passing immutable arguments and mutable arguments


in function call.

When immutable type arguments (eg. Numbers, strings etc) are passed in a
function call then any change in its value will also change the memory
address it is referring to. So after returning from the function definition the
originally passed variable remains unchanged.

When mutable type arguments (eg. List or Dictionaries) are passed in a


function call then any change in the value of mutable type will not change
the memory address of the variable. So after returning from function
definition, it shows the changed value.
13

a) def main( )
print(“hello”)

b) def func2( ) :
print(2 + 3)

c) def compute( ) :
print(x * x)

d) square( a )
return a*a

14

15
16

17

18

global statement is used when we want to modify a global


variable inside a local scope.

o/p
1000
num1= 1.0 num2= 100

Once a variable is defined global in a function, it cannot undone.


Thus the global statement is not recommented.

19 a)A name inside the parentheses of a function header that can


receive a value.
b) An argument passed to a specific parameter using the
parameter name.
c) A value passed to a function parameter.
d) A value assigned to a parameter name in the function header.
e) A value assigned to aparameter name in the function call.
f) A name defined outside all function definitions.
g) A variable created inside a function body.

ANSWER
a)Parameter
b)Keyword argument
c)Argument
d)Default parameter
e) Keyword argument
f) Global variable
g) Local variable

20

ANSWER
total = 0
ANSWER

21
ANSWER
i)
1
1
1

ii)
1
10
1

iii)
1
10
10

iv)
Hello there!

22

def addEm(x,y,z):
return x+y+z
print(“the answer is”, x+y+z)

ANSWER
23

ANSWER
i) 5 times 5 =25
ii) 25

24

ANSWER
25

ANSWER
i)
[1,2,3,[4]] [1,2,3,[4]]

ii)
[23,35,47,[49]]
23 35 47 [49]
True
26

27
28

29
30

31
32

33

34

35

a) .mod b) .lib c) .code d) .py


36

37

38
39

40

You might also like