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

Similar documents
Building Java Programs

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

Building Java Programs

Building Java Programs

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

Redundant recipes. Building Java Programs Chapter 3. Parameterized recipe. Redundant figures

Building Java Programs Chapter 3

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

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

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

Building Java Programs

CSc 110, Autumn Lecture 2: Functions

Static Methods & Decomposition

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

Building Java Programs

CSc 110, Autumn Lecture 30: Methods. 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

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

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

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

Entering the world of Javatar

CSE 142, Summer 2015

Graded Project. HTML Coding

CSE 201 Java Programming I. Smart Coding School website:

CSE 142, Summer 2014

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

Admin. CS 112 Introduction to Programming. Counting Down: Code Puzzle. Counting Down: Code Puzzle

CS 112 Introduction to Programming

Lecture 1: Basic Java Syntax

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

CS 106A, Lecture 7 Parameters and Return

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

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

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

Lesson 10: Interpreting Graphs of Proportional Relationships

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

Recap: Assignment as an Operator CS 112 Introduction to Programming

Topic 6 loops, figures, constants

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

CS 112 Introduction to Programming

Building Java Programs

Building Java Programs

Procedural decomposition using static methods

Building Java Programs

What is a Function? What are functions good for?

CSE 160 Spring 2018: Final Exam

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

Lesson 166 (Pages 27-29)

Java Programming. What is a program? Programs as Recipes. Programs Recipes 8/20/15. Pancakes In one bowl

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

Lecture 19: Recursion

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


public Twix() { calories = 285; ingredients = "chocolate, sugar, cookie, caramel"; }

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

Algorithms: The recipe for computation

Foreword. Agenda 22/09/2009 INTRODUCTION TO VBA PROGRAMMING. Foundations. Tools

Advanced if/else & Cumulative Sum

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

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

Module 1 - Applications: Visually Modeling Fractions

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

Announcements Tues., Aug and 2. to call them ( ) ! For next time, read Learning Computing with Robots, chs.

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

COMPUTATIONAL THINKING

WELCOME TO CS 24! Enrollment status: 105/105, Read the syllabus. Know what s required. Know how to get help. Problem Solving with Computers-II

def order(food): food = food.upper() print( Could I have a big + food + please? ) return fresh + food

Lecture 1: Introduction

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

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

NS6-50 Dividing Whole Numbers by Unit Fractions Pages 16 17

Topic 4 Expressions and variables

What students need to know for ALGEBRA I Students expecting to take Algebra I should demonstrate the ability to: General:

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

CSE 143 Lecture 10. Recursion

Building Java Programs

CSc 110, Autumn 2016 Lecture 18: Line-Based File Input

Topic 5 for loops and nested loops

Building Java Programs

Topic 9 More Graphics. Based on slides bu Marty Stepp and Stuart Reges from

1. Part A: a. Create ORACLE SQL Statements with appropriate PK and FKs for each table

CS298 Project Presentation By Priya Gangaraju

Introduction to Computer Programming

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

I can solve simultaneous equations algebraically and graphically. I can solve inequalities algebraically and graphically.

MAS115: R programming Lecture 2: Pseudocode Lab Class and Technical Material: Matrices, Factors, Data frames, Lists

Unit 3. parameters and graphics

Introduction Jan. 22, Ch 1

Parameters. Repetitive figures. A solution? Parameterization. Declaring parameterized methods. Generalizing methods. Readings: 3.1

CSc 110, Spring 2017

Adult Computer and Employment Skills

Recursion. Garfield AP Computer Science. As usual, significant borrowings from Stuart Reges and Marty Stepp at UW -- thanks!!

Week 3. parameters, return, math, graphics

Welcome to CSC148! Introduction to Computer Science

Robust Design: Experiments for Better Products

Math 085 Final Exam Review

4. Write sets of directions for how to check for direct variation. How to check for direct variation by analyzing the graph :

MASSACHUSETTS VOCATIONAL TECHNICAL TEACHER TESTING PROGRAM SCOPE OF TEST CODE #11 - CULINARY ARTS WRITTEN EXAM QUESTIONS TIME ALLOWED: 3 HOURS

Transcription:

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

Promoting reuse Programmers build increasingly complex applications Enabled by existing building blocks, e.g. methods The more general a building block, the easier to reuse Abstraction: focusing on essential properties rather than implementation details Algebra is all about abstraction Functions solve an entire class of similar problems

Redundant recipes Recipe for baking 20 cookies: Mix the following ingredients in a bowl: 4 cups flour 1 cup butter 1 cup sugar 2 eggs 40 oz. chocolate chips... Place on sheet and Bake for about 10 minutes. Recipe for baking 40 cookies: Mix the following ingredients in a bowl: 8 cups flour 2 cups butter 2 cups sugar 4 eggs 80 oz. chocolate chips... Place on sheet and Bake for about 10 minutes.

