Functions in javascript

Functions in javascript

introduction.

so what is a function in javascript? Quite often we need to repeat a similar action in many places in the script, for example, we need to show a specific behavior whenever the user makes a specific action on my website we want to show a message telling the user "Are you sure you want to log out?" whenever he presses the logout button.

so functions are the main building block of the program they allow the code to be called many times without repetition, it is a block of code designed to perform a specific task and it is executed when it's being invoked.


Function syntax.

function name(parameter1,parameter2, parameterN){

}

a function is defined by a function keyword, followed by a function name, followed by parentheses?(), parentheses?can include parameter names followed by commas, as the function parameters are included inside the parentheses in the function definition, the arguments are the values received by the function when it is invoked.

note that the parameters behave as a local variable inside the function.

also, the code to be executed by the function is placed inside brackets {}.

Example.

in this example, we will just show/alert a message, so we will not be in need to include parameters in the function definition.

function showMessage() {

alert( 'Hello everyone!' );

}

let result = showMessage();

//output -> hello everyone.



Function Invocation(calling).

the code inside the function will be executed when something calls the function such as:

  • an event happened (user clicks a button).
  • being called in javascript code as in the example above.
  • self call.


Calling the function that needs information.

when calling the function that has a parameter, you specify the values it should use in the parentheses?that follow its name, these values are called arguments and they can be provided as variables or values.

arguments as values

let's consider we have a function that gets the area

function getArea(width , height){

return width*height;

}

now let's call the function and pass the arguments as a value.

getArea(5 , 7);


arguments as variables.

let's consider the same function getArea as above and we need to call the function and pass the arguments as variables.

width = 6;

height = 7;

getArea(width, height);


again what is the difference between parameters and arguments?

when we define the function and pass variables to the function inside the parentheses() this is called parameters.

when we call the function and pass values inside the parentheses() this is called arguments.



Getting a single value out of the function.

some functions return information to the code that called them, for example when they perform a calculation, they return the result.

function calculateArea(width, height){

var area = width*height;

return area;

}

let one = calculateArea(3,5);

let two= calculateArea(10,5);

this calculateArea() function returns the area of a rectangle to the code that called it.

inside the function, a variable named area is created. it holds the calculated area.

the return keyword is used to return a value to the code that called the function.


Note.

the interpreter leaves the function when return keyword is used, and if there are any subsequent statements in this function they would not be processed.


Getting multiple values out of the function.

functions can return more than one value using an array, for example this function calculate the area and volume of a box.

function getSize(width, height, depth) {

var area = width * height;

var volume = width * height * depth;

var sizes= [area , volume];

return sizes;

}

var areaOne = getSize (3, 2, 3)[0];

var volumeOne = getSize (3, 2, 3)[1];?









要查看或添加评论,请登录

mohammed hussien的更多文章

  • Mastering TestNG Annotations: Unlocking the Power of Java Testing

    Mastering TestNG Annotations: Unlocking the Power of Java Testing

    As Java developers, we all know how crucial testing is for maintaining high-quality code. If you’re using TestNG…

  • objects in JavaScript part 1.

    objects in JavaScript part 1.

    introduction: so what are objects in javascript? well, objects group together variables and functions to create a model…

    2 条评论
  • comments in javascript

    comments in javascript

    comments. so what is a comment in javascript, well a comment is being used to make code more readable and easy to…

  • variables in javascript

    variables in javascript

    INTRODUCTION: Simply, variables in javascript are like containers for storing data or we can consider it as naming the…

  • What is javascript?

    What is javascript?

    JavaScript is a scripting language that allows you to implement complex features and add interactivity to web pages, a…

    1 条评论

社区洞察

其他会员也浏览了