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

Slides:



Advertisements
Similar presentations
Lecture 4 Basic Scripting. Administrative  Files on the website will be posted in pdf for compatibility  Website is now mirrored at:
Advertisements

Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. How To Program.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
CS 201 Functions Debzani Deb.
Main task -write me a program
Introduction to Methods
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
COMP 171: Functions, for loops and range John Barr Section 03 Slides by Toby Dragon.
Chapter 6 Functions 1. Opening Problem 2 Find the sum of integers from 1 to 10, from 20 to 37, and from 35 to 49, respectively.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
1 CS161 Introduction to Computer Science Topic #9.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Programming Fundamentals Enumerations and Functions.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
Today… The for loop. Introducing the Turtle! Loops and Drawing. Winter 2016CISC101 - Prof. McLeod1.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Today… Strings: –String Methods Demo. Raising Exceptions. os Module Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Today protected access modifier Using the debugger in Eclipse JUnit testing TDD Winter 2016CMPE212 - Prof. McLeod1.
PROGRAMMING USING PYTHON LANGUAGE ASSIGNMENT 1. INSTALLATION OF RASPBERRY NOOB First prepare the SD card provided in the kit by loading an Operating System.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Today… Functions, Cont.: –Designing functions. –Functional Decomposition –Importing our own module –A demo: Functional solution to assignment 2. Winter.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Loops BIS1523 – Lecture 10.
Topic: Functions – Part 2
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
CISC101 Reminders Assn 3 due Friday, this week.
Functions Inputs Output
Call Stacks, Arguments and Returns
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week.
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Fall 2018 CISC124 12/1/2018 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
CISC124 Labs start this week in JEFF 155.
Winter 2019 CISC101 2/17/2019 CISC101 Reminders
CISC124 Labs start this week in JEFF 155. Fall 2018
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CMPE212 – Reminders The other four assignments are now posted.
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
CISC101 Reminders Quiz 1 marking underway.
CISC101 Reminders All assignments are now posted.
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due today.
CMPE212 – Reminders Assignment 2 due next Friday.
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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