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

Answer Sheet - 38 Functions and Procedures

This document discusses functions and procedures in computer programming. It provides examples of Python programs that use functions to check if a number is prime, find the smallest of two numbers, and find the smallest of three numbers. It also includes end of topic questions about the advantages of functions and procedures, their definitions, how the function call process works, and the differences between functions and procedures. Finally, it gives a Python program that uses a function to check if a string is a palindrome.

Uploaded by

Mugerwa Charles
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
175 views

Answer Sheet - 38 Functions and Procedures

This document discusses functions and procedures in computer programming. It provides examples of Python programs that use functions to check if a number is prime, find the smallest of two numbers, and find the smallest of three numbers. It also includes end of topic questions about the advantages of functions and procedures, their definitions, how the function call process works, and the differences between functions and procedures. Finally, it gives a Python program that uses a function to check if a string is a palindrome.

Uploaded by

Mugerwa Charles
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Teach Computer Science

Functions and
procedures

teachcomputerscience.com
1.

Activity

teachcomputerscience.com
Activity-1
Duration: 10 minutes

1. You have created a Python program to find out whether a


number is prime or not in topic “Basic programming constructs”.
Now, implement the same program using function or
procedure.

Here is a program using procedure. Accept student’s answer if


a function is used.
def prime(num):
# prime numbers are greater than 1
if num > 1:
# check for factors
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
break
else:
print(num,"is a prime number")

# if input number is less than


# or equal to 1, it is not prime
else:
print(num,"is not a prime number")
number = int(input("Enter a number: "))
prime(number)

2. Which of the two: functions or procedures have you


implemented? Why?

Procedure. Now values are returned by the sub-program.


Accept student’s answer is function is used. Functions must
return a value.

teachcomputerscience.com
Activity-2
Duration: 15 minutes

1. Create a python program to find the smallest of two numbers.


Implement functions in your program.

def small_2(p,q):
if p<q:
return p
else:
return q
x= int(input("Enter first number: "))
y= int(input("Enter second number: "))
print ("The smallest number is: " , small_2(x,y))

2. Extend the program written above to find the smallest of three


numbers.

def small_2(p,q):
if p<q:
return p
else:
return q

def small_3(a,b,c):
return small_2(c, small_2(a,b))

x= int(input("Enter first number: "))


y= int(input("Enter second number: "))
z= int(input("Enter third number: "))
print ("The smallest number is: " , small_3(x,y,z))

teachcomputerscience.com
2.
End of topic
questions

teachcomputerscience.com
End of topic questions
1. What are the advantages of using functions and procedures in
programs?
• The overall size of code is reduced
• improves the readability of the program
• Reduces the time taken to write and test the program.
• Reduces the time taken to debug the program.
• Use of in-built functions saves time and reduces the possibility
of errors.
2. What are functions?
Functions are subprograms that can be called whenever required
from the main program.
3. Explain the function-call process in detail.
i. The main program executes until it reaches a function call
statement.
ii. Control and inputs are passed to function.
iii. The function code is executed.
iv. Control transferred to main program.
v. Output from function returned to function call statement.
vi. The main program continues the execution of the program.
4. What parameters are included in function definition?
Name of function, inputs to be passed to the function, function
code and outputs that are produced by the function
5. How are procedures different from functions?
Procedures like functions are sub-programs called from the main
program but do not return any values. Functions always return
value(s).

teachcomputerscience.com
End of topic questions
6. Using functions or procedures, write a Python program to check
whether the given string is a palindrome or not.
def palindrome(string):
k = ""
for i in string:
k=i+k
if (string==k):
print("Yes, this string is a palindrome")
break
else:
print("Not a palindrome")

text=str(input("Enter text: "))


palindrome(text)

teachcomputerscience.com

You might also like