Unit 6 Lesson 8 Defining function with parameters

I was completely overwhelmed with this lesson because I could not figure out how the program knew what to do with information about “player” until I realized that the function that was in the example solution has functions with parameters.

There needs to be a connection/practice opportunity for this. I do not believe that students have had any experience with this. Please direct me to it if they have.

Thank you,
Beth

3 Likes

Just ran into the same problem. The parameter concept was new hadnt been introduced yet.

1 Like

Beth and Rothmana,

Completely agree that this is tricky. I introduce this as a code walk and progress through each line of code and stop to ask questions, mainly about why some variables are in quotes and some not - then making the connection between the player after “checkWin” and why that isn’t a string, and where else we see it in our code. Lesson 4 is great to see the 3 different types of planes and what happens if you use a string value to target one of the planes versus a parameter that passes in the value called.

Keep in mind that this is a more efficient way to code, but doesn’t mean your students have to do it this way. If your students can get it to run with all the additional code for each thing (plane, player, etc…) they can, and if a student or two got through this and is able to understand and use it, they can help explain to the class (and ask their permission to use their code as an example in later years).

Brad

2 Likes

Could I get a simple explanation of why some of the variables are in quotes and some aren’t? Is it just element-id’s that would act as a variable but still be in quotes? I am so accustomed to words within quotes as strings that don’t interact with the program, they are just printed.

Kristen,

Functions with Parameters are covered in Unit 6 Lesson 14 (so this post is referring to an earlier post that doesn’t fully explain) - so know that there is an additional resource out there.

So quotes around any word makes it a String value, or a series of characters strung together - this is one of the basic data types (although not primitive) that budding programmers use. Once you get to functions with parameters (sometimes called arguments) you are using a placeholder to refer to later code within the function that the parameter will refer to. In the example above, the function movePlayer takes in the player parameter and applies it to anything in the code within the function.

Try this example:

function addNumbers (number) { var sum = 5 + number; return sum; }

addNumbers(5) = 10 // (5 + 5 = 10) the second 5 is the number parameter
addNumbers(2) = 7 // (5 + 2 = 7) the 2 is the number parameter
addNumbers(23) = 28 // (5 + 23 = 28) the 23 is the number parameter

So number - not a string value, is used as the variable that the parameter is used through the function addNumbers to represent the placeholder for what the user defines. Often this parameter is assigned in an app when the user types something into a textfield and it’s applied to the function as a parameter.

Does that help at all?

Brad

I think it will help as I work up to and through Lesson 14.
Thanks

1 Like