Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you call a function?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
How do you call a function? Can you give me a simple example? Thankz
 
Code:
#include <iostream.h>

float half(float what)
{
    return what/2;
}

int main(void)
{
    cout << half(4.2) << endl;
    return 0;
}
----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Here is one of my programs as example...hope this helps...
Do you have a specific project?


#include <iostream.h>
#include <math.h>
#define TRUE 1
#define FALSE 0

int Process_Function(int type_choice, int main_choice);
void datatype(Process_Function);
int main_menu(Process_Function);
int add_function(int type_choice, int main_choice);
float subtract_function(int type_choice, int main_choice);
double multiply_function(int type_choice,int main_coice);
long divide_function(int type_choice, int main_choice);






void main()
{

Process_Function();

}

void Process_Function(int type_choice, int main_choice)
{

do
{

int main_choice;
main_choice= main_menu();
if (main_coice=5);
break;
int type_choice=datatype();
switch(main_choice)
{
case 1:
int add_fuction(int type_choice,int main_choice);

break;
case 2:
float subtract_function(int type_choice,int main_choice);

break;
case 3:
double multiply_function(int type_choice,int main_choice);

break;
case 4:
long divide_function(int type_choice,int main_choice);

break;
default:

cout<<&quot;YOU SHOULD HAVE ENTERED A NUMBER FROM 1 TO 4. PLEASE TRY AGAIN.&quot;;
}
}
while(TRUE);
return 0;
}

int main_menu(Process_Function)
{


int main_choice=1;
double main_choice;
long main_choice;
float main_choice;
while (main_choice != 5 )
{
main_menu();
cin >> main_choice;

if (main_choice == 1 )
int add_function();
else
if (main_choice == 2 )
float subtract_function;
else
if (main_choice == 3 )
double multiply_function();
else
if (main_choice == 4 )
long divide_function();
else
if (main_choice == 5 )
break;
}


cout << endl << &quot;This program was written by: Wendy Matthews&quot; << endl;
cout << endl << endl;
cout << endl << &quot;1 - add&quot;;
cout << endl << &quot;2 - subtract&quot;;
cout << endl << &quot;3 - multiply&quot;;
cout << endl << &quot;4 - divide&quot;;
cout << endl << &quot;5 - Exit&quot;;
cout << endl << endl << &quot;Make a selection &quot;;


return main_choice;
}

int datatype(Process_Function)


{

int type_choice=1;
double type_choice;
float type_choice;
long type_choice;

while (type_choice != 5 )
{
datatype();

cin >> type_choice;

if (type_choice == 1 )
int type_choice;
else
if (type_choice == 2 )
float type_choice;
else
if (type_choice == 3 )
double type_choice;
else
if (type_choice == 4 )
long type_choice();
else
if (type_choice == 5 )
break;
}


cout << endl << &quot;This program was written by: Wendy Matthews&quot; << endl;
cout << endl << endl;
cout << endl << &quot;1 - integer&quot;;
cout << endl << &quot;2 - float&quot;;
cout << endl << &quot;3 - double&quot;;
cout << endl << &quot;4 - long&quot;;
cout << endl << &quot;5 - Exit&quot;;
cout << endl << endl << &quot;Make a selection &quot;;


return type_choice;
}



int add_function(int type_choice,int main_choice)
{
int num1, num2, answer;
cout<<&quot;Enter Number: &quot;<<endl<<num1;
cin>>num1;
cout<<&quot;Enter Another Number: &quot;<<endl<<num2;
cin>>num2;
answer=num1+num2;
return answer;
}

float subtract_function(int type_choice,int main_choice)
{
float num1, num2, answer;
cout<<&quot;Enter Number: &quot;<<endl<<num1;
cin>>num1;
cout<<&quot;Enter Another Number: &quot;<<endl<<num2;
cin>>num2;
answer=num1-num2;
return answer;
}

double multiply_function(int type_choice,int main_choice)
{
double num1, num2, answer;
cout<<&quot;Enter Number: &quot;<<endl<<num1;
cin>>num1;
cout<<&quot;Enter Number: &quot;<<endl<<num2;
cin>>num2;
answer=num1*num2;
return answer;
}

long divide_function(int type_coice,int main_choice)
{
long num1, num2, answer;
do
{
cout<<&quot;Enter Number: &quot;<<endl<<num1;
cin>>num1;
cout<<&quot;Enter Another Number: &quot;<<endl<<num2;
cin>>num2;
answer=num1/num2;
}
while (num1 > 1 && num2 > 0);
return answer;
}





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top