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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs Lecture 3-1: Parameters (reading: 3.1)
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.
Topic 11 Scanner object, conditional execution Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Building Java Programs Chapter 3 Parameters and Objects Copyright (c) Pearson All rights reserved.
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.
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 9: input ; if/else Adapted from slides by Marty Stepp and Stuart Reges.
CSc 110, Autumn 2017 Lecture 13: Cumulative Sum and Boolean Logic
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs Chapter 3
CSc 110, Autumn 2017 Lecture 16: Fencepost Loops and Review
CSc 110, Autumn 2017 Lecture 17: while Loops and Sentinel Loops
Building Java Programs
CSCI 161 – Introduction to Programming I William Killian
CSc 120 Introduction to Computer Programing II Adapted from slides by
CSc 110, Autumn 2016 Lecture 12: Advanced if/else; Cumulative sum
CSc 110, Autumn 2017 Lecture 5: The for Loop and user input
CSc 110, Autumn 2017 Lecture 31: Dictionaries
Topic 11 Scanner object, conditional execution
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2017 Lecture 8: input; if/else
CSc 110, Autumn 2016 Lecture 12: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2017 Lecture 9: Advanced if/else; Cumulative sum
CSc 110, Spring 2017 Lecture 5: Constants and Parameters
CSc 110, Spring 2018 Lecture 16: Fencepost Loops and while loops
CSc 110, Autumn 2016 Lecture 9: input; if/else
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, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum
Adapted from slides by Marty Stepp and Stuart Reges
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 13: Cumulative Sum and Boolean Logic
CSc 110, Spring 2018 Lecture 3: Functions.
CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants
Building Java Programs
Building Java Programs
Building Java Programs
CSc 110, Spring 2018 Lecture 8: input and Parameters
CSc 110, Spring 2018 Lecture 10: More Graphics
Let’s make Cookies!.
Cookie Chemistry Ms. Chang.
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
CSc 110, Autumn 2017 Lecture 8: More Graphics
Building Java Programs
Suggested self-checks:
Building Java Programs
Building Java Programs
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Optional Topic: User Input with Scanner
Presentation transcript:

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

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 picture: .-'';'-. ,' <_,-.`. /) ,--,_>\_\ |' ( \_ | |_ `-. / | \`-. ; _(`/ `.( \/ ,' `-....-' ___ _ /= =\/') /= = =\/ ou ou /v v\/') /v v v\/

A redundant solution This code is redundant. Would variables help? Would constants help? What is a better solution? def main(): world() turtle_equal() turtle_v() def world(): print(" .-'';'-.") print(" ,' <_,-.`.") print(" /) ,--,_>\\_\\") print(" |' ( \\_ |") print(" |_ `-. / |") print(" \`-. ; _(`/") print(" `.( \/ ,'") print(" `-....-'") def turtle_equal(): print(" ___ _") print(" /= =\\/')") print(" /= = =\\/") print(" ou ou") def turtle_v(): print(" /v v\\/')") print(" /v v v\\/") main() ... turtle - A function to draw a turtle of any shell pattern.

Parameterization parameter: A value passed to a function by its caller. Instead of turtle_equal, turtle_v, write turtle to draw any turtle. 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 turtle /= =\/') /= = =\/ ou ou "=" /v v\/') /v v v\/ "v"

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 counts program to use a parameterized function for drawing lines of numbers.

Interactive programs interactive program: Reads input from the console. While the program runs, it asks the user to type input. The input typed by the user is stored in variables in the code. Can be tricky; users are unpredictable and misbehave. But interactive programs have more interesting behavior.

input input: An function that can read input from the user. Using an input object to read console input: name = input(prompt) Example: name = input("type your name: ") The variable name will store the value the user typed in

input example Console (user input underlined): age 29 29 def main(): age = input("How old are you? ") years = 65 - age print(years, "years until retirement!") Console (user input underlined): How old are you? age 29 It's also useful to write a program that prompts for multiple values, both on the same line or each on its own line. 29 Traceback (most recent call last): File "<pyshell#13>", line 1, in <module> print(65 - age) TypeError: unsupported operand type(s) for -: 'int' and 'str'

input example Console (user input underlined): age 29 years 36 29 def main(): age = int(input("How old are you? ")) years = 65 - age print(years, "years until retirement!") Console (user input underlined): How old are you? 36 years until retirement! age 29 years 36 It's also useful to write a program that prompts for multiple values, both on the same line or each on its own line. 29