Adapted from slides by Marty Stepp and Stuart Reges

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1 self-check: #1-6 exercises: #1-3 videos: Ch.
Advertisements

BUILDING JAVA PROGRAMS CHAPTER 3 PARAMETERS AND OBJECTS.
Building Java Programs Chapter 3 Parameters and Objects Copyright (c) Pearson All rights reserved.
1 Parameters. 2 Repetitive figures Consider the task of drawing the following figures: ************* ******* *********************************** **********
Copyright 2008 by Pearson Education Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs Lecture 3-1: Parameters (reading: 3.1)
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
Copyright 2008 by Pearson Education 1 Class constants and scope reading: 2.4 self-check: 28 exercises: 11 videos: Ch. 2 #5.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs.
Topic 7 parameters Based on slides bu Marty Stepp and Stuart Reges from "We're flooding people with information. We.
Building Java Programs Chapter 3 Parameters and Objects Copyright (c) Pearson All rights reserved.
CS 112 Introduction to Programming Graphics; Animation Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Building Java Programs Parameters and Objects. 2 Redundant recipes Recipe for baking 20 cookies: –Mix the following ingredients in a bowl: 4 cups flour.
Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Warm Up As you enter get out a half sheet of loose paper. Write the HelloWorld program on it. Be prepared to correct your code.
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University.
Copyright 2010 by Pearson Education Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1.
CS305j Introduction to Computing Parameters and Methods 1 Topic 8 Parameters and Methods "We're flooding people with information. We need to feed it through.
CS 112 Introduction to Programming Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Copyright 2009 by Pearson Education Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1 self-check: #1-6 exercises: #1-3 videos: Ch.
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.
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 2: Functions.
Building Java Programs Chapter 3
Building Java Programs Chapter 3
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 16: Fencepost Loops and Review
Building Java Programs
CSCI 161 – Introduction to Programming I William Killian
Lecture 2: Static Methods Expressions reading: 1.4 – 2.1
Building Java Programs
Building Java Programs
CSc 110, Autumn 2017 Lecture 5: The for Loop and user input
Building Java Programs Chapter 3
CSc 110, Spring 2017 Lecture 6: Parameters (cont.) and Graphics
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
CS 106A, Lecture 7 Parameters and Return
CSc 110, Spring 2017 Lecture 5: Constants and Parameters
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
CSc 110, Autumn 2016 Lecture 19: lists as Parameters
CSc 110, Autumn 2017 Lecture 9: Graphics and Nested Loops
CSc 110, Autumn 2016 Lecture 5: Loop Figures and Constants
Topic 7 parameters "We're flooding people with information. We need to feed it through a processor. A human must turn information into intelligence or.
Building Java Programs
CSc 110, Spring 2017 Lecture 4: Nested Loops and Loop Figures
Redundant recipes Recipe for baking 20 cookies:
Building Java Programs
CSc 110, Spring 2018 Lecture 7: input and Constants
CSc 110, Spring 2018 Lecture 5: The for Loop and user input
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 3: Functions.
CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants
Lecture 8:The For Loop AP Computer Science Principles.
CSc 110, Spring 2018 Lecture 2: Functions.
Lecture 2: Static Methods Expressions reading: 1.4 – 2.1
Building Java Programs
CSc 110, Spring 2018 Lecture 8: input and Parameters
Building Java Programs
CSc 110, Spring 2018 Lecture 10: More Graphics
Let’s make Cookies!.
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
CSc 110, Autumn 2017 Lecture 8: More Graphics
Scope scope: The part of a program where a variable exists. From its declaration to the end of the { } braces A variable declared in a for loop exists.
Suggested self-checks:
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

Adapted from slides by Marty Stepp and Stuart Reges 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: 8 cups flour 2 cups butter 2 cups sugar 4 eggs 80 oz. chocolate chips ...

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: 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 This code is redundant. Would variables help? Would constants help? What is a better 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): def line_of_35(): for i in range(1, 36): ... line - A function to draw a line of any number of stars. box - A function to draw a box of any size.

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. main line ******* 7 ************* 13

Stating that a function requires a parameter in order to run 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.

Calling a function and specifying values for its parameters 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...

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) def chant(times): for i in range(0, times): print("Just a salad...") 3 7

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 a box of stars of the given size. # 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="") # 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("*")

Strings as parameters Output: 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 a box of stars of the given size. # 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) # 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("*") # 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)) Output: 2 and 4 9 and 3