The code in a function is not executed when the function is defined. It is executed when the function is invoked.
Some people use the term “call a function” instead of “invoke a function”.
example 1:
function number(a, b) {
return a * b;
}
number(10, 2); // Calling the function
number(10,2) means first value(10) will go to first argument(a) and second one(2) to second(b)
To call a function, you simply need to pass the required parameters along with the function name, and if the function returns a value, then you can store the returned value.
example2:
var hello = function() {
console.log(“i am saying hello”);
};
hello();//Calling the function
example3
function hello(thing) {
console.log("Hello " + thing);
}
hello(“world”)//calling a function
why function?
You can reuse code: Define the code once, and use it many times.
You can use the same code many times with different arguments.
run this roo
function hello(thing) {
console.log("Hello " + thing);
}
hello(“world”)//calling a function
hello(“sun”)//calling a function
hello(“moon”)//calling a function
@rcodeman Your explanation is clear and helpful, but I have a question about the “function call” section of your answer: Would you be passing “parameters” or “arguments”? Sometimes this can get confusing so I want to confirm I have it down right.
var greeting = function (name) {
console.log(“Great to see you,” + name);
};
greeting(“akhil”)
look the codes been typed there.here in the variable name called “greeting” i define a function as function(name) which tells us that the variable "greeting " is now a function and going inside a function there is new as +name that is parameter which is also called the formal parameter. here we call the function “greeting” assigning value akhil which passes to parameter and which will printed via log of console of that statement.
A function takes in inputs, does something with them, and produces an output.
Here’s an example of a function:
var sayHello = function(name) {
console.log('Hello ’ + name);
};
First we declare a function using var, and then give it a name sayHello. The name should begin with a lowercase letter and the convention is to use lowerCamelCase where each word (except the first) begins with a capital letter.
Then we use the function keyword to tell the computer that you are making a function
The code in the parentheses is called a parameter. It’s a placeholder word that we give a specific value when we call the function. Click “Stuck? Get a hint!” for more.
Then write your block of reusable code between { }. Every line of code in this block must end with a ;.
You can run this code by “calling” the function, like this:
sayHello(“Emily”);
Calling this function will print out Hello Emily.
Instructions
On line 11, call the greeting function and put in a name that you want the greeting function to include.
Press “Save & Submit Code” and see the function get into action! Saves you so much time.
?
Really can not wait for this semester to be over! Then I can focus on phase one and getting into the immersive program. Thanks for all of your help much appreciated.
I am so frustrated I am not getting this!!! I am following the instructions to best of my ability and Im still getting error messages now its saying I forgot to call the function now pass it a name. Can someone please help me