4. CPP Functions

 

Previous (3.cpp) <<

Next (5.cpp) >>

Function is a block of code that performs some operation :

example


returnType functionName( inputdata = defaultValue ){
    code to run
    return (dataOfReturnType );
}

must declare the function before it is used
so that compiler gets to know there will be a function looking like this

* if function definition is before function call then declaration is compulsory
* else must


2. Function Overloading

      // have multiple function with same name , having different parameters , or different number of parameters

 

    // note that: return type have no significance for overloading
    // only parameters distinguish function
    to_string(integer) = is a string data

 

 3. Inline Function
    // 
the inline keyword makes the function similar to macro defination
            the function code is replaced everywhere it is called , uses more memory but became faster
    // it is not guranteed that inline makes function inline everywhere , it depends on compiler what is suitable if code in function is smaller makes it inline

   
inline int calcHouseArea(int l, int b, int bl, int bw ) {
        return l*b +bl*bw;
    }
    int main(){
        cout << calcHouseArea( 10 , 12 ,8, 3 );
   
    }

4. Stack Mechanism
    of function call

    int calcHouseArea( int l , int b , int bl , int bw ){
        return l*b +bl*bw;
    }
   
int main(){
        int l,b,bw,bl;
        cin >>l>>b>>bw>>bl;
        cout<<calcHouseArea(l,b,bl,bw);
       
        return 0;
    }

 // first main function takes its memory
    then calcHouse function builds its memory above the main function memory
    when calchousearea function returns the value its memory got free

    so if there is anoter function called by calcHouseArea
then stack order is
    main>calchousearea>another
    so first another get cleared then calchouse then main

5. Recurssion
   
   
int recursiveFunctionName (data){
        termination condition
        propagation condition .. leading toward termination condition after some calls
    }


int fact(int n) {
    if(n==0) return 1;  //
termination condition
    else n*fact(n -1 ); // propagation condition leading to termination condition
}

Previous (3.cpp) <<

Next (5.cpp) >>

Comments

Popular posts from this blog