ISY104 – Foundations of Programming Week 5 – User Defined Functions
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.
|1||def foo(): print(2+3)|
|5||The 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.
|2||def foo(): print(2+3)||#01|
|Nothing will be displayed||Though this has foo function definition as|
above, it is not invoked. So, no part of the program will be executed.
|3||def foo(): print(2+3)|
|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.
for x in range(2):#03 foo()#04
|This is same as question 3, except we are using a for loop to repeat the invocation of foo two times.|
|5||def foo(a, b): print(a+b)|
|5||First 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 a and the second argument, namely 3 is transferred to variable b in line 01. The variables, if any in a
function header are called, formal parameters.
|6||def foo(a, b):#01 print(a+b)#02|
|The first foo function call on line 03 has arguments, 2 and 3. So a is initialized to 2 and b 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 a will be initialized to 3 and b will be 7 in the function header.
|7||def getName():#01 n = input(“Name: “)#02 return n#03|
name = getName()#04 print(name)#05
|Chris, If the user enters, Chris, at runtime||The 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.
|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.|
def getMax(x,y):#01 if x>y:#02
|The first call to getMax on line 06, initiatizes parameter x to 1 and y to 2. On line 02, 1>2 is false, so the else part is executed returning y 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.
|def maxOf3(x,y,z):#01 m=getMax(x,y)#02|
print(maxOf3(1,2,3)) #05 print(maxOf3(3,1,2))#06 print(maxOf3(3,2,1))#07
|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:
Activity – 2:
A software company sells a package that retails for $99. Quantity discounts are given according to the following table:
|100 or more||40%|
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
For solution, connect with our online professionals.