Function without parameter :
Below is the simple example for function without any parameter.
1. function is the keyword here
2. welcomeMessage is the function name.
3. welcomeMessage() is just calling the function.
So when you call control executes welcomeMessage() , it will call welcomeMessage function and executes the alert function inside that.
The below is little different because of variable declaration. See below,
First It will show alert message like Hello World! Then after It will show Welcome. Because of variable declaration places and due to let keyword its happening.
Above we are passing two parameters and both were executed inside the function.
function welcomeMessage() {
alert( 'Hello World!' );
}
welcomeMessage();
1. function is the keyword here
2. welcomeMessage is the function name.
3. welcomeMessage() is just calling the function.
So when you call control executes welcomeMessage() , it will call welcomeMessage function and executes the alert function inside that.
The below is little different because of variable declaration. See below,
let userName = 'Welcome'; function welcomeMessage() { let userName = "World"; // local variable let message = 'Hello, ' + userName; alert(message); } welcomeMessage(); alert( userName );
First It will show alert message like Hello World! Then after It will show Welcome. Because of variable declaration places and due to let keyword its happening.
Function with parameters :
We can also pass parameters to the function. See below example,function welcomeMessage(msg1,msg2) { alert(msg1);
alert(msg2); } welcomeMessage("sample","Test");
Above we are passing two parameters and both were executed inside the function.
Function with returning a value :
The below we can see a function which is returning a value. Example below.
function sum(a, b) { return a + b; } let result = sum(1, 2); alert( result ); // Output: 3
Here let result = sum(1,2) is the calling place and after executing the function , its returning value to result.
Conclusion :
These are the simple examples about function. Here the post we have used everywhere let keyword. We already saw let is a block level scope.
No comments:
Post a Comment