CSc 110, Spring Lecture 2: Functions. Adapted from slides by Marty Stepp and Stuart Reges

Similar documents
CSc 110, Autumn Lecture 2: Functions

Building Java Programs

Building Java Programs

Building Java Programs

Recap: Structure of a Java program CS 112 Introduction to Programming. A Foundation for Programming Why Introduce Static Methods?

CS 112 Introduction to Programming

Topic 3 static Methods and Structured Programming

Static Methods & Decomposition

Entering the world of Javatar

clicker question Comments Using comments Topic 3 static Methods and Structured Programming

CSE 142, Summer 2015

CSE 142, Summer 2014

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Introduction to Computer Programming

Building Java Programs

Building Java Programs Chapter 1

Lecture 1: Basic Java Syntax

Procedural decomposition using static methods

Building Java Programs

Building Java Programs Chapter 1

CSE 201 Java Programming I. Smart Coding School website:

CSc 110, Autumn 2016 Lecture 6: Parameters. Adapted from slides by Marty Stepp and Stuart Reges

Building Java Programs. Introduction to Java Programming

CSc 110, Spring 2017 Lecture 3: Expressions, Variables and Loops. Adapted from slides by Marty Stepp and Stuart Reges

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

CS 112 Introduction to Programming

Topic 7 parameters. Based on slides from Marty Stepp and Stuart Reges from

A Unicorn Named Sparkle Word Search. A New Friend for Sparkle Coloring Sheet. A Unicorn Named Sparkle s First Christmas Coloring Sheet

CSc 110, Spring 2017

CSc 110, Autumn Lecture 30: Methods. Adapted from slides by Marty Stepp and Stuart Reges

Lecture 1. basic Python programs, defining functions

Building Java Programs

Building Java Programs

Building Java Programs

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Recap: Assignment as an Operator CS 112 Introduction to Programming

CSc 110, Autumn Lecture 26: Assertions. Adapted from slides by Marty Stepp and Stuart Reges

Graded Project. HTML Coding

CS 112 Introduction to Programming

The diagram below is only partially complete. Use the terms in the word list to complete the empty boxes. Make dinner

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

Lab 1: Course Intro, Getting Started with Python IDLE. Ling 1330/2330 Computational Linguistics Na-Rae Han

COMPUTATIONAL THINKING

CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random. Adapted from slides by Marty Stepp and Stuart Reges

The CS job market. Building Java Programs Chapter 1. Programming languages. What is programming? Introduction to Java Programming

CS 190C: Introduction to Computational Thinking

Writing a Basic Program in Python

Building Java Programs. Introduction to Programming and Simple Java Programs

CSc 110, Autumn 2016 Lecture 12: Advanced if/else; Cumulative sum. Adapted from slides by Marty Stepp and Stuart Reges

CSc 110, Spring Lecture 13: Random numbers and Boolean Logic. Adapted from slides by Marty Stepp and Stuart Reges

Everyday Algorithms. Algorithms and Introduction to Programming. bake a cake. start. mix sugar and butter. stir in 2 eggs. stir in flour.

CSc 110, Autumn 2016 Lecture 7: Graphics. Adapted from slides by Marty Stepp and Stuart Reges

CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum. Adapted from slides by Marty Stepp and Stuart Reges

Algorithmic Thinking: Loops and Conditionals

Topic 6 loops, figures, constants

CSc 110, Spring Lecture 14: Booleans and Strings. Adapted from slides by Marty Stepp and Stuart Reges

CSc 110, Autumn Lecture 14: Strings. Adapted from slides by Marty Stepp and Stuart Reges

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

CSc 110, Autumn 2016 Lecture 15: lists. Adapted from slides by Marty Stepp and Stuart Reges

CPS 216, Problem Solving and Programming Techniques in C++ Saleh M. Alnaeli, Ph.D. Spring, Lab_Week2

Introduction to Computer Programming concepts using StarLogo TNG

