JavaScript Functions Worksheet

Question 1

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

a function is used so you don't have to repeat your coding!
Question 2

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

the values a function has passed in is called parameters!
Question 3

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

the body of the function is what's getting passed through! the characters that enclose the body are curly braces.
Question 4

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

calling it means we are gonna give it a value and see what it returns.
Question 5

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

a comma is what you'd use to seperate parameters
Question 6

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


function convertKilometersToMiles(km)
    return km * 0.6217;
}
                

There's no curly brace after the parentesses. the function doesnt know what's in the body.
Question 7

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 + "!");
                

The alert would return whatever the user's name is because the prompt asked them to enter the 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.