Functions

DevJunctionPoint
0

 



What is a function? 

  • A function is a block of code to perform a specific task.
  • A function is declared by a function keyword followed by a name, followed by parentheses ().
  •  A function can also take a default parameter. To store data in a function, a function has to return certain data types.

  • A parenthesis can take a parameter. If a function takes a parameter it will be called with argument.
  • A function can be declared or created in a couple of ways:

  1.  Declaration function
  2.  Expression function
  3.  Anonymous function
  4.  Arrow function

 Function Declaration Syntax:


//declaring a function without a parameter
function functionName() {
  // code goes here
}
functionName() // calling function by its name and with parentheses

  •  The function can be declared without a parameter.

// function without parameter,  
a function that makes a number square
function square() {
  let num = 2
  let sq = num * num
  console.log(sq)
}

square() // 4

// function without parameter
function addTwoNumbers() {
  let numOne = 10
  let numTwo = 20
  let sum = numOne + numTwo

  console.log(sum)
}

addTwoNumbers()
/ a function has to be called by its name to be executed


Function returning value
  •  A function can also return values, if a function does not return values the value of the function is undefined.

function addTwoNumbers() {
      let numOne = 2
      let numTwo = 3
      let total = numOne + numTwo
      return total

  }

console.log(addTwoNumbers())


 Function with a single parameter Syntax:


// function with one parameter
function functionName(parm1) {
  //code goes her
}
functionName(parm1) // during calling or invoking one argument needed


Example:


function square(number) {
  return number * number
}

console.log(square(10))


 Function with two parameters:


// function with two parameters
function functionName(parm1, parm2) {
  //code goes her
}
functionName(parm1, parm2) // during calling or invoking two arguments needed



function sumTwoNumbers(numOne, numTwo) {
  let sum = numOne + numTwo
  console.log(sum)
}
sumTwoNumbers(10, 20) // calling functions
// If a function doesn't return it doesn't store data, so it should return

function sumTwoNumbers(numOne, numTwo) {
  let sum = numOne + numTwo
  return sum
}

console.log(sumTwoNumbers(10, 20))


Function with many parameters


// function with multiple parameters
function functionName(parm1, parm2, parm3,...){
  //code goes here
}
functionName(parm1,parm2,parm3,...)
//During calling or invoking three arguments needed



//This function takes an array as a parameter and sums up the numbers in the array
function sumArrayValues(arr) {
  let sum = 0;
  for (let i = 0; i < arr.length; i++) {
    sum = sum + arr[i];
  }
  return sum;
}
const numbers = [1, 2, 3, 4, 5];
    //calling a function
console.log(sumArrayValues(numbers));


Anonymous Function
  •  Anonymous function or without a name

const anonymousFun = function() {
  console.log(
    'I am an anonymous function and my value is stored in anonymous'
  )
}


 Expression Function
  • Expression functions are anonymous functions. After we create a function without a name and we assign it to a variable. To return a value from the function we should call the variable. 

// Function expression
const square = function(n) {
  return n * n
}

console.log(square(2)) // -> 4


 Self Invoking Functions
  • Self-invoking functions are anonymous functions that do not need to be called to return a value.

(function(n) {
  console.log(n * n)
})(2) // 4, but instead of just printing
if we want to return and store the data, we do as shown below

let squaredNum = (function(n) {
  return n * n
})(10)

console.log(squaredNum)


Arrow Function
  • The arrow function is an alternative to writing a function, however, function declaration and arrow function have some minor differences.
  • The arrow function uses the arrow instead of the keyword function to declare a function. Let us see both function declaration and arrow function.

// This is how we write normal or declaration function
// Let us change this declaration function to an arrow function
function square(n) {
  return n * n
}

console.log(square(2)) // 4

const square = n => {
  return n * n
}

console.log(square(2))  // -> 4

// if we have only one line in the code block, it can be written as follows, explicit return
const square = n => n * n  // -> 4


 Function with default parameters
  • Sometimes we pass default values to parameters, when we invoke the function if we do not pass an argument the default value will be used. Both function declaration and arrow function can have a default value or values.

// syntax
// Declaring a function
function functionName(param = value) {
  //codes
}

// Calling function
functionName()
functionName(arg)



function generateFullName(firstName = 'naveen', lastName = 'ram') {
  let space = ' '
  let fullName = firstName + space + lastName
  return fullName
}

console.log(generateFullName())
console.log(generateFullName('David', 'Smith'))



function calculateAge(birthYear, currentYear = 2019) {
  let age = currentYear - birthYear
  return age
}

console.log('Age: ', calculateAge(1819))

  • Let us see how we write the above functions with arrow functions

// syntax
// Declaring a function
const functionName = (param = value) => {
  //codes
}

// Calling function
functionName()
functionName(arg)




const greetings = (name = 'navin') => {
  let message = name + ', learn js'
  return message
}

console.log(greetings())
console.log(greetings('navin'))




Tags

Post a Comment

0Comments

Do leave your comments

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Ok, Go it!
To Top