When operating in JavaScript, an user might use a function. A function is a way they can organize their code to make it easier to create, maintain, and reuse. When using functions, they must have a name, the function must be passed through zero or more pieces of information, and the function returns a value. To keep functions efficient and less confusing, users might want to avoid repeating code over and over again unless needed. A simple function could look as basic as
function HelloWorld() {
alert(“HelloWorld”);
}
As you can see, a function must begin with the function keyword, then you have the function name (HelloWorld in this case) followed by parentheses, then the code in the function runs when the function is called. Calling a function just means that you write the function name followed by the parentheses again, but this time outside the initial function. Function arguments are when a function call will contain data that is passed into the function. A quick example would be function sum(num1, num2) {
alert( num1 + num2); }
sum(4, 5);
This function call would have the number 9 appear as the function arguments are added together. Return keywords can also be used to send back data to whatever initially started the function. When the function meets the return keyword, it will stop doing everything at that point and no code after return will run.
Leave a Reply
You must be logged in to post a comment.