CSc 110, Spring Lecture 11: if / else. Adapted from slides by Marty Stepp and Stuart Reges

CSc 110, Spring Lecture 28: Sets and Dictionaries. Adapted from slides by Marty Stepp and Stuart Reges

Introduction Jan. 22, Ch 1

The >>> is a Python prompt indicating that Python is ready for us to give it a command. These commands are called statements.

CSc 110, Autumn Lecture 11: if / else. Adapted from slides by Marty Stepp and Stuart Reges

Building Java Programs

Building Java Programs

Python Unit

Two figures that have the exact same shape, but not necessarily the exact same size, are called similar

STEAM Clown Productions. Python - Conditional. STEAM Clown & Productions Copyright 2018 STEAM Clown. Last Updated: Thursday, January 24, 2019.

Grade 7 Math LESSON 14: MORE PROBLEMS INVOLVING REAL NUMBERS TEACHING GUIDE

PREPARING FOR PRELIM 2

Symmetry Students complete symmetric figures that are partially given and create their own symmetric figures.

Lecture 4. Defining Functions

CSc 110, Spring 2017 Lecture 38: Critters. Adapted from slides by Marty Stepp and Stuart Reges

CSc 110, Spring Lecture 10: Strings. Adapted from slides by Marty Stepp and Stuart Reges

Programming for Engineers in Python. Recitation 1

CITS 4406 Problem Solving & Programming

CMSC 201 Fall 2018 Python Coding Standards

Lesson 166 (Pages 27-29)

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

Algorithms: The recipe for computation

Teaching London Computing

Java objects: a first view [Reading: chapters 1 & 2] CSC 142 B 1

The current topic: Python. Announcements. Python. Python

CSE 11 Style Guidelines

CSc 110, Autumn Lecture 13: Cumulative Sum and Boolean Logic. Adapted from slides by Marty Stepp and Stuart Reges

Algorithmic Problem Solving. Penny Rheingans University of Maryland Baltimore County

Chapter 2 Working with Data Types and Operators

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

Programming for Engineers in Python. Autumn

Intensive Introduction to Computer Science. Course Overview Programming in Scratch

4.2 Function definitions the basics

Week 2. expressions, variables, for loops

Scheme of work Cambridge International AS & A Level Computing (9691)

Lecture 4. Defining Functions

CSC Web Programming. Introduction to JavaScript

Introduction to Computer Programming for Non-Majors CSC 2301, Fall The Department of Computer Science

Transcription:

CSc 110, Spring 2017 Lecture 2: Functions Adapted from slides by Marty Stepp and Stuart Reges 1

Review From last lecture: print, strings, escape sequences. What is the output of the following statement? print("who said,\"to thine own self be true.\"?") Write a print statement to produce this output: //\\ 2

Comments comment: A note written in source code by the programmer to describe or clarify the code. Comments are not executed when your program runs. Syntax: # comment text Python statement # comment text Examples: # This is a one-line comment. # This is a very long # multi-line comment. print("hello!") # Output a greeting 3

Comments example # Suzy Student # CSc 110 # Displays lyrics # first part print("when I first got into magic") print("it was an underground phenomenon") print() # second part print("now everybody's like") print("pick a card, any card") 4

functions 5

Algorithms algorithm: A list of steps for solving a problem. Example algorithm: "Bake sugar cookies" Mix the dry ingredients. Cream the butter and sugar. Beat in the eggs. Stir in the dry ingredients. Set the oven temperature. Set the timer for 10 minutes. Place the cookies into the oven. Allow the cookies to bake. Mix ingredients for frosting. Spread frosting and sprinkles onto the cookies. 6

Problems with this algorithm lack of structure: Many steps; tough to follow at a glance. What if there is batter for 24 cookies and the baking sheet fits only 12? Mix the dry ingredients. Cream the butter and sugar. Beat in the eggs. Stir in the dry ingredients. Set the oven temperature. Set the timer for 10 minutes. Place the first batch of cookies into the oven. Allow the cookies to bake. Set the oven temperature. Set the timer for 10 minutes. Place the second batch of cookies into the oven. Allow the cookies to bake. Mix ingredients for frosting. Spread frosting and sprinkles on the cookies Repetition: Steps are listed twice 7

