How to call a function

Hi,

still getting my JavaScript feet wet

i have been struggling with this one

here is what i am working on

<script>

        function bark(name, weight) {

            if (weight > 20) {

                console.log(name + " says WOOF WOOF");

            }else {

                console.log(name + " says woof woof");

            }
 
        }

    </script>

and my function name and arguments (i do get that)

bark ("rover, 23);

if i call the bark function, the arguments are assigned to the parameters

i dont get how the name (“bark”) and weight (20) are assigned as the given function name and weight code (before any thing has been assigned)

i think i am missing a step here

i hope i am making sense … tricky concept for me

please pass on the proper code for calling this function, i will study it and maybe learn something!

and i am happy to see any other advice here!

MANY THANKS!

When the JavaScript interpreter gets to the function called bark, it doesn’t run the code that’s in the function. Instead it just remembers that there is a function there called bark.

function bark(name, weight) {
    ...
}

Whenever you use the bark function, which is the word bark followed by information in () parenthesis, it then passes the arguments that you give it to the function.

For example with bark("rover", 23) the first argument is the string "rover" and the second argument is the number 23

When those arguments are passed to the bark function, they are received inside of the function as parameters in the same order that they’re given. With the bark function, those parameters are called name and weight

The argument "rover" is received by the bark function as parameter called name inside of that function. The other argument 23 is received by the bark function as a parameter called weight.

When the bark("rover", 23) command takes place, that is when JavaScript goes to the bark function to run the code that’s inside of there. When that function finishes, code execution returns to whatever is after the `bark(“rover”, 23) command.

The following tweet from Wes Bos is the best way I’ve seen to put many of these terms into context:

1 Like

ok,

i got it up to here

i have the function name: bark

i have the 2 arguments (“rover”, 23);

i am confused how the arguments become parameters which then are applied to the body’s weight and name… how does the body’s name and weight know i want “rover” and 23? where do i put that?

how do i code the arguments name to be “rover” and weight to be 23 then have that applied to the name and weight in the body…

or how does function bark(name, weight) become the code for “rover” and 23?

how about this… where do i enter the call and how do the parameters fill?

i get how it should look ultimately, just not how it gets there

i want you to know i sincerely appreciate your time and trouble!

boy if this is simple and i just don’t get it…

What you have there is called a function definition. It defines a function, but none of the code gets run until you invoke the function.

You call, or invoke, a function by using the name of the function followed by opening and closing parenthesis.

bark()

When you invoke the bark function with no arguments, the bark function will then occur with undefined as its parameters.

When you invoke the bark function with some arguments, JavaScript passes those arguments to the function.

bark("Rover", 23);

The function then receives those arguments as its own parameters. A handy rule of thumb is that arguments are given, while parameters are received.

Usually it is done after the function has been defined. It can be done before the function definition too, but there can be some edge-cases that just make it easier and more reliable when done afterwards.

The parameters default to all being undefined. When you call the function with some number of arguments, those are passed to the function in the same order.

If you don’t use enough arguments, the remaining parameters will be undefined. If you use too many arguments, the extra arguments are just ignored and don’t affect the function parameters.

2 Likes

AH HA!

BINGO!

i got it now

took some time and deducing, but i figured it out

want to know where my trouble was?

so silly looking back…

simply code the function name and the arguments… that’s it… so simple now!!

tough one for me

thanks you SO much for your help!!

2 Likes

You’re most welcome. As a final thing, I stumbled across this function-based humor today.

2 Likes

funny!

i am new to code humor!

follow up…

i am not clear what happens when a function has no arguments… help me out?

how do i invoke from a separate js file… say code.js?

again, thank you for your time, guidance and patience!

I think that I answered that in a recent post above:

That’s done in exactly the same way.

hmm, i am sorry

i meant a function with out parameters

i tried


<script>

function talkingToAFriend () {

alert("Hi! whats your name?");

}

talkingToAFriend ();

    </script>

the alert showed “Hi! whats your name?”

is that all to it?

i dont understand what is happening here

thanks for the help!

Yes, that’s it. Although there really shouldn’t be a space between the function name and the parenthesis.

ok!

thanks again!

i will give you %0.00000000001 of my first million!

sound fair?

No thank you, I’m good.