JavaScript Functions Worksheet
Question 1What is a function, and why would you ever want to use one in your code?
Why use one? Functions keep code organized, reusable, and easy to maintain.
What do you call the values that get passed into a function?
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.
Do all functions return a value? (Note: this is a bit of a trick question - make sure to ask me about this in class.)
What is the 'body' of a function, and what are the characters that enclose the body of a function?
What does it mean to 'call', or 'invoke' a function (note that 'calling' and 'invoking' a function mean the same thing)?
You call a function by writing its name with parentheses (). If the function needs values, you put them inside the parentheses.
If a function has more than one parameter, what character do you use to separate those parameters?
What is the problem with this code (explain the syntax error)?
function convertKilometersToMiles(km) return km * 0.6217; }
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 + "!");
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.