Skip to main content

DAY 9 - JULY 30

FUNCTION


BASIC FUNCTION SYNTAX


function  f.name(parameter)

{

}



EVERY TIME WE NEED TO CALL/INVOKE THE FUNCTION


function add(ab) {
  
   console.log(a + b)
}

 var a = add// function add(a, b)
 var b = add(3,5//8


console.log / return inside function


function add(ab) {
      console.log(a + b)
a - b
}


Console.log(a+b) will display the output directly

If u want to use the output of this function as data for other line u need to use return .

If we use both it will print console and log immediately and store the return value in the variable



return RELATED PROBLEMS

PROBLEM 1

function myfun(ab) {
    console.log("hi");
    if (a == 10) {
        return a - b;
    }
    console.log(a + b);
    return a + b;
    console.log("hello");
}


a1 = myfun(1020);
console.log(a1); //hi -10

b1 = myfun(2010);
console.log(b1); // hi 30 30


PROBLEM 2


var m1 = 10;
m1 = 20;
function f1(m1)
{
if(m1 == 10)
{
  m1 = 20;
   return 30;
}
if(m1 == 11)
{
  return;
}
return 30;
console.log(m1);
}

m1 = f1(10);
console.log(m1); //30
m1 = f1(11);
console.log(m1); // undefined

If Parameter Count < Argument Count then the remaining parameters are consider as undefined


If Parameter Count  > Argument Count then the remaining parameters are omitted

Because of this Function Overloading is not possible in JavaScript

function add(ab,c) {
    
    return a +b+c
}

 var a =10,20,30)//60
 var b = 10//NaN (10+undefined+undefined)


FUNCTION OVERLOADING

 

function add (a,b,c)

function add (inta,intb)

function add (inta,intb)

In above case if user gives only two inputs a, b then automatically JavaScript take value c as undefined and automatically execute the first add thats why Function Over Loading is not possible in JavaScript



DEFAULT PARAMETER

In javascript we can able to set only the last parameter as default parameter we can’t set first or middle parameter as default parameter.



function add(a=10,b,c) {
    
    return a+b+c
}

 var a = add(20,30)// 20+30+undefined = NaN


 function add(a,b,c=10) {
    
    return a+b+c
}

 var a = add(20,30)// 20+30+10 =60


ANONYMOUS FUNCTION

Function without name and passed inside the variable.To call this function we need to call the variable


var add =function(a,b,c) {
    
   return a+b+c
}

 var a = add(20,30,10)// 20+30+10 =60


IIFE - Immediately Invoked Functional Expression

IIFE functions are wraped and called immediately .These kind of functions are called only once.



(function(a,b,c) {
    
  return a+b+c
})(10,20,30//60



INLINE FUNCTION

   When we declare a function inside a function we can’t call that inside function alone in program those kind of functions are called as inline function.




function x() {
    console.log("hi")
    function add(a,b){
    console.log(a+b)}
    add(4,6)
      
    }

In the above example we can't call the function add separately this is called as inline function

 

CALL BACK FUNCTION

When you are calling outside function inside any other function is called call back. Passing function into the function


EXAMPLE PROB FOR CALL BACK FUNCTION


Write 4 different functions add,sub,mul and div.And put all  function inside array

var operator = [add,sub,mul,div]; and then write a function maths

function maths(ind,arr)
{

val1 =5;

val2 = 10

(write code to call add,sub,mul,div)

}

SOLUTION   

https://day9-callback.netlify.app/


ARROW FUNCTION

var add = (a,b=> a+b;

add(1,2//3


ARRAY METHODS YOUTUBE VIDEO



Comments

Popular posts from this blog

CODEKATA BITWISE

Given a number N and an array of N elements, find the Bitwise XOR of the array elements. Input Size : N <= 100000 Sample Testcase : INPUT 2 2 4 OUTPUT 6   // ONE TESTCASE PASSED const   readline  =  require ( "readline" ); const   inp  =  readline . createInterface ({    input :   process . stdin }); const   userInput  = []; inp . on ( "line" , ( data )  =>  {    userInput . push ( data ); }); inp . on ( "close" , ()  =>  {    var   N  =  Number ( userInput [ 0 ]);    var   str  = String ( userInput [ 1 ])    var   arr  =  str . split ( ' ' ). map (( val ) => Number ( val ))       var   first  =  arr [ 0 ]       for ( i = 1 ; i < N ; i ++)   {  first  = first ^ arr [ i ]}    console . log ( first ...

DATA BASE

  DATA BASE Backend deals with Server and Data Base CLOUD COMPUTING Cloud – Cloud Computing -- Rented Computer Service Ram, Processor, OS, Storage WHY WE PREFER CLOUD COMPUTING 1. Capital Investment (at initial stage renting is better than purchasing) 2. Maintenance Problem (Power Backup, Hard Disk, Upgrade) 3. Easily Swappable (Scale Up & Scale Down) (festival time ecommerce need extra storage and facilities after that it may need less, College websites during exam time (E.g)   Anna university website ,recent time – income tax website – pan card & aadhar card linking) 4.   Don’t need any separate place to keep those Systems. TOP COMPANIES AWS – Amazon Web Service - Netflix GCP – Google Cloud Platform Azure - Microsoft Azure WHY LINUX PREFERRED THAN OTHER OS Linux, Windows, Mac 1. Open Source 2.   Secure than Windows & Mac. 3. Command Line Usage In the Linux OS, the command line is a very handy and powerful tool used for...

DAY 15 - AUG 08

  DOM – DOCUMENT OBJECT MODE   HTML DOC –Browser – Browser Engine(html parser) – DOM   Tree structure takes less time for searching that’s why we prefer tree structure in DOM. Why we prefer object model? Object model means OOPS concept based it have features like data abstraction, encapsulation…. Window   -- Window is the default thing available in the Browser’s JavaScript Engine. Document -- Document is property of Window Screen – Physical dimension of   the browser   Draw some sampl tree We can create html with the help of tag and make it dynamic(to do some action) by adding   javascript   using script tag then why specifically we need this dom à Will understand in future classes that we can’t do something directly with the tag.   DOM MANIPULATION   var element =document.createElement(‘tag’) Element.setAttribute(“attribute”,”name”) If we have morethan one same attribute for the element we can’t use s...