JavaScript Function Definition
Usually a function is defined when a code will be used recursively. Functions used in other programming languages are also referred to as "subroutines". The function consists of two parts: name and body. Parameters can be passed to the function from the name section. The codes to be run on the body are also executed. While parameters can be passed to a function, values can also be returned via function names. I wrote that the function consists of two parts. The function body contains the commands to be executed.
We can also use the function over and over again with the function name. What we need to know to call (run) the function is the function name and the parameters the function takes.
Definition: Let's talk about how to define a function with JavaScript;
In JavaScript, every function is an object. To return a value other than the default value from a function, the return statement must be used. A function without a return value returns the default value. The default values in all functions except objects created with the constructor method are undefined. Spelling Rule: function name([parameter[, parameter[, ... parameter]]]) { expressions } Although the above definition is the most accurate definition rule, we can divide functions into 4 main groups for those who are new to the concept of functions.
1. Functions without parameters and return values
Example:
//defining the function
function greet(){
alert("hello world");
}
//using the function greet();
2. Functions with parameters and no return value
When a function takes parameters, the parameters that the function will take from outside are specified with commas in parentheses after the name of the function. The greet function below receives the values coming from outside with the name variable. It uses it with its own name variable and prints it to the screen. In the main program, the defined function is called twice with different parameters.
//defining the function
function greet(name){
alert("hello "+name);
}
//use of function
//we can call functions as many times as we want.
greet("good");
hello("ahmet");
3. Functions with no parameters and return values
It is mandatory to use a return statement in functions that have a return value. The value produced in the function is passed to the main program with the function name via the return statement. In the example below, the year value taken is transferred to the year variable with the yearGettir() function. The year variable is also printed to the screen with an alert.
//defining the function
function GetYear(){
return (new Date()).getFullYear();
}
//use of function
var year=yearGet();
alert(year);
4. Functions with parameters and return values
//defining the function
function numberSum(s1,s2){
var total=s1+s2;
return return total;
}
var number1=120;
var number2=250;
//use of the function
var result= numberSum(number1,number2);
alert(result);
JavaScript Anonymous Function Definition
While we can define a function with a name, it is also possible to define it anonymously.
Example: Defining a Multiplication Function with a Name
function product(x, y) {
return x * y;
}
Example: Defining the multiplication function anonymously
var product =function (x, y) {
return x * y;
}
Note 1: Anonymous functions are ideal definition methods that can be used for many JavaScript methods and properties that take functions.
Note 2: Defining anonymous functions is an important step for those who will learn the concept of jquery. Many operations in the jquery library are carried out as anonymous definitions.
Example: Example of reading values in an array. With anonymous function
var array=[12,25,85,45,74];
/*forEach method reads every value in the array*/
/*array.forEach(function)*/
array.forEach(function(incoming){
alert(incoming);
});
JavaScript Arrow Function /JavaScript Lambda Function
If you are interested in the C# programming language, you are more or less familiar with the concept of Lambda function definition.
With Arrow Functions included in the JavaSript language with EcmaScript 6, you can define an anonymous function and return a value.
Writing Rule: You can write it in the most comprehensive way as follows.
(param1, param2, …, paramN) => { expressions }
E.g.Appendix 1: Defining a simple squaring function with arrow function
//definition part
var squareAl = x => x * x;
//use of the defined function
window.alert(getsquare(10));
Example 1: Identification by classical method
//defining the function
functionSquare(x)
{
return x*x;
}
//use of the function
window.alert(getsquare(10));
Example 2: We can define the anonymous example above with the arrow function as follows.
var array=[12,25,85,45,74];
/*forEach method reads every value in the array*/
/*array.forEach(function)*/
array.forEach((element)=>alert(element));
Example 3: squaring the numbers 1,2,3 in an array
var array = [1, 2, 3];
var square1 = array.map(x => x * x);
alert(square1);
//With the classical method
var square2 = array.map(function (x) { return x * x });
alert(square2);