Download presentation
Presentation is loading. Please wait.
1
Week 2 Lab2 Practice Dina A. Said dasaid@ucalgary.ca
2
Functions: Parameter Passing To pass a parameter to a function, define it as a variable in the function declaration e.g. def square(length) At run time, call the function with the value of this parameter e.g. square(6) Example: def printNameID(name,ID): print name print ID At the command window write: printNameID(“John”,122898) 3/18/2009CPSC203- Week2- Lab2
3
Functions: return value Your function may return a value using return command, e.g. return 6 return sum Note that, you should return only one value If a function returns a value, you can use it in assignment e.g. F=mySum() Now, you have defined a variable F that will holds the value that the function mySum() returns 3/18/2009CPSC203- Week2- Lab2
4
Example 3/18/2009CPSC203- Week2- Lab2 def mySum(): sum=0 for x in (1,2,3): sum=sum+x return sum In command window: mySum() F=mySum() Print F
5
Exercise Modify the previous function to return the summation of numbers in a range starting from x to y, where x and y are parameters passed to your function Test your function with mySum(1,3) You answer should be 6 Note: If the range is required to be inclusive, your range should be (x,y+1) However, if the range is required to be exclusive, your range should be (x+1,y) The default is inclusive 3/18/2009CPSC203- Week2- Lab2
6
3/18/2009CPSC203- Week2- Lab2 def mySum(x,y): sum=0 for no in range (x,y+1): sum=sum+no return sum Note that you can’t have two variables with the same name
7
Example Write a function returns the multiplication of numbers in range starting from x to y. Test your function with myMul(2,3) 2*3=6 myMul(2,6) 2*3*4*5*6=720 3/18/2009CPSC203- Week2- Lab2
8
3/18/2009CPSC203- Week2- Lab2 def myMul(x,y): mul=1 for n in range (x,y+1): mul=mul*n return mul Initialization
9
Exercise Write a function that returns the Permutations of a given number, where the permutation of x is calculated as 1*2*…*x. For example, the permutation of 6 is calculated as 1*2*3*4*5*6=720 and the permutation of 7 is calculated as 1*2*3*4*5*6*7=5040 Hints: Think about the range of your loop from 1 to the number Use multiplication What value should you initialize the permutation with? 3/18/2009CPSC203- Week2- Lab2
10
Exercise Modify the permutation program to check first if the given number if greater than zero. You should print error message otherwise. What is the return value in case of error message? 3/18/2009CPSC203- Week2- Lab2
11
3/18/2009CPSC203- Week2- Lab2 def perm(x): if (x<=0): print "Error" return -1 else: result=1 for n in range (1,x+1): result=result*n return result
12
Example Write a function that accepts four numbers and returns the maximum of them 3/18/2009CPSC203- Week2- Lab2 def myMax(x,y,z,w): max=x for n in (y, z, w): if (n>max): max=n return max max=x=3 n=y=6 is n>max? yes max =6 n=z=2 is n>max? No do nothing n=w=9 is n>max? yes max =9 myMax(3,6,2,9) X=3 y=6 z=2 w=9
13
Exercise Write a function that accepts 3 numbers and returns the minimum of them Test your program with myMin(9,5,2) 2 myMin(3,4,10) 3 myMin(2,-4,8) -4 3/18/2009CPSC203- Week2- Lab2
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.