CSc 110, Autumn 2017 Lecture 3: Functions.

Slides:



Advertisements
Similar presentations
Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading:
Advertisements

Building Java Programs Chapter 1 Introduction to Java Programming.
Java Programs + Algorithms
1 Procedural decomposition using static methods suggested reading:1.4.
1 Procedural decomposition using static methods. 2 Algorithms Recall: An algorithm is a list of steps for solving a problem. What is the algorithm to.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 1: Introduction to Java Programming.
Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Copyright 2010 by Pearson Education Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading:
Copyright 2008 by Pearson Education 1 Class constants and scope reading: 2.4 self-check: 28 exercises: 11 videos: Ch. 2 #5.
Copyright 2008 by Pearson Education Building Java Programs Chapter 1 Lecture 1-2: Static Methods, Avoiding Redundancy reading: self-check:
Ways to solve problems Top down approach – Break problem up into smaller problems Bottom up approach – Solve smaller problem and then add features – Examples:
Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Copyright 2008 by Pearson Education Building Java Programs Chapter 1: Introduction to Java Programming.
Main task -write me a program
Copyright 2008 by Pearson Education Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading:
Topic 3 static Methods and Structured Programming "The cleaner and nicer the program, the faster it's going to run. And if it doesn't, it'll be easy to.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 1: Introduction to Java Programming.
CS 112 Introduction to Programming Java Primitive Data Types; Arithmetic Expressions Yang (Richard) Yang Computer Science Department Yale University 308A.
Static methods. 2 Algorithms algorithm: a list of steps for solving a problem Example algorithm: "Bake sugar cookies" –Mix the dry ingredients. –Cream.
BUILDING JAVA PROGRAMS CHAPTER 1 INTRODUCTION TO JAVA PROGRAMMING.
CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Practice and Evaluation. Practice Develop a java class called: SumCalculator.java which computes a sum of all integer from 1 to 100 and displays the result.
Copyright 2010 by Pearson Education Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading:
Week 1 basic Python programs, defining functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Unit 1 Basic Python programs, functions Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
Building Java Programs Chapter 1 Introduction to Java Programming Copyright (c) Pearson All rights reserved.
Building Java Programs Chapter 1 Introduction to Java Programming.
Drawing complex figures with static methods. 2 Static methods question Write a program to print these figures using methods ______ / \ \ / \______/ \
1 WELCOME TO CSE 142! host: benson limketkai University of Washington, Summer 2007.
Building Java Programs Chapter 1 Introduction to Java Programming Copyright (c) Pearson All rights reserved.
CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
CS 112 Introduction to Programming Java Primitive Data Types; Arithmetic Expressions Yang (Richard) Yang Computer Science Department Yale University 208A.
8/2/07. >>> About Me Scott Shawcroft * Junior * Computer Engineering * Third Quarter TA * Creative Commons Intern * Small-time Open Source Developer
Building Java Programs Chapter 1 Introduction to Java Programming Copyright (c) Pearson All rights reserved.
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2016 Lecture 2: Functions. Review What is the output of the following print statements? print("this class\tis' the \"best\"") Write a.
Building Java Programs
CSc 110, Autumn 2017 Lecture 2: Functions.
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 2: Static Methods Expressions reading: 1.4 – 2.1
Building Java Programs
Topic: Functions – Part 2
basic Python programs, defining functions
CSc 110, Autumn 2017 Lecture 5: The for Loop and user input
Adapted from slides by Marty Stepp and Stuart Reges
basic Python programs, defining functions
CSc 110, Spring 2017 Lecture 5: Constants and Parameters
Topic 3 static Methods and Structured Programming
CSc 110, Autumn 2016 Lecture 5: Loop Figures and Constants
CSc 110, Spring 2017 Lecture 4: Nested Loops and Loop Figures
CSc 110, Spring 2018 Lecture 5: The for Loop and user input
Building Java Programs
CSc 110, Spring 2018 Lecture 3: Functions.
Building Static Methods
CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants
Building Java Programs
CSc 110, Spring 2018 Lecture 2: Functions.
Lecture 2: Static Methods Expressions reading: 1.4 – 2.1
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 1
Building Java Programs
Chapter 1 Lecture 1-2: Static Methods reading:
Building Java Programs
Building Java Programs
Building Java Programs
host: benson limketkai University of Washington, Spring 2007
Building Java Programs
Presentation transcript:

CSc 110, Autumn 2017 Lecture 3: Functions

Structure of a program No code should be placed outside a function. Instead use a main function. The one exception is a call to your main function def main(): message1() message2() print("Done with everything.") def message1(): print("This is message1.") def message2(): print("This is message2.") print("Done with message2.") main()

Functions question Write a program to print these figures using functions. ______ / \ / \ \ / \______/ +--------+ | STOP |

Development strategy First version (unstructured): ______ / \ / \ \ / \______/ +--------+ | STOP | First version (unstructured): Create an empty program. Copy the expected output into it, surrounding each line with print syntax. Run it to verify the output.

Program version 1 def main(): print(" ______") print(" / \\") print("| STOP |") main()

When to use functions (besides main) Place statements into a function if: The statements are related structurally, and/or The statements are repeated. You should not create functions for: An individual print statement. Only blank lines. Unrelated or weakly related statements. (Consider splitting them into two smaller functions.)

Development strategy 2 Second version (structured, with redundancy): ______ / \ / \ \ / \______/ +--------+ | STOP | Second version (structured, with redundancy): Identify the structure of the output. Divide the code into functions based on this structure.

Output structure The structure of the output: initial "egg" figure ______ / \ / \ \ / \______/ +--------+ | STOP | The structure of the output: initial "egg" figure second "teacup" figure third "stop sign" figure fourth "hat" figure This structure can be represented by functions: egg tea_cup stop_sign hat

Program version 2 def stop_sign(): print(" ______") print(" / \\") def main(): egg() tea_cup() stop_sign() hat() def egg(): print(" ______") print(" / \\") print("/ \\") print("\\ /") print(" \\______/") print() def tea_cup(): print("+--------+") def stop_sign(): print(" ______") print(" / \\") print("/ \\") print("| STOP |") print("\\ /") print(" \\______/") print() def hat(): print("+--------+")

Development strategy 3 Third version (structured, without redundancy): ______ / \ / \ \ / \______/ +--------+ | STOP | Third version (structured, without redundancy): Identify redundancy in the output, and create functions to eliminate as much as possible. Add comments to the program.

Output redundancy The redundancy in the output: ______ / \ / \ \ / \______/ +--------+ / | STOP | The redundancy in the output: egg top: reused on stop sign, hat egg bottom: reused on teacup, stop sign divider line: used on teacup, hat This redundancy can be fixed by functions: egg_top egg_bottom line

Program version 3 # Draws a teacup figure. def tea_cup(): egg_bottom() line() print() # Draws a stop sign figure. def stop_sign(): eggTop() print("| STOP |") # Draws a figure that looks sort of like a hat. def hat(): egg_top() # Draws a line of dashes. def line(): print("+--------+") # Suzy Student, CSc 110, Spring 2094 # Prints several figures, with methods for structure and redundancy. def main(): egg() tea_cup() stop_sign() hat() # Draws the top half of an an egg figure. def egg_top(): print(" ______") print(" / \\") print("/ \\") # Draws the bottom half of an egg figure. def egg_bottom(): print("\\ /") print(" \\______/") # Draws a complete egg figure. def egg(): egg_top() egg_bottom() print()