Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC101 Reminders Assn 3 due tomorrow, 7pm.

Similar presentations


Presentation on theme: "CISC101 Reminders Assn 3 due tomorrow, 7pm."— Presentation transcript:

1 CISC101 Reminders Assn 3 due tomorrow, 7pm.
Winter 2018 CISC101 12/4/2018 CISC101 Reminders Assn 3 due tomorrow, 7pm. Quiz 3 next week. Topics in slides from last lecture. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

2 Today Building Functions, Cont.:
Go over function syntax in more detail. Keyword and Default Arguments. Making our input function even more robust and flexible. Winter 2018 CISC101 - Prof. McLeod

3 Invoking Functions - Review
Name the function and then use round brackets. The brackets can be empty or have one or more arguments inside. For example: print() Displays a linefeed on the console. print("Hello") Displays the string Hello on the console. print("Hello", "Alan") Displays Hello and then Alan, separated by a space on the console. Winter 2018 CISC101 - Prof. McLeod

4 Invoking Functions, Cont.
Arguments are separated by commas. Arguments can be: Literal values. Variables. Expressions. In the case of a variable or an expression, it is first evaluated to come up with the resulting value before it is fed into the function. Winter 2018 CISC101 - Prof. McLeod

5 Aside - Keyword Arguments
The previous print() examples used positional arguments. We have also done things like: print("Hello", "Alan", sep="\n") The two strings will be on different lines this time. The sep="\n" thing is called a keyword argument. We will learn more about keyword arguments and default arguments shortly! Winter 2018 CISC101 - Prof. McLeod

6 Function Returns A function may return something.
Functions that don’t return anything (like print(), for example) are sometimes called procedures or void functions. Can you think of some functions that return something? input() float() str() len() sorted() Winter 2018 CISC101 - Prof. McLeod

7 Function Returns This “thing” that is returned can be any Python type such as a string, a list, an int, a float, etc. It is also possible to return multiple things in Python: a, b, c = someFunction(arg1, arg2) But, is the a, b, c part three “things” or just one thing really? What type is it? Winter 2018 CISC101 - Prof. McLeod

8 A Function with Parameters
Here is a (useless) function that displays the higher of two numbers: def showHighest(num1, num2) : if num1 > num2 : print(num1, "is higher.") else : print(num2, "is higher.") Winter 2018 CISC101 - Prof. McLeod

9 A Function with Parameters, Cont.
So, when you invoke this (completely useless) function from within some other function (main() perhaps…), you need to supply two things for the parameters - you supply two arguments: showHighest(3.4, 6.7) The code in showHighest() runs and the larger number is displayed. Within showHighest(), num1 has the value 3.4 and num2 has the value 6.7 Winter 2018 CISC101 - Prof. McLeod

10 A Function with Parameters, Cont.
To put it another way: The positional arguments 3.4 and 6.7 have been mapped into the parameters num1 and num2. num1 and num2 are variables that have been created in the function’s parameter list and are local to the function. Winter 2018 CISC101 - Prof. McLeod

11 Functions Returning a Value
How can showHighest() be changed to return the highest number instead of printing it out? (It is kind of tacky to have functions print things instead of returning them - let main() do the printing!) def getHighest(num1, num2) : if num1 > num2 : return num1 else : return num2 Winter 2018 CISC101 - Prof. McLeod

12 Functions Returning a Value, Cont.
Or: def getHighest(num1, num2) : if num1 > num2 : return num1 return num2 Winter 2018 CISC101 - Prof. McLeod

13 Returning Values So, here are a few things you need to know about returning things: You can have as many return statements as you want in a function. If you don’t have a return statement, then your function does not return anything. It is invoked without expecting any value to come out of the function (no assignment required when invoking). Execution of a function stops as soon as you execute the return statement, even if there is code after the return statement. Winter 2018 CISC101 - Prof. McLeod

14 Jazzing Up the Parameter List
When using a function, you might not always need or wish to supply all possible parameters. Or, you might not want to have to worry about the order of the arguments supplied. The use of keyword (when invoking) and default (when defining) arguments allows you to enhance the flexibility of how your function is used. Winter 2018 CISC101 - Prof. McLeod

15 parameterName=argument
CISC101 Keyword Arguments Suppose you have a function with several parameters, but you don’t want to worry about supplying values in the matching order. You can use keyword arguments to supply the arguments in any order with the syntax: parameterName=argument in the parameter list. See KeywordArgumentsDemo.py Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

16 Keyword Arguments, Cont.
All positional arguments must come before keyword arguments. But, the keyword arguments can be in any order. Unless the function has default arguments, you must still supply arguments for each parameter. Style note: No spaces on either side of the equals sign. Winter 2018 CISC101 - Prof. McLeod

17 Default Arguments You can make a function a great deal more flexible by making it optional for the user to supply all the arguments. You do this by creating default arguments in your function definition statement. Default arguments must come after all positional parameters. The same syntax as for Keyword Arguments, but this time it is used in the def line instead of the invoking line. See DefaultArgumentsDemo.py Winter 2018 CISC101 - Prof. McLeod

18 Default Arguments, Cont.
You must decide which parameters are optional, if any. Then you must make assumptions to come up with values for those optional parameters. Supplying an argument value for a default argument replaces the default value. We will see quite a bit of this stuff when using Tkinter! Reduces the need for multiple function versions. Winter 2018 CISC101 - Prof. McLeod

19 Example: The print() BIF
We know that print() can have any number of positional arguments. It also has sep= and end= default arguments. They have been defaulted as sep=" " and end="\n" In other words the default separator between printed items is a space and a linefeed will be added to the output after the last item has been printed. You can only change these values using the keyword arguments. Winter 2018 CISC101 - Prof. McLeod

20 Example: Our Robust Input Function
So far, our input function has to have two limits defined for the legal low and legal high limits for the desired number. Suppose there is no upper or lower limit – how can you use this function? Set the two limits to some default value – but what should that value be? Your only choice is to set them to None. (This keyword can be used to create variables without actually assigning them a value.) See myInputAgain.py again and testMyInput.py. Winter 2018 CISC101 - Prof. McLeod

21 Our Robust Input Function, Cont.
Note that we could not use a variable set to None in a comparison (or in an expression that carries out a calculation). So, we had to alter the code in the function. Is it worth it? Only two things left to fix: What if the invoking code supplies a string for lowLimit or highLimit? What if the invoking code supplies a lowLimit > highLimit? Winter 2018 CISC101 - Prof. McLeod

22 Checking Argument Types
A disadvantage of loosely typed language like Python is that the interpreter cannot check to make sure that you are supplying the proper argument types to a function. Improper argument types will result in a run-time error unless the function itself checks. What should the function do if argument types are not correct? Winter 2018 CISC101 - Prof. McLeod

23 Checking Argument Types, Cont.
CISC101 Checking Argument Types, Cont. A function that expects a number for an argument might get a string (or something else!) instead. Ouch! So, a *very* robust function will check argument types. For example if numArg is supposed to be a number, check to see if: type(numArg) == type(1) or type(numArg) == type(1.1) or, simply: type(numArg) == int or type(numArg) == float is True. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

24 isinstance() Another BIF that you can use to check types. For example:
>>> aVal = 1234 >>> isinstance(aVal, int) True >>> isinstance(aVal, str) False Winter 2018 CISC101 - Prof. McLeod

25 Most! Robust Input Function
See the mrInput module, and the program testMrInput.py. If the lowLimit and highLimit values are reversed then just switch them. What to do if they are the same? Winter 2018 CISC101 - Prof. McLeod

26 Summary Keyword arguments allow you some flexibility in how you supply arguments when invoking a function. Default arguments are used in a function definition and assign default values to parameters that do not have to be mapped when the function is invoked. Other languages (like Java) use method overloading to provide this flexibility. Winter 2018 CISC101 - Prof. McLeod

27 Summary, Cont. Checking arguments can be a bit more strenuous in a dynamically typed language like Python – you need to check types as well as values. None can be assigned to a variable or a parameter when you cannot assign a real value. You can check for == None or != None, but that’s all you can do! Winter 2018 CISC101 - Prof. McLeod


Download ppt "CISC101 Reminders Assn 3 due tomorrow, 7pm."

Similar presentations


Ads by Google