TUTORIAL : Creating and calling the Function, it Returns The Value Function (JavaScript) as well as a Sense of Function in JavaScript

Words
1231
Reading
6 min
Listen
Play
9y

What will I learn?

  • Sense of Function in JavaScript
  • Creating and calling the Function
  • Returns The Value Function

Requirements

  • Web Consule
  • PC (Windows/Linnux)

Difficulty

  • Bassic

The content tutorial

Function

To create a great program, it is broken into smaller sub program, then jointly build applications end. In JavaScript programming, sub programme is known by the term function. Function in JavaScript is also quite unique, because it can be stored into a variable, or as the argument. The term programmingnya, which function in JavaScript is the called as First-class citizen. In addition each variable in the function have a scope or scope is limited. In this chapter we will discuss about the function of JavaScript, as well as how to use.

Sense Function

Function or in English is called as a function, is a collection of program code that is designed to accomplish a specific task, and is part of the main program. Function needed to break up the flow of large programs into several small programs for easy on manage. Every function had the duty and function of each. Could have been inside a function, there is another function, depending on the complexity of the problems that want to solve. JavaScript itself has hundreds of built-in functions can be used, such as the alert () function. In addition, we can also create your own functions.

The following basic format creation function in JavaScript:

image.png

function function_name (argument1, argument2,...) { 
statement;
 statement;
 return value; 
}

After the keyword function, followed by function_name. Function_name is the name of the function. Writing function names follow the rules of the identifier, or in other words follow the rules of making variables, i.e. should not be prefixed numbers, must not contain any spaces, etc

Each function can have 1 or more of the argument. The argument is a variable that acts as the input values into the function. The argument was written in parentheses after the function_name. Block function is marked with brackets kurawal. This is where the JavaScript code that forms the function is written. The command return is used as the output of the function. Writing the argument and command return is optional and may not be written. If you've yet to learn a programming language, my explanation above, it may be difficult to understand. But don't worry, I'll explain the steps use sample source code.

Creating and calling the Function

As the first practice, I will design a morning () function that displays the words "good morning" in 4 languages:

image.png

1 function pagi(){ 
2 console.log("Selamat Pagi"); 
3 console.log("Good Morning"); 
4 console.log("Ohayou Gozaimasu"); 
5 console.log("Buenos Dias"); 
6 }

This function consists of 4 lines of JavaScript code, it displays my 4 pieces of string using the command console. log (). The above function has no argument, but still should be written with the parentheses empty: morning (). This is what distinguishes a functions with other identifiers such as variables. If you find I write a word followed by empty parentheses, meaning it's a function. Write a function like this is called also with function declaration or short larasian function. If you run the above code, it will not appear anything. Because we have to "call" the function first. How do you do? Simply write the name of the function, as the following example:

image.png

1 function pagi(){
2 console.log("Selamat Pagi");
3 console.log("Good Morning"); 
4 console.log("Ohayou Gozaimasu");
5 console.log("Buenos Dias"); 
6 }
7
8 pagi();

image.png

Once the declaring function in the morning (), I run it by way of writing the name of the function: morning ();. Run the function this way is known as the calling function (callinga function). Another term is running, executing, dispatching, or invoking a function. What if we try to call this function multiple times?

image.png

1 function pagi(){ 
2 console.log("Selamat Pagi"); 
3 console.log("Good Morning"); 
4 console.log("Ohayou Gozaimasu"); 
5 console.log("Buenos Dias");
6 } 
7 
8 pagi(); 
9 pagi(); 
10 pagi();

image.png

Since there are 4 greetings in 1 time, when calling a function in the morning () is called for as much as three times, will perform 12 greetings. This is the practicality of using a function, we can easily run the same code repeatedly, and can be called at any time.

Returns The Value Function

Morning () function that we designed before this very practical to display 4 greetings in each time calling. But what if I want a greeting is not directly perform, but accommodated first into variable (to later be processed again)? We can use the keyword return. The command will return a return value when the function is called. Let's see an example of its use:

image.png

1 function pagi(){ 
2 return "Selamat Pagi"; 
3 }

This time I changed the function of morning () being just 1 line: return "good morning". Thus, I can run it as follows:

image.png

1 function pagi(){ 
2 return "Selamat Pagi"; 
3 } 
4 
5 var salam = pagi(); 
6 console.log(salam); // "Selamat Pagi"

Note how function calls in the morning (), this time I dial using the assignment operator. In other words, the results of the function in the morning (), will be stored into the variable greeting. After that, what the content of the variable greeting? Is the string "good morning". In addition to them into a variable, I could also write it like this:

image.png

1 function pagi(){ 
2 return "Selamat Pagi"; 
3 } 
4 
5 console.log(pagi());

What can guess what is shown by the command console. log (())? The String "Good Morning". This stems from the commands return "good morning" which is directly displayed by console. log (). In addition to the return value, another effect of the return is, will instantly stop function, as the following example:

image.png

1 function pagi(){ 
2 return "Selamat Pagi"; 
3 console.log ("Good Morning"); // tidak akan pernah di eksekusi 
4 } 
5 
6 console.log(pagi()); // "Selamat Pagi"

I put the 1 line after the command return, i.e. console .log ("Good Morning"). This line will never be executed, because at the moment the program code to process a return, the function of the morning () is considered complete. In the previous example I return the string value directly written after return. We can also return the value of the form variable:

image.png

1 function pagi(){ 
2 var salam = "Selamat Pagi"; 
3 return salam; 
4 } 
5 
6 console.log(pagi()); // "Selamat Pagi"

This example is very similar to the previous one. It's just the string "good morning" this time I put into the variable greeting. Furthermore, the variable greeting is what return..

Curiculum



Posted on Utopian.io - Rewarding Open Source Contributors

TUTORIAL : Creating and calling the Function, it Returns The Value ... | Ecency