Structured algorithms structured algorithm: Decomposed into related tasks. 1 Make the batter. Mix the dry ingredients. Cream the butter and sugar. Beat in the eggs. Stir in the dry ingredients. 2 Bake the cookies. Set the oven temperature. Set the timer for 10 minutes. Place the cookies into the oven. Allow the cookies to bake. 3 Decorate the cookies. Mix the ingredients for the frosting. Spread frosting and sprinkles onto the cookies. 8

Removing repetition A well-structured algorithm can describe repeated steps easily. 1 Make the batter. Mix the dry ingredients.... 2a Bake the cookies (first batch). Set the oven temperature. Set the timer for 10 minutes.... 2b Bake the cookies (second batch). Repeat Step 2a 3 Decorate the cookies. Mix the ingredients for the frosting. 9

functions function: A named sequence of statements. supports the creation of well-structured programs eliminates repetition by code reuse procedural decomposition: dividing a problem or sequence of statements into functions Example: def print_warning(): print("this product causes cancer") print("in lab rats and humans.") 10

Declaring a function Syntax: Gives your function a name so it can be executed def name(): statement statement... Statement Function names: Space is part of the syntax. Statements in a function must have the same level of indentation. Consist of upper and lower case letters, "_", and digits 0 through 9. Example: def print_warning(): print("this product causes cancer") print("in lab rats and humans.") 11

Calling a function Syntax: name() Executes the statements within a function You can call the same function many times if you like. Example: print_warning() #using underscores makes names readable Output: This product causes cancer in lab rats and humans. 12

Functions calling functions def message1(): print("this is message1.") def message2(): print("this is message2.") message1() print("done with message2.") message1() message2() print("all done.") Output: This is message1. This is message2. This is message1. Done with message2. All done. 15

Control flow When a function is called, the program's execution... "jumps" into that function, executing its statements, then "jumps" back to the point where the function was called. message1() message2() print("all done.")... def message1(): print("this is message1.") def message2(): print("this is message2.") message1() print("done with message2.") def message1(): print("this is message1.") 16

Structure of a program Best practice for well-structured programs: all code should be placed inside a function. def message1(): print("this is message1.") def message2(): print("this is message2.") message1() print("done with message2.") message1() message2() print("done with all.") def main(): message1() message2() print("done with all.") def message1(): print("this is message1.") def message2(): print("this is message2.") message1() print("done with message2.") main() Use a function called main. 17

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. Unrelated or weakly related statements. (Consider splitting them into two smaller functions.) 18

Problem Write a program to print these figures using functions. \/ \/ +--------+ STOP \/ +--------+ 20

Development strategy \/ \/ +--------+ STOP \/ Approach simply get this to print correctly First version (unstructured): Start IDLE. File->open -> new Copy the expected output into it, surrounding each line with print syntax. Run it to verify the output. +--------+ 21

Program version 1 def main(): print(" ") print(" \") print("\") print("\") print(" \\/") print() print("\") print(" \\/") print("+--------+") print() print(" ") print(" \") print("\") print(" STOP ") print("\") print(" \\/") print() print(" ") print(" \") print("\") print("+--------+") main() 22

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

Output structure \/ \/ +--------+ 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 24

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

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

Repetition 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 27

Program version 3 # Suzy Student, CSc 110, Spring 2094 # Prints several figures, with functions 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() # Draws a teacup figure. def tea_cup(): egg_bottom() line() print() # Draws a stop sign figure. def stop_sign(): eggtop() print(" STOP ") egg_bottom() print() # Draws a figure that looks sort of like a hat. def hat(): egg_top() line() # Draws a line of dashes. def line(): print("+--------+") 28