![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2005
Posts: 11
Rep Power: 0
![]() |
Simple Function Questions
Just a couple of simple function questions relating to a simple program exercise from a book. I'm trying to write a program to calculate BMI (Body/mass index) and am writing each calculation as a simple function.
1) Does "main()" have to be the first function listed in a program or can I define my functions then "main()" 2) Is it a good idea to create a function for each computation/formula? I know that for a simple operation like this I am making things overly complex but I want to get into the habit of creating functions, if that's proper form. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Sep 2005
Location: Požega, Croatia
Posts: 93
Rep Power: 4
![]() |
1.
main doesn't need to be first, but if you declare other functions after main, and wish to use them in main(), you need to make prototypes. example : // prototype
int function1 (int a, int b); // note the semicolon
int main()
{
int a = function1 (423, 36);
return 0;
}
// function
int function1 (int a, int b)
{
return a*b;
}2. Yes. example: What if you wish to return sum and product of parameters sent to function? you can't do this : ... return n1+n2; return n1*n2; Each function should do only one thing, so your program will be more readable, and one step closer to bug-free code. |
|
|
|
|
|
#3 |
|
Expert Programmer
|
1) It does not need to be the first function.
2)I think you should have one function, but you can do what you like. Here is an example of what you could do: int calculate(int bodymass)
{
//calculate the bodymass or whatever it does
//cout the answer
}Sorry if that didn't make sense because i dont really know what formula you would use to find the BMI. EDIT: If you tell me the forumla i would write out the a working example, but i think you might want to do it yourself because you seem keen to learn. EDIT EDIT: grrr binaryStorm beat me
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
It is a good idea to modularize your program for comprehension and maintenance purposes. It is a good idea to examine your application for objects and design them. Functions then become methods of these objects, when appropriate. Wheels and carburetors are objects; friction and air flow are variables; explosions and calculations are procedural functions that might well serve as methods.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 | |
|
Programmer
|
Quote:
__________________
of all the things he has lost, i think he misses his mind the most typedef typename pizza_t<oven_policy<225,12.5>,ingredient_policy<salami,mushroom,cheese> > Pizza; |
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Sep 2005
Posts: 11
Rep Power: 0
![]() |
So, I set up my program but when I complile I get the error "too few arguments to function" since I'm new to C++ I have no idea what this means. Can someone point me in the right direction? I'm not looking for someone to right the program for me, just help me correct what I have and explain what I did wrong.
Code is below.// Programming exercise 3.2 - figuring BMI
#include <iostream>
using namespace std;
double weightconversion(double);
double feettoinches(double);
double inchestometers(double);
double bmicalc(double);
const int feet_inches = 12;
const double inches_meters = 0.0254;
const double lbs_kilos = 2.2;
int weight_lbs = 0;
int height_feet = 0;
int height_inches = 0;
double weight_kilos = 0.0;
double height_meters = 0.0;
double heightininches = 0;
double bmi = 0.0;
int main()
{
cout << "Enter your weight: "; // User input values
cin >> weight_lbs;
cout << endl
<< "Enter your height in feet and inches: ";
cin >> height_feet >> height_inches;
weight_kilos = weightconversion(weight_lbs);
heightininches = feettoinches();
height_meters = inchestometers(heightininches);
bmi = bmicalc();
cout << endl
<< "Your BMI is: "
<< bmi
<< endl;
system("PAUSE");
return 0;
}
double weightconversion(double) // Function for conversion of weight
{
weight_kilos = weight_lbs * lbs_kilos;
return weight_kilos;
}
int feettoinches() // Function for conversion of height to inches
{
heightininches = (height_feet * feet_inches) + height_inches;
return heightininches;
}
double inchestometers(double) // Function for conversion inches to meters
{
height_meters = heightininches * 0.0254;
return height_meters;
}
double bmicalc() // Function to calculate BMI
{
bmi = weight_kilos * (height_meters * height_meters);
return bmi;
} |
|
|
|
|
|
#7 |
|
Expert Programmer
|
Too few arguments means that you created a function and when you called it you didnt use the right amount of arguments.
example: //simple function
int multiply (int a, int b)
{
int sum;
sum = a * b;
return sum;
}
//pretend this is where you want to call the function.
multiply(2,3);
//that would give you the answer 6
//but if you call it like this:
multiply(6);
//it will say: too few arguments, because you only supplied one and the function needs two.Hope i have helped.
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: Sep 2005
Posts: 11
Rep Power: 0
![]() |
So I can't do something like this:
...
double function1(double);
...
double function1()
{
...
}Basically, I delclare a function at the beginning of the program and then use it later with empty brackets, like I could "main()"? |
|
|
|
|
|
#9 |
|
Expert Programmer
|
nono.
You make you function first. double function1(){
...
}funtion1(); You only write stuff inbetween the ( and ) (when making a function) when you want an argument to the function.
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#10 |
|
Newbie
Join Date: Sep 2005
Posts: 11
Rep Power: 0
![]() |
Ok, I think I corrected it. Basically when I called the functions in "main()" I had to do this:
weight_kilos = weightconversion(weight_lbs); What I was originally trying to do was this: weight_kilos = weightconversion(); So, when I call a function in main I always have to assign some sort of value to it between "()"? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|