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 block of code that performs a specific function/task. They are re usable so you are able to perform them more than once.
Question 2

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

They are called arguments.
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.)

Not all of them, if you have one that is undefined, it will return as "undefined"
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 what is to be executed when the function is called upon.
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 execute the function by requesting it to perform its defined task.
Question 6

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

When a function has more than one parameter, you use a comma.
Question 7

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


function convertKilometersToMiles(km)
    return km * 0.6217;
                }
                

This is missing the opening curly brace. Indicated by the red ending curly brace in VS Code.
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 + "!");
                

The prompt function will return a value. It will return the text entered by the user.

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.