Credits

Saturday, February 21, 2009

Another Sample on C++ Functions

Actual and formal parameters hold similar values. Use the command return should you need to return a value to the calling program. Remember that the data type should be similar to the returned value otherwise you will be seeing errors when you run this program.

Below is another C++ function with parameter passing.
[+/-] show/hide


#include iomanip //enclose this in <>
#include iostream //enclose this in <>

using namespace std;
void read();
float payment(float totalcharges);
float charges();
float compute(float charges);
main()
{
float tcharge;
read(); //calling function read
tcharge=charges(); //passing the returned value
payment(tcharge); //actual parameter passing
cin.get();
cin.ignore();
}

void read()
{
char name[30];
char course[15];
cout<<"input student's name:";
cin.getline(name,30);
cout<<"input student's course:";
cin>>course;
}

float charges()
{
float tuition, units, misc;
cout<<"input tuition rate per unit: ";
cin>>tuition;
cout<<"input total units enrolled: ";
cin>>units;
cout<<"input total miscellaneous: ";
cin>>misc;
return tuition*units+misc;}

float payment(float totalcharges) //formal parameters
{
float cash;
cout<<<">
cout<<<"input>
cin>>cash;
cout<<<"your>
}

AddThis