Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.

Similar presentations


Presentation on theme: "Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1."— Presentation transcript:

1 Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1

2 Part A, Assignment 1 – Mortgage Calculator Let’s add this code to the solution for this part of the assignment to make input completely robust. Compensate for non-numeric input as well as checking for the number being between legal limits. See: RobustMortgageCalculator.py Can you crash this program? CISC101 - Prof. McLeod2Winter 2016

3 Mortgage Calculator, Cont. Do the three sections of code that obtain the input look similar to you? What are the differences between these three sections of code? The prompt and the two limits. It would be nice if we only needed to write this section once! CISC101 - Prof. McLeod3Winter 2016

4 Functional Input Version Move one copy of this input section into a separate function. Make the function generic enough so that it can work for all three inputs. The different prompts and limits will be supplied as arguments to the function. The function will return the number obtained. See FunctionalMortgageCalculator.py CISC101 - Prof. McLeod4Winter 2016

5 Functional Input Version, Cont. Shorter program! Input code is all in one place – changes only need to be made in one place, instead of three places. Prevents repetitious code. Our new function could be used by other programs: Put it in a code module and import it into other programs, just like we have been doing with the turtle and math modules. (Later) CISC101 - Prof. McLeod5Winter 2016

6 Function Syntax 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 2016CISC101 - Prof. McLeod6

7 Function Mechanics When invoking a function, you supply arguments 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. Winter 2016CISC101 - Prof. McLeod7

8 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. CISC101 - Prof. McLeod8Winter 2016

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

10 CISC101 - Prof. McLeod10 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 2016

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

12 CISC101 - Prof. McLeod12 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 2016

13 CISC101 - Prof. McLeod13 A Simple Class # A very simple class with one method: class myClass(object) : def aMethod() : for i in range(10) : print(i, end=', ') print("\nAll done!") def main() : myClass.aMethod() main() Winter 2016

14 CISC101 - Prof. McLeod14 Modules and Packages A module (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 2016

15 CISC101 - Prof. McLeod15 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

16 CISC101 - Prof. McLeod16 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

17 CISC101 - Prof. McLeod17 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

18 CISC101 - Prof. McLeod18 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

19 CISC101 - Prof. McLeod19 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 I/O. By convention, main() should always be the starting point of your program. Winter 2016

20 CISC101 - Prof. McLeod20 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

21 CISC101 - Prof. McLeod21 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

22 CISC101 - Prof. McLeod22 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 –Slower? (not much…) Winter 2016

23 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. McLeod23Winter 2016


Download ppt "Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1."

Similar presentations


Ads by Google