In this tutorial we look at how to write functions in Node.js. We’ll examine various way of working with functions.
We would cover the following topics:
- Defining and Calling a Function
- Parameters to Functions
- Function Scope
- Return Values and Inline Functions
1. Defining and Calling a Function
This is exactly similar to functions in JavaScript. The structure of a function is given below:
function functionName (parameters) { //body of function //optional return statement }
Now, functions in Node.js must return a value. So function definitions include an optional return statement. However, if the return statement is missing, then the function returns undefined.
After a function is defined, you can then call it. When you call a function, then it begins executing.Let’s take an example. We write a simple function that displays a greeting.
//Defining a function function sayHello(){ console.log("Hello Programmer!") console.log("Hope you're doing great!") } //we call the function here sayHello()
Write and run the code above to see the result.
2. Parameters to Function
A parameter to a function is enclosed in brackets after the functionName. A parameter is a variable used by a function during execution. For example, the program below is a function that takes on parameter name, and prints out a message using the name.
This means that the value of the parameter must be provided during the function call.
//Function definition function greeting(name){ console.log("Good morning " + name) console.log("Programming is fun, you know!") } //function call greeting("Kindson")
If you run this program, the output will be:
Good morning Kindson Programming is fun, you know!
If a function has more parameters than required, then the extra parameters are ignored.
Below is another function.
This function takes three numbers as parameter. It then calculates the sum and average of the numbers.
//Function definition function doSum(x, y , z){ console.log("The sum is ") console.log(x+y+z) console.log("The average is: ") console.log((x+y+z)/3) } //function call doSum(10, 20, 30)
3. Function Scope
Scope means the block of code where a variable is visible. So anytime a function is defined, the function block becomes a scope.
Variables defined inside the function block is in the function scope. Therefore, these variables are not visible (or available) outside the function block. However, variables defined outside the function are visible inside the function scope.
Let’s take an example.
The function below uses a variable from outside the function.
var car = 'Toyota camry'; function myFunction() { var car = 'Bugatti'; console.log(car); //car in function scope } myFunction(); console.log(car); //car outside the function scope
If you run the code above the output will be:
Bugatti Toyota camry
So what happens is this:
The car inside the function scope has the value ‘Bugatti’ while the car outside the function has a value of ‘Toyota Camry’.
Also note: if you have a variable with the same name inside and outside a function, then the variable declared inside has precedence within the function scope.
4. Return Values and Inline Functions
As mentioned earlier, function returns a value. So you can simple assign the return value to a a variable. For example, the code below contains a function that calculates the area of a circle. The return value is the area of the circle of given area. This return value is then assigned to a variable called area.
function getArea(radius) { return Math.PI * radius * radius } area = getArea(5);
Inline Functions
You can also choose to write this function in one line. In this case, you don’t give it a name. You just assign the function definition to the variable. This is what it would look like.
area = (function() { return Math.PI * 5 * 5 })(); console.log(area)
I recommend you try it yourself. Also remember to watch follow the video lessons for more clarifications.