Word Blanks Challenge - functions?

Word Blanks Challenge - functions?
0.0 0

#1

Hello,

I’m a bit confused, trying to understand “function” at the top and the given words at the bottom. How does the function know what words go with which part of the function?

function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) {
var result = "";
// Your code below this line

result+= "My "+myAdjective+" "+myNoun+" "+myVerb+" very "+myAdverb+".";  

// Your code above this line
return result;
 }

 // Change the words here to test your function
 wordBlanks("dog", "big", "ran", "quickly");

#2
  1. At the top is the function declaration:

    function wordBlanks(...

  2. Inside the ( parens ) are one or more “arguments” - variables passed into the function:

    function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) {...

  3. Declaring a function does not execute it; it has to be called:

    wordBlanks("dog", "big", "ran", "quickly");

    That executes wordBlanks, passing four arguments to it.

FYI: paste code in between triple back-ticks. See here for more:


#3

The function assumes that the parameters passed (e.g. “dog”,“big”) are in the same order as the variables in the function declaration (e.g. myNoun, myAdjective). As such, it is entirely possible for someone to put a verb in place of a noun.

This is why when you will see function calls with ‘null’ passed as a parameter. The function has no way of knowing that you just decided not to have a value for a parameter.

As you might imagine, placing the incorrect parameter in a function call can have strange consequences. As such, developers often devote a certain amount of effort to checking the parameters. Sometimes it might be a simple as making sure that the parameter passed is the correct type (e.g integer, string) other times it might be more complex.


#4

thank you … cleared the fog


#5

thanks for the markdown tip …