Download presentation
Presentation is loading. Please wait.
Published byAlvin Fisher Modified over 8 years ago
1
Today… Functions, Cont.: –Designing functions. –Functional Decomposition –Importing our own module –A demo: Functional solution to assignment 2. Winter 2016CISC101 - Prof. McLeod1
2
2 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 2016
3
CISC101 - Prof. McLeod3 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 2016
4
CISC101 - Prof. McLeod4 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 2016
5
CISC101 - Prof. McLeod5 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 2016
6
CISC101 - Prof. McLeod6 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 2016
7
CISC101 - Prof. McLeod7 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 2016
8
CISC101 - Prof. McLeod8 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 2016
9
CISC101 - Prof. McLeod9 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 2016
10
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. CISC101 - Prof. McLeod10Winter 2016
11
CISC101 - Prof. McLeod11 Example – Washing Machine Control Top-most level: 1.Connect wasther to 110V AC, hot/cold water supply and drain. 2.Place clothes and soap in washer. 3.Washer removes dirt from clothes. 4.Remove clothes containing a minimal amount of water and dirt. We are interested in step 3.
12
Winter 2016CISC101 - Prof. McLeod12 Example, Cont. – Second Level Step 3 on the previous slide decomposed: 1.Agitate clothes in soapy water. 2.Remove soapy water. 3.Rinse with water. 4.Remove as much water as possible from clothes.
13
Winter 2016CISC101 - Prof. McLeod13 Example, Cont. – Third Level Step 1 (only) from the previous slide: 1.Lock washer door. 2.Fill machine with water of specified temperature. 3.Activate agitator motor to specified rate. 4.Agitate for specified time period.
14
Winter 2016CISC101 - Prof. McLeod14 Example, Cont. – Fourth Level Step 2 (only) from the previous slide: 1.Query front panel settings for water temperature. 2.Depending on settings, open cold valve only, hot valve only or both. 3.If water is not flowing activate alarm. 4.Close water input valves when level sensor indicates desired water level has been obtained.
15
Winter 2016CISC101 - Prof. McLeod15 Example, Cont. – Fourth Level, Pseudocode desiredTemp = getTempSetting(frontpanel) if desiredTemp == hot openValve(hot) else if desiredTemp == cold openValve(cold) else openValve(hot) openValve(cold) if not waterFlowing() sendAlarm(waterError) while not waterFull(desiredLevel) if waterFull(desiredLevel) closeValves()
16
Winter 2016CISC101 - Prof. McLeod16 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.
17
CISC101 - Prof. McLeod17 Two Ways to Use import To tell the interpreter that we wish to use module functions, you need to import the module as in: import math But, if you don’t want to have to say math.sqrt() (ie. you don’t want to have to name the module all the time), you can use a different kind of import statement: from math import * Or, to just import the sqrt() function for example: from math import sqrt Winter 2016
18
CISC101 - Prof. McLeod18 Module Functions, Cont. If you have used from math import *, for example, then you can invoke any of the math functions directly: print(sqrt(2)) Instead of: print(math.sqrt(2)) Winter 2016
19
Example – The myInput Module Put our robust input function in a code module and import it into other programs, just like we have been doing with the turtle and math modules. Create myInput.py, which contains only the getInt() and getFloat() functions and does not need to have a main() function. In another program use “import myInput”. Note the creation of a byte code version (*.pyc) of the new module… See myInput.py and FunctionalMortgageCalculatorV2.py. CISC101 - Prof. McLeod19Winter 2016
20
Aside – Coding Modules Our new module could certainly contain other useful input functions (file input, for example). If you did write a main() in the module, as we have been doing and put in a call to main() in column 1, then this function would run every time the module is imported. CISC101 - Prof. McLeod20Winter 2016
21
Another Functional Program See the game of pig assignment solution as a functional program. FunctionalGameOfPig.py Could you break this up any further? Winter 2016CISC101 - Prof. McLeod21
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.