Posts

Showing posts from August, 2024

recursions and recursive functins in c++

  //code for factorial #include <iostream> using namespace std ; int factorial ( int y ) {     if (y <= 1 ){         return 1 ;   //ye nhi btaenge to infinitely chalta rahega     }     return y * factorial (y - 1 ); } int fib ( int y ) {     if (y <= 2 ){         return 1 ;     }     return fib (y - 1 ) + fib (y - 2 ); } // fibonacci sequence int main (){     int x;     // cout<<"enter the number whose factorial u wanna find ";     // cin>>x;     // cout<<"the factorial of "<<x<<" is "<<factorial(x)<<endl;     cout << "enter the term u wanna find out in the fibonacci sequence" << endl;     cin >> x;     cout << "the term in fibonacci sequence at position " << x << " is " << fib (x);     retur...

inline function, static variables, default arguments, constant arguments

  #include <iostream> using namespace std ; // int product(int a, int b) // { //     int c = a*b; //     return c; // } /***********INLINE FUNCTION********* */ /*C++ provides inline functions to reduce the function call overhead. An inline function is a function that is expanded in line when it is called.  When the inline function is called whole code of the inline function gets inserted   or substituted at the point of the inline function call. This substitution is   performed by the C++ compiler at compile time. An inline function may increase   efficiency if it is small.*/   //bade functions m inline function use nhi krna as vo pura replace hojaega   //call ki jagah so zyada memory khaega   //incline function ek request hoti hai to the compiler, compiler decide krta hai vo requst accept krni ya nahi inline int product ( int a , int b ) {     int c = a * b;     //use of static variables is n...

call by value and call by reference in c++

  #include <iostream> using namespace std ; // int sum(int a, int b){ //     int c; //     c = a + b; //     return c; // } void swap ( int a , int b ){  /*temp  a  b*/     int temp = a;          /* 5    5  7*/     a = b;                 /* 5    7  7*/    //ye ek value exchange krne ka tarika hai     b = temp;                /* 5    7  5*/     } /*yaha x aur y (actual arguments) ki value copy hui a aur b m but and vo exchange hue jisse x and y kuch frk nhi pdega*/ /********CALL BY REFERENCE USING POINTERS******* */ void swapPointer ( int* a , int* b ){ //int* mtlb address liya hai     int temp = * a; //*a mtlb dereference kiya hai (value at address a)     * a = * b;     * b = temp; }...