JavaScript Functions Worksheet

Question 1

What is a function, and why would you ever want to use one in your code?

What is a function? It’s a reusable block of code for a specific task.

Why use one? Functions keep code organized, reusable, and easy to maintain.
Question 2

What do you call the values that get passed into a function?

The values you give to a function are called arguments.

When you make a function, you write parameters as placeholders for those values. Then, when you use the function, you pass in arguments, which are the real values for those placeholders.
Question 3

Do all functions return a value? (Note: this is a bit of a trick question - make sure to ask me about this in class.)

If a function doesn’t use return, it just gives back undefined, which means "no value." But if a function has a return statement, then it will give back whatever you told it to.
Question 4

What is the 'body' of a function, and what are the characters that enclose the body of a function?

The body of a function is where all the code goes that the function will do when you call it. The body is put inside curly braces { }.
Question 5

What does it mean to 'call', or 'invoke' a function (note that 'calling' and 'invoking' a function mean the same thing)?

To call or invoke a function means to make the function do its job. When you call a function, you tell it, "Go run your code now."

You call a function by writing its name with parentheses (). If the function needs values, you put them inside the parentheses.
Question 6

If a function has more than one parameter, what character do you use to separate those parameters?

If a function has more than one parameter, you separate them with a comma ,.
Question 7

What is the problem with this code (explain the syntax error)?


function convertKilometersToMiles(km)
    return km * 0.6217;
}
                

The problem with this code is that it’s missing the opening curly brace The function needs { } around the code it runs. Without the first , JavaScript doesn’t know where the function code starts, so it gives an error.
Question 8

In the code below, there are two functions being invoked. Which one returns a value? Explain why you know this.


const name = prompt("Enter your name");
alert("Hello " + name + "!");
                

In the code below, the function prompt is the one that gives back a value.

Here’s why:

prompt("Enter your name") is a function that pops up a box asking the user to type something. Whatever the user types is given back by prompt as a value, which is then saved in name. alert("Hello " + name + "!") does not give back a value. It just shows a message on the screen and doesn’t return anything. So, prompt gives back what the user types, but alert only shows a message and doesn’t give back any value.

Coding Problems

Coding Problems - See the 'script' tag below this h3 tag. You will have to write some JavaScript code in it.

Always test your work! Check the console log to make sure there are no errors.