Unit 7 Lesson 1 Parameters and Return Explore

  • Slides: 25
Download presentation
Unit 7 - Lesson 1 Parameters and Return Explore

Unit 7 - Lesson 1 Parameters and Return Explore

Unit 7 Lesson 1 - Warm Up Prompt: Why would you want to make

Unit 7 Lesson 1 - Warm Up Prompt: Why would you want to make your code easier to work with or read?

Unit 7 Lesson 1 - Activity You and your partner should have Small sticky

Unit 7 Lesson 1 - Activity You and your partner should have Small sticky notes Regular sized sticky notes Pen / Pencil Envelopes

Unit 7 Lesson 1 - Activity 3 “lemon” make. Cake 3 tiers “lemon” flavor

Unit 7 Lesson 1 - Activity 3 “lemon” make. Cake 3 tiers “lemon” flavor

Unit 7 Lesson 1 - Activity make. Cake tiers 3 “lemon” flavor

Unit 7 Lesson 1 - Activity make. Cake tiers 3 “lemon” flavor

Unit 7 Lesson 1 - Activity make. Cake tiers 3 “lemon” flavor 3 “lemon”

Unit 7 Lesson 1 - Activity make. Cake tiers 3 “lemon” flavor 3 “lemon” 3

Unit 7 Lesson 1 - Activity 3 “lemon” 3

Unit 7 Lesson 1 - Activity 3 “lemon” 3

Unit 7 Lesson 1 - Activity Argument - the value passed to the parameter

Unit 7 Lesson 1 - Activity Argument - the value passed to the parameter 3 “lemon” make. Cake 3 tiers “lemon” flavor Parameter - a variable in a function definition. Used as a placeholder for values that will be passed through the function.

Unit 7 Lesson 1 - Activity What if I wanted a four layer chocolate

Unit 7 Lesson 1 - Activity What if I wanted a four layer chocolate cake? What would that look like? make. Cake 4 tiers 4 “chocolate” flavor

Unit 7 Lesson 1 - Activity Do This: Time to create your own function

Unit 7 Lesson 1 - Activity Do This: Time to create your own function with parameters! ● Complete Challenge #1 in your Activity Guide

Unit 7 Lesson 1 - Activity Let’s look at another function, this time one

Unit 7 Lesson 1 - Activity Let’s look at another function, this time one that calculates the cost of making a cake. Arguments 3 “lemon” cost. Cake 3 tiers “lemon” flavor Parameters

Unit 7 Lesson 1 - Activity cake. Cost tiers 3 “lemon” flavor

Unit 7 Lesson 1 - Activity cake. Cost tiers 3 “lemon” flavor

Unit 7 Lesson 1 - Activity Create two new local variables, flavor. Cost and

Unit 7 Lesson 1 - Activity Create two new local variables, flavor. Cost and total. Determine the value of flavor. Cost based on the argument passed through the flavor parameter Calculate total using flavor. Cost and the argument passed through the tiers parameter ? ? ? ?

Unit 7 Lesson 1 - Activity 3 “lemon” After running this, what does flavor.

Unit 7 Lesson 1 - Activity 3 “lemon” After running this, what does flavor. Cost equal? What does total equal? 4 12

Unit 7 Lesson 1 - Activity 3 “lemon” What does it mean to return

Unit 7 Lesson 1 - Activity 3 “lemon” What does it mean to return total? A return does two things: ● It stops the flow of the function. If a return is inside of a conditional, if that condition is met the function ends there. ● It returns a value to the place where the function was called.

Unit 7 Lesson 1 - Activity cake. Cost 3 tiers 12 “lemon” flavor We’ve

Unit 7 Lesson 1 - Activity cake. Cost 3 tiers 12 “lemon” flavor We’ve called the cake. Cost function. It has returned the value 12. But what happens to that value? How is it stored?

cake. Cos t tie 11 rs or “lfla em von” Unit 7 Lesson 1

cake. Cos t tie 11 rs or “lfla em von” Unit 7 Lesson 1 - Activity 12 cake. Calculator Let’s return to variable baggies! A function return value can be stored in a variable.

Unit 7 Lesson 1 - Activity Here’s how this looks in Javascript: var cake.

Unit 7 Lesson 1 - Activity Here’s how this looks in Javascript: var cake. Calculator = cake. Cost(3, “lemon”); After the expression is evaluated, cake. Caculator stores the value 12.

Unit 7 Lesson 1 - Activity We can also print to the console like

Unit 7 Lesson 1 - Activity We can also print to the console like so: console. log(“Cake cost: ” + cake. Cost(3, “lemon”)); Console Cake cost: 12

Unit 7 Lesson 1 - Activity Do This: Create a cost calculator function for

Unit 7 Lesson 1 - Activity Do This: Create a cost calculator function for building the house you created a function for earlier. ● Complete Challenge #2 on your Activity Guide

Unit 7 Lesson 1 - Wrap Up Takeaways ● Functions with parameters and return

Unit 7 Lesson 1 - Wrap Up Takeaways ● Functions with parameters and return values help us simplify our code ● Functions can only return one value at a time ● A function can have: ○ No parameters and no return values ○ Parameters, but no return values ○ Return values, but no parameters ○ Parameters and return values

Unit 7 Lesson 1 - Wrap Up Vocabulary ● Parameter - a variable in

Unit 7 Lesson 1 - Wrap Up Vocabulary ● Parameter - a variable in a function definition. Used as a placeholder for values that will be passed through the function. ● Argument - the value passed to the parameter ● Return - used to return the flow of control to the point where the procedure (also known as a function) was called and to return the value of expression.