0% found this document useful (0 votes)
35 views6 pages

Functions Revision Worksheet - AK

Uploaded by

shilpa07.nair
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)
35 views6 pages

Functions Revision Worksheet - AK

Uploaded by

shilpa07.nair
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/ 6

COMPUTER SCIENCE REVISION WORKSHEET

CHAPTER : FUNCTIONS

I. Function Chapter Vocabulary


1. Function
2. Inbuilt Functions
3. User defined Functions
4. Function definition
5. Function Header
6. DocString
7. Function Call
8. Formal Parameters/Arguments
9. Actual Parameters/Arguments
10. Variable Arguments (Example *n)
11. Passing Mutable and Immutable Parameters/Arguments
12. Positional / required arguments
13. Default arguments
14. Keyword / Named arguments
15. return statement
16. Fruitful Function
17. Non-Fruitful (Void) Functions
18. Scope of Variables
19. Lifetime
20. Local and global Scope
21. LEGB Rule
22. global keyword
II. Output Based Questions

1. def fun1(): 2. def fun1(m=8,n=10):


global s global x
print (s) x=4
y=6
def fun2(): print (m+n-x)
s="welcome" def fun2():
d=s+"students" x=5
return s return x
print(fun2())
s="WELCOME"
print(fun1()) a,b=5,10
print(fun2()) x=20
fun1(b)
OUTPUT print(x)

WELCOME OUTPUT
None 16
welcome 5
4

1
3. def f3(a,b): 4. def check(a):
global x,y for i in range(len(a)):
x=a+b a[i]=a[i]+5
a,y=a+x,a*x return a
print(a,b,x,y) b=[1,2,3,4]
f3(5,10) c=check(b)
print(f3(b=x,a=y)) print(c)

OUTPUT OUTPUT

20 10 15 75 [6, 7, 8, 9]
165 15 90 6750
None

5. def sayHello(): 6. def printMax(a, b):


print('Hello World!') if a > b:
sayHello() print(a, 'is maximum')
sayHello() elif a == b:
print(a, 'is equal to', b)
OUTPUT else:
print(b, 'is maximum')
File "<ipython-input-1-4e256937cb09>", printMax(3, 4)
line 3
print('Hello World!')
^
OUTPUT
IndentationError: expected an indented
block 4 is maximum

7. x = 50 8. def say(message, times = 1):


def func(x): print(message * times)
print('x is', x)
x=2 say('Hello')
print('Changed local x to', x) say('World', 5)
func(x)
print('x is now', x) OUTPUT
OUTPUT
x is 50
x is 50 Changed local x to 2
Changed local x to 2 x is now 50
x is now 50

9. def func(a, b=5, c=10): 10. def power(x, y=2):


print('a is', a, 'and b is', b, 'and c is', c) r=1
func(3, 7) for i in range(y):
func(25, c = 24) r=r*x
func(c = 50, a = 100) return r
print(power(3))
OUTPUT print(power(3, 3))
x is 50 OUTPUT
Changed local x to 2 9
x is now 50 27

2
11. def sum(*args): 12. def FunStr(S):
'''Function returns the sum of all Values''' T=""
r=0 for i in S:
for i in args: if i.isdigit():
r += i T=T+i
return r return T
print (sum.__doc__) X="PYTHON 3.9"
print (sum(1, 2, 3)) Y=FunStr(X)
print (sum(1, 2, 3, 4, 5)) print(X,Y,sep="*")

OUTPUT OUTPUT
Function returns the sum of all values PYTHON 3.9*39
6
15
Help on function sum in module __main__:

sum(*args)
Function returns the sum of all values

13. S="WELCOME" 14. V=50


def Change(T): def Change(N):
T="Hello" global V
print(T,end='@') V,N=N,V
Change(S) print(V,N,sep="#",end='@')
print(S) Change(20)
print(V)
OUTPUT
OUTPUT
PYTHON 3.9*39 20#50@20

15. def ListChange(): 16. V=25


for i in range(len(L)): def Fun(Ch):
if L[i]%2==0: V=50
L[i]=L[i]*2 print(V,end=Ch)
if L[i]%3==0: V*=2
L[i]=L[i]*3 print(V,end=Ch)
else: print(V,end='*')
L[i]=L[i]*5 Fun("!")
L=[2,6,9,10] print(V)
ListChange()
for i in L: OUTPUT
print(i,end="#")
25*50!100!25
OUTPUT

20#36#27#100#

3
III. Rewrite the following code in Python after removing all error(s) if any. Underline each
correction done in the code with appropriate explanation for the corrections done .

1.

CODE
#Error Q1
def findandprintsum():
# Identifiers cannot have special characters like &
count=sum=0
ans='y'
while ans=="y": #Comparison operator == must be used instead of assignment operator
num=int(input("enter a number :")) #identifier num not defined
if num < 0: #Statements should start on a newline
print("number entered is below zero. Aborting!")
break # : cannot be used along with break statement
sum=sum+num
count=count+1
ans=input("want to enter more numbers ?(y/n)..")
print("your entered", count,"numbers so far.")
print("sum of numbers entered is ",sum)

2. def increment(n):
n.extend(40)
return n[0],n[1],n[2],n[3]

L=[23,34,47]
m1,m2,m3,m4,m5=increment(L)
print(L(0:))
print(m1,m2,m3,m4)
print(L[3]=m4)

CODE

#Error Q2
def increment(n):
n.extend([40]) #Tn.extend(40) ypeError: 'int' object is not iterable
return n[0],n[1],n[2],n[3]

4
L=[23,34,47]
m1,m2,m3,m4=increment(L) #m1,m2,m3,m4=increment(L) ValueError: not enough values to
unpack (expected 5, got 4)
print(L[0:]) # print(L(0:)) SyntaxError: invalid syntax
print(m1,m2,m3,m4)
print(L[3],m4) # print(L[3],m4) Syntax Error assignment expression cannot be given in print
statmentExpression

3.

CODE
#Error Q3
def checksum(): # Syntax Error ( ) were missing
x=int (input("Enter the number")) # Logical Error String value must be converted into int
if x%2 == 0: #Syntax Error : Criteria must have two == signs for comparison
for I in range(2*x):
print(I) # NameError: name 'i' is not defined
else: # loop word must be removed
print("#")

5
4.

CODE
#Error Q4
total=0

# arg2 should have a default value before arg1 and colon missing
def sum(arg1,agr2=50):

total=arg1+agr2
print("Total :",total)
return total #use lowercase r for return statement

#__main__
ret=sum(10,20) #value can't be assigned to function call

print("Total :",total)

You might also like