JavaScript Functions Worksheet
Question 1What is a function, and why would you ever want to use one in your code?
Functions allow you to execute a group of statements multiple times in your program without having to repeat code. This can reduce the number of errors that could occur. They are used every single day.
What do you call the values that get passed into a function?
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 you need a result from a function, then you would use return to obtain that result.
There was a comment I came across recently online that stated, "Some functions simply do what they have to do and they don't need to give any result."
What is the 'body' of a function, and what are the characters that enclose the body of a function?
The characters that enclose the body of a function are 'curly braces', also known as '{}'.
For Example: function uselessFunction(parameter){body of function;}
What does it mean to 'call', or 'invoke' a function (note that 'calling' and 'invoking' a function mean the same thing)?
For Example: uselessFunction("parameter");
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 + "!");
The prompt function returns the input value from what the user submitted, storing the value to the variable name.
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.