Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 10th, 2005, 1:03 PM   #1
meverha1
Newbie
 
Join Date: Sep 2005
Posts: 11
Rep Power: 0 meverha1 is on a distinguished road
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.
meverha1 is offline   Reply With Quote
Old Sep 10th, 2005, 1:18 PM   #2
BinaryStorm
Programmer
 
BinaryStorm's Avatar
 
Join Date: Sep 2005
Location: Požega, Croatia
Posts: 93
Rep Power: 4 BinaryStorm is on a distinguished road
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.
BinaryStorm is offline   Reply With Quote
Old Sep 10th, 2005, 1:19 PM   #3
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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 
}
Then in the main function you can cin the bodymass or whatever you need and then it calls that function.

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.
coldDeath is offline   Reply With Quote
Old Sep 10th, 2005, 1:22 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Sep 10th, 2005, 5:17 PM   #5
Shapeless
Programmer
 
Shapeless's Avatar
 
Join Date: Jul 2005
Location: germany/hamburg
Posts: 32
Rep Power: 0 Shapeless is on a distinguished road
Send a message via ICQ to Shapeless Send a message via MSN to Shapeless
Quote:
Originally Posted by coldDeath
1)
int calculate(int bodymass)
{
//calculate the bodymass or whatever it does
//cout the answer 
}
Well, bad idea. One function has only one job todo.your function "calculate" calculates and prints the output. And so return and do not show "the answer".
__________________
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;
Shapeless is offline   Reply With Quote
Old Sep 11th, 2005, 10:38 AM   #6
meverha1
Newbie
 
Join Date: Sep 2005
Posts: 11
Rep Power: 0 meverha1 is on a distinguished road
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;
}
meverha1 is offline   Reply With Quote
Old Sep 11th, 2005, 10:44 AM   #7
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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.
coldDeath is offline   Reply With Quote
Old Sep 11th, 2005, 10:53 AM   #8
meverha1
Newbie
 
Join Date: Sep 2005
Posts: 11
Rep Power: 0 meverha1 is on a distinguished road
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()"?
meverha1 is offline   Reply With Quote
Old Sep 11th, 2005, 10:59 AM   #9
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
nono.
You make you function first.
double function1(){
...
}
Then in the main() function you call it like this:
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.
coldDeath is offline   Reply With Quote
Old Sep 11th, 2005, 11:04 AM   #10
meverha1
Newbie
 
Join Date: Sep 2005
Posts: 11
Rep Power: 0 meverha1 is on a distinguished road
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 "()"?
meverha1 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:50 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC