Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week.

Similar presentations


Presentation on theme: "CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week."— Presentation transcript:

1 CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week.
Winter 2018 CISC101 12/1/2018 CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week. Do exercises 5 and 6. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

2 Today Building Functions, Cont. Winter 2018 CISC101 - Prof. McLeod

3 Function Syntax We have already been defining a function – main().
def fcnName (no parameters, one or many parameters separated by commas) : # code indented inside the function, parameters being # used anywhere inside the function # including return statement(s): return nothing or something We have already been defining a function – main(). Winter 2018 CISC101 - Prof. McLeod

4 Modular Version of the “Game of Pig”
Take a critical look at the Assignment 2 sample solution. Can the use of functions improve this program? See FunctionalGameOfPig.py. Winter 2018 CISC101 - Prof. McLeod

5 Function Mechanics When invoking a function, you may need to supply argument(s) inside the ( ) after the function name. These arguments are mapped to the function’s parameters, which hold the argument values for use inside the function. A function may return a value (or a collection of values) which will end up being used in the code that invoked the function. A function is one aspect of code “Modularity”. Winter 2018 CISC101 - Prof. McLeod

6 Modular Programming Programs get large!
CISC101 Modular Programming Programs get large! We need ways of organizing all our own code and the code that is already written that we use. Avoid repetition of code, make code easy to find, to debug and to distribute. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

7 Modular Programming, Cont.
There are many layers to how code can be grouped together: PACKAGE MODULE MODULE MODULE CLASS ATTRIBUTES METHODS CLASS ATTRIBUTES METHODS FUNCTION FUNCTION IMPERATIVE CODE Winter 2018 CISC101 - Prof. McLeod

8 Operational Code There are three places where you can put code that does something: “Imperative” code: In our programs so far, these would be lines like: main() and def main() : The code is not inside a function, and as a result is written starting in the leftmost column. See the next slide for an example of an imperative program: Winter 2018 CISC101 - Prof. McLeod

9 Imperative Program or “Script”
# This is the most simple kind of program # you can write! for i in range(1, 11) : print(i, end=',') Displays: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, Winter 2018 CISC101 - Prof. McLeod

10 Code in main() # This is the kind of program we have been # writing, so far: def main() : for i in range(10) : print(i, end=', ') print("\nAll done!") main() We have been writing single function programs until now. Winter 2018 CISC101 - Prof. McLeod

11 A Simple Class Invoked: Displays:
# A very simple class with one method: class MyClass(object) : def aMethod() : for i in range(100, 1001, 100) : print(i, end=', ') print("\nMethod is finished") Invoked: MyClass.aMethod() Displays: 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, Method is finished Winter 2018 CISC101 - Prof. McLeod

12 Module Example See moduleExample.py and ImportModuleExample.py.
Note the creation of a “byte-code” version of our little module in the folder __pycache__. Winter 2018 CISC101 - Prof. McLeod

13 Modules and Packages Modules (which we have been building – we just didn’t know it!) can contain any or all of the following: Imperative code Functions Classes A package is a collection of modules. To obtain all the code defined in a module you import it, as we have been doing. Any imperative code in the module is executed when the module is imported. Everything else is just loaded into memory. Winter 2018 CISC101 - Prof. McLeod

14 What is a Function? A function contains code that is isolated from other functions except for a designed interface: The interface consists of parameters that will contain supplied arguments to be used in a function and a return value that comes out. You can have as many parameters as you want (or none at all) and can return nothing or a single thing. The single “thing” can be a tuple of multiple values. Winter 2018 CISC101 - Prof. McLeod

15 The Advantages Each function is a building block for your program.
Construction, testing and design is easier. Functions avoid code duplication. Functions make re-use of your code more likely. Well written functions reduce the need for internal comments. Winter 2018 CISC101 - Prof. McLeod

16 Designing a Function A function should only do one thing. If you describe the function and need to use the word “and”, then it is probably doing more than one thing. Try to keep the parameter list short – 3 or fewer parameters, if possible. Or take advantage of default arguments (more on this later). The function should be short: in the range of 1 to 15 lines ideally. Be prepared to re-structure a working program to get a better design. Winter 2018 CISC101 - Prof. McLeod

17 Designing a Function, Cont.
Choose good, descriptive function and parameter names, so it is obvious what the function is doing. If you only need to add a bit more code to make your function more universally applicable – do it! Try to always check all your parameter values for legality. Winter 2018 CISC101 - Prof. McLeod

18 Designing a Function, Cont.
Try to get your function to return something rather than print something. Often main() will have to worry about all the console output. By convention, main() should always be the starting point of your program. Winter 2018 CISC101 - Prof. McLeod

19 Program Construction You can start from a functional decomposition of the problem. Write function headers and add parameters. Put the return value in a comment for now. Choose function names that describe what the function does. Make sure each function does one thing only. You may find a need for additional functions as you fill in the code for each function. Don’t be afraid to further decompose a function if it is getting too big or doing too many things. Winter 2018 CISC101 - Prof. McLeod

20 Testing and Debugging How can you test a one function program? In this case you need to wait until the entire program is complete before you can start testing! Or, you can choose to test one function at a time by: Putting a pass command in other empty (for the moment) functions. Add temporary code to main() to invoke your test function with test values and then display its return value. You know a failure is from the function under test! Small functions are much easier to debug!! Winter 2018 CISC101 - Prof. McLeod

21 You Decide! Multi-function PROs: CONs: Easier to design.
CISC101 You Decide! Multi-function PROs: Easier to design. Easier to construct. Easier to read. Requires fewer comments. Easier to test and fix. Easier to re-use. CONs: Longer program overall. Slower? (not much…) Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

22 Functional Decomposition
A common-sense idea: break a large problem down into smaller pieces! Keep breaking down the pieces until they are small enough to solve. Each piece will represent a function if the problem is a programming problem. Winter 2018 CISC101 - Prof. McLeod

23 Example – Washing Machine Control
Top-most level: Connect wasther to 110V AC, hot/cold water supply and drain. Place clothes and soap in washer. Washer removes dirt from clothes. Remove clothes containing a minimal amount of water and dirt. We are interested in step 3. Winter 2018 CISC101 - Prof. McLeod

24 Example, Cont. – Second Level
Step 3 on the previous slide decomposed: Agitate clothes in soapy water. Remove soapy water. Rinse with water. Remove as much water as possible from clothes. Winter 2018 CISC101 - Prof. McLeod

25 Example, Cont. – Third Level
Step 1 (only) from the previous slide: Lock washer door. Fill machine with water of specified temperature. Activate agitator motor to specified rate. Agitate for specified time period. Winter 2018 CISC101 - Prof. McLeod

26 Example, Cont. – Fourth Level
Step 2 (only) from the previous slide: Query front panel settings for water temperature. Depending on settings, open cold valve only, hot valve only or both. If water is not flowing activate alarm. Close water input valves when level sensor indicates desired water level has been obtained. Winter 2018 CISC101 - Prof. McLeod

27 Example, Cont. – Fourth Level, Pseudocode
desiredTemp = getTempSetting(frontpanel) if desiredTemp == hot openValve(hot) else if desiredTemp == cold openValve(cold) else if not waterFlowing() sendAlarm(waterError) while not waterFull(desiredLevel) if waterFull(desiredLevel) closeValves() Winter 2018 CISC101 - Prof. McLeod

28 Example, Cont. And so on… Continue the process until you have sufficient detail to write pseudocode. Put together a list of functions. List: The name of the function. The return type, if any. The required parameters. What the function does. What the function does if input is illegal. Winter 2018 CISC101 - Prof. McLeod


Download ppt "CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week."

Similar presentations


Ads by Google