Parameterized recipe Recipe for baking 20 cookies: Mix the following ingredients in a bowl: 4 cups flour 1 cup sugar 2 eggs... Recipe for baking N cookies: Mix the following ingredients in a bowl: N/5 cups flour N/20 cups butter N/20 cups sugar N/10 eggs 2N oz. chocolate chips... Place on sheet and Bake for about 10 minutes. parameter: A value that distinguishes similar tasks.

Redundant figures Consider the task of printing the following lines/boxes: ************* ******* *********************************** ********** * * ********** ***** * * * * *****

A redundant solution def main(): line_of_13() line_of_7() line_of_35() box10x3() box5x4() def line_of_13(): for i in range(1, 14): print("*", end="") print() def line_of_7(): for i in range(1, 8): print("*", end="") print() This code is redundant. Would variables help? Would constants help? What is a better solution? line - A function to draw a line of any number of stars. box - A function to draw a box of any size. def line_of_35(): for i in range(1, 36): print("*", end="") print()...

Parameterization parameter: A value passed to a function by its caller. Instead of line_of_7, line_of_13, write line to draw any length. When declaring the function, we will state that it requires a parameter for the number of stars. When calling the function, we will specify how many stars to draw. 7 main line ******* 13 line *************

Declaring a parameter Stating that a function requires a parameter in order to run def <name> (<name>): <statement>(s) Example: def say_password(code): print("the password is: " + code) When say_password is called, the caller must specify the code to print.

Passing a parameter Calling a function and specifying values for its parameters <name>(<expression>) Example: say_password(42) say_password(12345) Output: The password is 42 The password is 12345

Parameters and loops A parameter can guide the number of repetitions of a loop. chant(3) def chant(times): for i in range(0, times): print("just a salad...") Output: Just a salad... Just a salad... Just a salad...

How parameters are passed When the function is called: The value is stored into the parameter variable. The function's code executes using that value. chant(3) chant(7) 37 def chant(times): for i in range(0, times): print("just a salad...")

Common errors If a function accepts a parameter, it is illegal to call it without passing any value for that parameter. chant() # ERROR: parameter value required The value passed to a function must be of a type that will work. chant(3.7) # ERROR: must be of type int if it # is used as a range bound Exercise: Change the stars program to use a parameterized function for drawing lines of stars.

Stars solution # Prints several lines of stars. # Uses a parameterized method to remove redundancy. def main(): line(13) line(7) line(35) # Prints the given number of stars plus a line break. def line(count): for i in range(0, count): print("*", end="") print()

Multiple parameters A method can accept multiple parameters. (separate by, ) When calling it, you must pass values for each parameter. Declaration: def <name>(<name>,..., <name>): <statement>(s) Call: <name>(<exp>, <exp>,..., <exp>)

Multiple parameters example def main(): printnumber(4, 9) printnumber(17, 6) printnumber(8, 0) printnumber(0, 8) def printnumber(number, count): for i in range(0, count): print(number, end="") print() Output: 444444444 171717171717 00000000 Modify the stars program to draw boxes with parameters.

Stars solution # Prints several lines and boxes made of stars. # Third version with multiple parameterized methods. def main(): line(13) line(7) line(35) print() box(10, 3) box(5, 4) box(20, 7) # Prints the given number of #stars plus a line break. def line(count): for i in range(0, count): print("*", end="") print() # Prints a box of stars of the given size. def box(width, height): line(width) for line in range(0, height - 2): print("*", end="") for space in range(0, width - 2): print(" ", end="") print("*") line(width)

Strings as parameters say_hello("allison") teacher = "Bictolia" say_hello(teacher) def sayhello(name): print("welcome, " + name) Output: Welcome, Allison Welcome, Bictolia Modify the stars program to use string parameters. Use a function named repeat that prints a string many times.

Stars solution # Prints several lines and boxes made of stars. # Fourth version with String parameters. def main(): line(13) line(7) line(35) print() box(10, 3) box(5, 4) box(20, 7) # Prints the given number of # stars plus a line break. def line(count): repeat("*", count) print() # Prints a box of stars of the given size. def box(width, height): line(width) for line in range(height 2): print("*", end="") repeat(" ", width - 2) print("*") line(width) # Prints the given String the given # number of times. def repeat(s, times): for i in range(0, times): print(s, end="")

Value semantics value semantics: When numbers and strings are passed as parameters, their values are copied. Modifying the parameter will not affect the variable passed in. def strange(x): x = x + 1 print("1. x = " + x) x = 23 strange(x) print("2. x = " + x)... Output: 1. x = 24 2. x = 23

A "Parameter Mystery" problem def main(): x = 9 y = 2 z = 5 mystery(z, y, x) mystery(y, x, z) def mystery(x, z, y): print(str(z) + " and " + str(y - x))