0% found this document useful (0 votes)
23 views

CSP Parameters, Return, and Libraries - Lesson 1 - Parameters and Return Explore

This document outlines a lesson on functions with parameters and return values in computer science. It includes prompts for students to practice defining functions that accept parameters and return values. The lesson explains that parameters act as placeholders for values passed into a function, while return values allow functions to send a result back to the code that called the function. Students are asked to create their own functions that demonstrate understanding of parameters and return values.

Uploaded by

Aalaa Mady
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

CSP Parameters, Return, and Libraries - Lesson 1 - Parameters and Return Explore

This document outlines a lesson on functions with parameters and return values in computer science. It includes prompts for students to practice defining functions that accept parameters and return values. The lesson explains that parameters act as placeholders for values passed into a function, while return values allow functions to send a result back to the code that called the function. Students are asked to create their own functions that demonstrate understanding of parameters and return values.

Uploaded by

Aalaa Mady
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Unit 7 - Lesson 1

Parameters and Return Explore

Computer Science Principles


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 notes
Regular sized sticky notes
Pen / Pencil
Envelopes
Unit 7 Lesson 1 - Activity

“lemon”

makeCake 3
tiers flavor
“lemon”
Unit 7 Lesson 1 - Activity

tiers flavor
“lemon”
makeCake 3
Unit 7 Lesson 1 - Activity

tiers flavor
“lemon”
makeCake 3

“lemon
3

“lemon

3
Unit 7 Lesson 1 - Activity

“lemon
3

“lemon

3
Unit 7 Lesson 1 - Activity

Argument - the value passed to the


parameter

“lemon”

makeCake 3
tiers flavor
“lemon”

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
chocolate cake? What would 4 ”
that look like?

“chocolate

makeCake 4
tiers flavor

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


Arguments
time one that calculates the cost of
making a cake.
3

“lemon”

costCake 3
tiers flavor
“lemon”

Parameters
Unit 7 Lesson 1 - Activity

tiers flavor
“lemon”
cakeCost 3
Unit 7 Lesson 1 - Activity

Create two new local variables, flavorCost


and total.

Determine the value of flavorCost based on


the argument passed through the flavor
parameter

Calculate total using flavorCost and the


argument passed through the tiers parameter

???????
Unit 7 Lesson 1 - Activity

“lemon
3 ”

After running this, what


does flavorCost equal? 4
What does total equal? 12
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

We’ve called the


cakeCost function. It
tiers flavor
“lemon”
cakeCost 3
has returned the value
12.

12
But what happens to
that value?

How is it stored?
Unit 7 Lesson 1 - Activity

lamvoon”
r
“fle
Let’s return to variable
baggies!

tie11rs A function return

12
t
cakeCos
value can be stored in a
variable.

cakeCalculator
Unit 7 Lesson 1 - Activity

Here’s how this looks in Javascript:

var cakeCalculator = cakeCost(3, “lemon”);

After the expression is evaluated,


cakeCaculator stores the value 12.
Unit 7 Lesson 1 - Activity

We can also print to the console like so:

console.log(“Cake cost: ” + cakeCost(3, “lemon”));

Console

Cake cost: 12
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
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

Create a Cluster!
Let’s add the following three words into our AP CS Principles Unit Journal.
Create a vocabulary page within your journal, or use our digital template.

Start with the words, your initial definitions, and visual representations of the
words.

Argume
Parameter AP CSP
Unit
nt Return
Journal

You might also like