Functions

Functions are a way to store series of steps that we do to perform certain actions.

How to create a function

  1. Function(The keyword that create a box)
  2. addTwoNumber(Name of the Created box)
  3. Curley Brackets ( {} )
  • So whatever inside curley brackets is known as body of the function.
  • The body of the function containing the series of steps.
function add (){

}

Function call or function Execution
To execute the  function we have to use open and close bracket or 
parathesis -() after  the function name .

add()

Parameter

The value that you pass inside the parathesis after the function name.

ex-

function sayHello(name){

}

Argument

The value that you pass when callled a function is known as argument.

ex-

sayHello("john")

Understanding Input and Output of a function

- Function with Input

Every time we call a function. we can pass a name and value changes to tat specific name that is known as function

  • When we call a function we can also pass input to the function.
function sayHello (name){
console.log(`Hello ${name}`)
} (1- Same function)

sayHello("John")
sayHello("Arya")
sayHello("Bran") (2- Different Input)

Output
Hello John
Hello Arya
Hello Bran (3- Different Message)

- Function with Output

For output from the function there is special keyword that we should be using and that keyword is called @return.

function sayHello(name){
return `I am ${name}`
}

sayHello("lovekush")

D/w Argument and parameter

  • Argument are real value
  • Argument are pass when you calling a function
  • To store the argument we use paramenter
  • Parameter is define when you defing the function
  • parameter are placeholder

Rule Of the functions

  • When you calling a function it always return a value
  • If the fucntion contain any return statement it will always return Undefined
  • If the fucntion return statement it will return the value on the RHS

D/w Return statement and Non-Return statement

Return statement

  • When you use return statement you see (<.) aerrow pointing right side

  • When aerrow(<.) pointing outside you can store the it inside variable .

ex-

addNumber(12,12)
<. 24
you can store it
let sum = addNumber(12,12)
<. sum
<. 24

Non-return statement

  • When you caaling a function it always return a value.
  • When function doesnt contain any return statement it always returnUndefined`

ex-

function sayHello(name){
 console.log(name)
}
let message = sayHello("lovekush")
message
lovekush
<. Undefined

Nature of Scope

  • Variable defined outside can be accessed Inside but variable define Inside Cannot be accessed Outside

ex-

let username = "John"   => Outer Variable

function sayHello () {
let username = "lovekush"  => Inner Variable
return `Hello ${username}`
}

let message = sayHello()  => Outer Variable

Note

Whenever you dont pass any argument the value of that parameter default toUndefined`

Default Parameter

Default parameter is a way to change the default value by doing something like this-

          (when the value is not defined
           default it to zero(0) )
function add ( a=0, b=0  ){
console.log(`The value of a is ${a} and b is ${b}`)
return a+b
}
add(0,"sum")

output-
0 , "sum"