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 not recommended in inline function
// static int e=0; //this will excute only once
// e = e+1; //next time this function is run, the value of e is retained
// return c+e;
return c;
}
/*********constant arguments******* */
// int strlen(const char *p){
// In C++, a constant argument is a function argument that cannot be changed by the
// function. The const qualifier in the function prototype tells the compiler that the
// argument is constant, and any attempt to change it will result in a compile-time error.
// Constant arguments are useful when functions are called by reference, and they can be
// used in any function that requires a constant argument.
// }
int moneyReceived (int currentMoney, float interestRate = 1.04)
//here interestRate is the default argument, jisko agr value na dein to vo
//by deafault vahi value leleta and agr dedete to value update hojati
//default arguments ko baad m likhna hai and compulsory argument ko pehle
{
return currentMoney*interestRate;
}
int main(){
int a,b;
int money = 100000;
cout<<"enter the value of a "<<endl;
cin>>a;
cout<<"enter the value of b "<<endl;
cin>>b;
cout<<"the product of a and b is "<<product(a,b)<<endl;
// cout<<"the product of a and b is "<<product(a,b)<<endl;
// cout<<"the product of a and b is "<<product(a,b)<<endl;
// cout<<"the product of a and b is "<<product(a,b)<<endl;
// cout<<"the product of a and b is "<<product(a,b)<<endl;
// cout<<"the product of a and b is "<<product(a,b)<<endl;
// cout<<"the product of a and b is "<<product(a,b)<<endl;
// cout<<"the product of a and b is "<<product(a,b)<<endl;
// cout<<"the product of a and b is "<<product(a,b)<<endl;
// cout<<"the product of a and b is "<<product(a,b)<<endl;
// cout<<"the product of a and b is "<<product(a,b)<<endl;
// cout<<"the product of a and b is "<<product(a,b)<<endl;
// cout<<"the product of a and b is "<<product(a,b)<<endl;
// cout<<"the product of a and b is "<<product(a,b)<<endl;
cout<<"if you have "<<money<<" in your bank account, you will receive "<<moneyReceived(money)<<endl;
cout<<"FOR VIP: if you have "<<money<<" in your bank account, you will receive "<<moneyReceived(money, 1.10)<<endl;
return 0;
}
Comments
Post a Comment