JavaScript Functions Worksheet

Question 1

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

A function is a piece of code that performs a task. It can be called or invoked many times, which can save lots of time and improve efficiency.
Question 2

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

They are 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 is the code that actually does the task. It is enclosed in curly brackets that are usually broken apart to different lines.
Question 4

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

If you call or invoke a function you are just telling the computer to use or run the function.
Question 5

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

You would use a comma.
Question 6

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


function convertKilometersToMiles(km)
    return km * 0.6217;
}
                

There isn't an opening curly bracket after the parenthesis.
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 prompt function returns a value because it stores the value that the user enters as the name variable.

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.