Javascript Fundamentals #Function and Function Expressions
Do you want to know the basics of JavaScript functions and function expressions? Writing functions is an essential part of JavaScript programming, as it allows us to make our code more efficient and break up large pieces of code into simpler, easier-to-manage chunks. In this tutorial, you'll get an overview of various types of functions in JavaScript and how to write them with examples.
JavaScript Function
What is a JavaScript function? A JavaScript function is a block of code that performs a specific task or action. With the help of functions, complex problems can be broken down into smaller, more manageable components. Furthermore, JS has built-in functions such as Math.sqrt() which will help you solve various math problems quickly and easily. In this tutorial, you will learn how to create your user-defined functions and use them in your programs.
How to Write and Declare Functions in JavaScript
Declaring a function involves writing the keyword ‘function’, followed by a descriptive name and pair of parentheses. Inside the parentheses will be any parameters, and after them, curly brackets will comprise the body of the function. For example:
function addNumbers(num1, num2) {
return num1 + num2;
}
How To Call a Function in Simple Terms
In code, a function needs to be called for it to execute. Calling a function involves providing its name followed by parentheses and the arguments needed if any. For example, the above program declares a function named greet(), which can then be called by typing greet() and any necessary arguments within the parentheses.
Example:
// program to print a text
// declaring a function
function greet() {
console.log("Hello there!");
}
// calling the function
greet();
How To Call a Function Parameters
When calling a function with parameters, you need to provide the parameter values as arguments in the call. The argument values are assigned to the corresponding parameters when the called function is activated. The number and type of arguments must match the number and type of parameters declared when defining the function or an error will occur.
Example :
// program to print the text
// declaring a function
function greet(name) {
console.log("Hello " + name + ":)");
}
// variable name can be different
let name = prompt("Enter a name: ");
// calling function
greet(name);
How To Call Function Return
A return statement is used to define the end of a function and assign a value that will be returned to the caller of the function. Any code after the return statement is executed, with the value being undefined if no value is explicitly stated as returned from the function.
Example :
// program to add two numbers
// declaring a function
function add(a, b) {
return a + b;
}
// take input from the user
let number1 = parseFloat(prompt("Enter first number: "));
let number2 = parseFloat(prompt("Enter second number: "));
// calling function
let result = add(number1,number2);
// display the result
console.log("The sum is " + result);
Functions are beneficial to use in programming because they make code reusable, easier to understand, and more efficient. Declaring a function once can be used multiple times throughout the program. This helps break down a complex set of tasks into smaller chunks and makes the program easier to read and debug. Additionally, this also increases the efficiency of the code as functions remove redundant lines of code.
Understanding Function Expressions in JavaScript
Function expressions are one of the fundamentals of JavaScript and understanding how to write them is essential for navigating code.
Function expressions involve writing a function as part of a larger expression, which can be extremely helpful in creating more powerful and dynamic programs. To explain further, let's look at an example that defines a function as an expression:
// program to find the square of a number
// function is declared inside the variable
let x = function (num) { return num * num };
console.log(x(4));
// can be used as variable value for other variables
let y = x(3);
console.log(y);
These functions can be anonymous, meaning they are unnamed or named where the function is given an identifier that can be used to call it. In the example above, the function is stored as x and then called using the variable name.
Finish.