Abc Assignment Help

ISY104 User Defined Functions in Python Program: Activity Sheet Week 5 Assessment Answer

ISY104 – Foundations of Programming Week 5 – User Defined Functions

Objective:

This week we will work on user defined functions

During the workshop we discuss the answers to question 1 on pages 1 and 2 and then work on solving the questions in the latter part of page 2.

1. State the reason for the output generated by the following program segments. It is important to remember the terminology used in the reasoning. The terms are shown in bold text in the “reasoning” column in the table below.


Code SegmentsAnswerReasoning
1def foo(): print(2+3)


foo()
#01
#02


#03
5The first statement to that is executed in 03. It invokes a function named foo. This causes the control to flow to the foo function definition on line 01 and 02. Line 02 has a print function that displays 5. By the way the first line of a function definition is called
function header. That is line 01 represents the function header.
2def foo(): print(2+3)#01
#02
Nothing will be displayedThough this has foo function definition as
above, it is not invoked. So, no part of the program will be executed.
3def foo(): print(2+3)


foo()
foo()
#01
#02


#03
#04
5
5
This is same as the first question except foo() is invoked on line 03 and again on line
04. When line 03 is executed, the control transfers to the function definition to display 5. Then the control returns to line 04
which again causes the control to transfer to the function definition to display 5 again.
4
def foo():#01
print(2+3)#02


for x in range(2):#03 foo()#04
5
5
This is same as question 3, except we are using a for loop to repeat the invocation of foo two times.
5def foo(a, b): print(a+b)


foo(2, 3)
#01
#02


#03
5First line 03 is executed to invoke the foo function. This time though, the foo function call passes two values, namely 2 and 3. The general terminology of values passed to a function when it is invoked is, arguments.
When the control transfers to the foo function definition, the first argument, namely 2 is transferred to variable and the second argument, namely 3 is transferred to variable in line 01. The variables, if any in a
function header are called, formal parameters.


6def foo(a, b):#01 print(a+b)#02


foo(2, 3)#03
foo(3, 7)#04
5
10
The first foo function call on line 03 has arguments, 2 and 3. So is initialized to 2 and is initialized to 3 resulting in the display of 5 when the print statement is invoked. The second foo function call on line 04 has
arguments 3 and 7. So will be initialized to 3 and will be 7 in the function header.
7def getName():#01 n = input(“Name: “)#02 return n#03


name = getName()#04 print(name)#05
Chris, If the user enters, Chris, at runtimeThe getName function uses a return statement to return a value. Whenever a return statement executes, it returns the control and a value (if any) to the caller. In our case, getName is called at line 04 and
the returned value is assigned to the variable, name.
8
def isOdd(a):#01
return a%2==1#02


print(isOdd(2))#03
print(isOdd(3))#04




False True
isOdd returns True if parameter a is odd and returns False otherwise. On line 03 isOdd is called with 2 as the argument. 2 is not odd so it returns False. On line 04, 3 is passed as an argument, so it returns True.
9
def getMax(x,y):#01 if x>y:#02
return x#03
else:#04
return y#05

print(getMax(1,2))#06 print(getMax(4,3))#07
print(getMax(5,5))#08
2
4
5
The first call to getMax on line 06, initiatizes parameter to 1 and to 2. On line 02, 1>2 is false, so the else part is executed returning which is 2.
In the second call to getMax, x gets initialized to 4 and y to 3. On line 02, 4>3 is True, so line 03 is executed, returning x which is 4.


Similar reasoning applies to the third call.
1
0
def maxOf3(x,y,z):#01 m=getMax(x,y)#02
m=getMax(m,z)#03
return m#04
print(maxOf3(1,2,3)) #05 print(maxOf3(3,1,2))#06 print(maxOf3(3,2,1))#07
3
3
3
maxOf3 returns the max of three numbers. maxOf3 calls the getMax function defined above, twice, first to get the max of the first two numbers and secondly to get the max of the




You will find solutions to the following with the help of your tutor.

Activity – 1:

A university decides that if a graduate student has achieved 120 credit points and has got a minimum of GPA as 2.0 then the student will get an unconditional offer to their masters by research course. If the student has achieved more than or equal to 150 credit points or has at least a GPA or 3.5 then a "conditional offer" will be given while in all other cases the student will be rejected. Write a python program using a user-defined function that accepts the credit points and GPA as parameters and displays accordingly the offer type.

Activity – 2:

A software company sells a package that retails for $99. Quantity discounts are given according to the following table:

QuantityDiscount
10-1910%
20-4920%
50-9930%
100 or more40%

Write a program that uses two user defined functions, one for reading the number of packages purchased and the other for calculating and displaying the amount of the discount (if any) and the total amount of the purchase after the discount.

Activity – 3:

Write a user-defined function that adds the first 10 numbers (1 to 10) and returns the sum. Invoke the function two times and display the sums.

Activity - 4:

Write a python program that asks the user the sales amount and the commission rate and the program displays the commission accrued and the program keeps on asking the user this data as long they want to continue testing. You have to design appropriate functions to perform the activity.

Activity - 5:

Write a user defined function that takes a positive number as an argument and returns its digits in reverse order. E.g. if the number is 35768, it must return 86753.

Activity - 6:

Write a user defined function that returns the count of all odd number in a list passed as an argument. Invoke the function using a list of your choice. Display the count.

Other interesting questions: https://www.w3resource.com/python-exercises/python-  functions-exercises.php

Answer

For solution, connect with our online professionals. 

Customer Testimonials