- What is a function parameter?
A function parameter is an input value that is passed from the caller function to the called function. The parameters are assigned values before they are passed to the called functioned. To the called function, the parameters passed work as if they were defined and assigned a value within that function.
For example, the double hours, the double minutes and the double rate are the parameter from function main to function CalculatePay function.
double CalculatePay(double hours, double minutes, double rate){
double percent_minutes = minutes/60;
double pay = (hours + percent_minutes) * rate;
return pay;
}
int main()
{
double hours{38};
double minutes{30};
double rate{50.25};
double myPay = CalculatePay(hours,minutes,rate);
cout<< "My pay for the week is " << myPay << â\nâ;
return 0;
}
6. Why do functions need parameters?
Functions need parameters to perform the required task; in the example above parameters are used to calculate pay. They work as if the variables were assigned within the called function.