CSc 110, Autumn Lecture 2: Functions

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

Building Java Programs

Building Java Programs

Building Java Programs

CSE 142, Summer 2015

CSE 142, Summer 2014

Static Methods & Decomposition

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

Topic 3 static Methods and Structured Programming

CS 112 Introduction to Programming

Introduction to Computer Programming

Entering the world of Javatar

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

clicker question Comments Using comments Topic 3 static Methods and Structured 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:

Building Java Programs. Introduction to Java Programming

CSc 110, Spring 2017

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

Building Java Programs. Introduction to Programming and Simple Java Programs

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

Graded Project. HTML Coding

Lecture 1. basic Python programs, defining functions

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

Writing a Basic Program in Python

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

Building Java Programs

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

Building Java Programs

Introduction Jan. 22, Ch 1

Building Java Programs

CS 190C: Introduction to Computational Thinking

Building Java Programs. Chapter 1: Introduction to Java Programming

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

COMPUTATIONAL THINKING

CMSC201 Computer Science I for Majors

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

CS 112 Introduction to Programming

Welcome to CSE 142! Whitaker Brand. University of Washington, Winter 2018

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

CMSC 201 Fall 2018 Python Coding Standards

Welcome to CSE 142! Zorah Fung University of Washington, Spring Building Java Programs Chapter 1 Lecture 1: Introduction; Basic Java Programs

Python in 10 (50) minutes

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

CMSC201 Computer Science I for Majors

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

CSE 142. Lecture 1 Course Introduction; Basic Java. Portions Copyright 2008 by Pearson Education

Chapter 2 Working with Data Types and Operators

CC112 Structured Programming

CSC Web Programming. Introduction to JavaScript

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

Introduction to Computer Programming concepts using StarLogo TNG

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

Introduction to Computer Programming for Non-Majors

CSc Introduction to Computing

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

Creating a Program in JCreator. JCreator is then used to create our program. But the first step is to create a new file.

Introduction to Python Code Quality

Java Programming. What is a program? Programs Recipes. Programs as Recipes 8/16/12. Pancakes In one bowl. mix: 1½ cup flour

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

S206E Lecture 19, 5/24/2016, Python an overview

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

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

Introduction to Computer Programming for Non-Majors

the rules The Goal Get all three of your monkeys around the board and into the Banana Grove before anyone else can!

Algorithmic Thinking: Loops and Conditionals

Getting started with Java

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Welcome to Python! If you re the type of person who wants to know

Introduction to Python (All the Basic Stuff)

CIS 110: Introduction to Computer Programming. Lecture 2 Decomposition and Static Methods ( 1.4)

The current topic: Python. Announcements. Python. Python

Full file at

Outline. CIS 110: Introduction to Computer Programming. What is Computer Science? What is computer programming? What is computer science?

Computer Programming : C++

Chapter Two: Classes and Objects

Introduction to Computation for the Humanities and Social Sciences. CS 3 Chris Tanner

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

CITS 4406 Problem Solving & Programming

Algorithms: The recipe for computation

Variables, Constants, and Data Types

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Functions: Decomposition And Code Reuse

CSC 467 Lecture 3: Regular Expressions

CSE 101 Introduction to Computers Development / Tutorial / Lab Environment Setup


Programming for Engineers in Python. Autumn

The cuisine package. Ben Cohen. 2000/08/01, v0.5

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

PREPARING FOR PRELIM 2

Programming Languages & Translators. XML Document Manipulation Language (XDML) Language Reference Manual

SSEA Computer Science: CS106A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Lecture Writing Programs. Richard E Sarkis CSC 161: The Art of Programming

UEE1302 (1102) F10: Introduction to Computers and Programming

Important Dates. Please refer to the 2017 Fall Product Program large white envelope. These dates are permanent, and cannot be changed.

Transcription:

CSc 110, Autumn 2017 Lecture 2: Functions

Escape sequences escape sequence: A special sequence of characters used to represent certain special characters in a string. \t tab character \n new line character \" quotation mark character \' quotation mark character \\ backslash character Example: print("\\hello\nhow\tare \"you\"?\\\\") Output: \hello how are "you"?\\

Questions What is the output of the following print statements? print("\ta\tb\tc") print("\\\\") print("'") print("\"\"\"") print("c:\nin\the downward spiral") Write a print statement to produce this output: /\ //\\

Answers Output of each print statement: \\ ' """ C: in a b c he downward spiral print statement to produce the line of output: print("\ /\\\ //\\\\\")

Creating a Python Program File

Creating a Python Program File When Run -> Run Module is selected:

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 Examples: # This is a one-line comment. # This is a very long # multi-line comment.

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

functions

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. Spread frosting and sprinkles onto the cookies....

Problems with algorithms lack of structure: Many steps; tough to follow. redundancy: Consider making a double batch... 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 timer for 10 minutes. Place the second batch of cookies into the oven. Allow the cookies to bake. Mix ingredients for frosting....

Structured algorithms structured algorithm: Split into coherent 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....

Removing redundancy A well-structured algorithm can describe repeated tasks with less redundancy. 1 Make the cookie 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....

functions function: A named group of statements. denotes the structure of a program eliminates redundancy by code reuse procedural decomposition: dividing a problem into functions Writing a function is like adding a new command to Python. Function A statement statement statement Function B statement statement Function C statement statement statement

Declaring a function Syntax: Gives your function a name so it can be executed def name(): statement statement... statement Example: def print_warning(): print("this product causes cancer") print("in lab rats and humans.")

Calling a function Syntax: name() Executes the function s code You can call the same function many times if you like. Example: print_warning() #separate multiple words with underscores Output: This product causes cancer in lab rats and humans.

Functions calling functions def message1(): print("this is message1.") def message2(): print("this is message2.") message1() print("done with message2.") message1() message2() print("done with everything.") Output: This is message1. This is message2. This is message1. Done with message2. Done with main.

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() def message1(): print("this is message1.") message2() print("done with main.")... def message2(): print("this is message2.") message1() print("done with message2.") def message1(): print("this is message1.")

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.") message1() print("done with message2.") 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.)

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

Development strategy \ / \ / \ / \ / +--------+ 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("\") print("\\ /") print(" \\ /") print() print("\\ /") print(" \\ /") print("+--------+") print() print(" ") print(" \") print("\") print(" STOP ") print("\\ /") print(" \\ /") print() print(" ") print(" \") print("\") print("+--------+") main()

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

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

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("+--------+")