Functions and parameters

I'm currently taking an online C++ class so i really don't have as much access to my instructor as i would like. I read the chapter on functions and i watched youtube and googled everything, but i still don't understand it. Could someone please explain functions to me so i can do my project. I would have to calculate the amount of calories that should be coming from a person’s intake of carbohydrates, proteins, and fats. I have no clue how to start this, HELP PLEASE!!
Last edited on
Will it be returning a value?

return statements :

-Once a value returning function computes the value the function returns the value via the return statement.
-In other words is passes this value outside this function via the return statement.This means you can use the value outside of the scope of the function. i.e. in other parts of the program.
Last edited on
What Im asking is are you planning to use a value created inside the function outside of the function. Also it sounds like you will be calling some parameters into the function did he set requirements to your variables, such as int or double? Do you have any code already written where you have attempted to figure this out on your own?

i'm not sure if im on the right track so here's what i have so far :


/**
*
*@brief calculate the amount of calories that should be coming from a person's intake of
carbohydrates, proteins, and fats
*
*/

/**
*
*@brief this program calculates calories that comes from a person's intake of carbs,proteins, and fats.
*
*
*/


#include <iostream>
#include<string>

using std::cout;



int main()
{


std::string username = "Enter Name of user: ";
cout << username;
std::cin >> username;


int calories =0;

cout << "Enter number of calories: ";
std::cin >> calories;


int low = 0;
int high = 0;


return 0;


}



std::string getName(ostream &output, iostream &input)

{


output << "Enter Name";
input >> name;



return name;


}





int getNumberCalories(ostream &output, iostream &input)
{



output <<"Enter number of calories you are planning to eat a day: ";
input >> calories;











return calories;

}



// 5000 35% => 1750
// 1237 29% => 358.73
// 2007 12% => 240.84


int calcCalorieAmount(int numofcalories, double percentofcalories){


int numofcalories = 0;

double percentofcalories =0;



double const caloriespercent = numofcalories*percentofcalories;




return caloriespercent;


}


// 600 calories, 5 calories per gram => 120 grams
// 750 calories, 10 calories per gram => 75 grams
// 1503 calories, 35 calories per gram => 43 grams

int getGrams(double calorierange, int caloriespergram )

{

double calorierange = 0;


int caloriespergram = 0;



const double caloriesbygram = calorierange/caloriespergram;




return caloriesbygram;

}


int printRange(ostream &output){

















return 0;
}
I thought of doing that as well mutexe; but I found that sometimes in the beginning students can be a little challenged in understanding what the context may mean. So I was going to try and break it down based on there response's.

You have, in your main(), a neat little collection of code to get the user's name.
(I will rewrite it some):

1
2
3
4
5
6
int main()
{
  std::string name;

  std::cout << "Please enter your name: ";
  std::getline( std::cin, name );

You may want to be able to do this over and over again. That desire is a good indication that a function may be useful:

1
2
3
4
5
6
7
std::string get_users_name()
{
  std::string s;
  std::cout << "Please enter your name: ";
  std::getline( std::cin, s );
  return s;
}

Now you can replace the code in main() with just the function:

1
2
3
4
5
int main()
{
  std::string name;

  name = get_users_name();

Hope this helps.
whats wrong with my program? It's suppose to calculate the amount of calories that should be coming from a person’s intake of carbohydrates, proteins, and fats. What am i missing?




#include <iostream>
#include<string>
#include <iomanip>

using namespace std;

string getName(ostream &output, iostream &input);
int getNumberCalories(ostream &output, iostream &input);
int calcCalorieAmount(int numofcalories, double percentofcalories);
int getGrams(double calorierange, int caloriespergram );
int printRange(ostream &output);

int main()
{


int calories =0;


int low = 0;
int high = 0;


return 0;


}


string getName(ostream &output, iostream &input)

{
string name;

output << "Enter Name";
input >> name;



return name;


}





int getNumberCalories(ostream &output, iostream &input)
{

int calories;

output <<"Enter number of calories you are planning to eat a day: ";
input >> calories;











return calories;

}



// 5000 35% => 1750
// 1237 29% => 358.73
// 2007 12% => 240.84


int calcCalorieAmount(int numofcalories, double percentofcalories){






double const caloriespercent = numofcalories*percentofcalories;




return caloriespercent;


}


// 600 calories, 5 calories per gram => 120 grams
// 750 calories, 10 calories per gram => 75 grams
// 1503 calories, 35 calories per gram => 43 grams

int getGrams(double calorierange, int caloriespergram )

{



const double caloriesbygram = calorierange/caloriespergram;




return caloriesbygram;

}



void printRange(ostream &output, string nutrient, double lowrange, double highrange, int gram){



output<<nutrient<< left<<setw(15);

output<< lowrange<< right<<setw(8);

output<<highrange<<right<<setw(8);

output<<gram<< right<<setw(8);





return ;
}
Last edited on
If you don't mind will you use either the format buttons to the right of your reply box or go to the following: http://www.cplusplus.com/articles/z13hAqkS/ , so it will be easier to read your code. Also eliminating some of the larger chunks of space also accommodates the eye. Im not trying to be a pain, it just helps separate the code from your input and the lack of excessive space makes it easier to read. On another note, huge leap forward with the function prototypes and some of the syntax.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include<string>
#include <iomanip>

using namespace std;

string getName(ostream &output, iostream &input);
int getNumberCalories(ostream &output, iostream &input);
int calcCalorieAmount(int numofcalories, double percentofcalories);
int getGrams(double calorierange, int caloriespergram );
int printRange(ostream &output);


//my function 
void printStuff(int cal, int lo, int hi);

int main()
{

/*Also if you make comments for what specific functions are suppose to do & what they are doing, it helps. */
/*
Defining the function after main is clean coding, and using the function prototype above main tells main "Hey main, when you run through the code inside you, you may encounter one or all of these functions.(Note everything takes place in the function main.) If you do encounter one of the functions listed above go to there definition below and apply what variables I (the programmer) has provided in main. (This can be user input, coder input, file input, and what ever else input is out there. )
 */

/*Right now the only thing that will happen in your program is the declaration of the 3 variables with a value of 0. If you want the functions to get called you would have them inside the function main(){ ... } */

int calories =5;    /* notice I changed the values for you to see how this works */
int low = 10;
int high = 100;
printStuff(calories, low, high);   /*Notice 3 things here no need for function type, no need for variable type, and the actual parameters are different from the formal parameters. If you want more help you need to read at a minimum:  http://www.cplusplus.com/doc/tutorial/basic_io/  */

return 0;
}
//comments     /* or comments */   

// ^- notice the two different ways to comment, 

//single line comment

/* Multi line 
comment :: The reason behind the comments on how to comment is because without comments is a guessing game to the helper.*/

string getName(ostream &output, iostream &input)

{
string name;
/* To be clear here this means that your output/input will be coming from a file or files not a user. might want to check the syntax on this */
output << "Enter Name";
input >> name;

/* For user input and output: http://www.cplusplus.com/doc/tutorial/basic_io/    <- Read me from top to bottom */

return name;    /* Note that return (name or what ever) simply means is accessible outside the scope of the function getName */
}


int getNumberCalories(ostream &output, iostream &input)//these parameters are for some one who is inputting a file.
{

int calories;

output <<"Enter number of calories you are planning to eat a day: ";
input >> calories;

return calories;

}

// 5000 35% => 1750
// 1237 29% => 358.73
// 2007 12% => 240.84


int calcCalorieAmount(int numofcalories, double percentofcalories){


double const caloriespercent = numofcalories*percentofcalories;


return caloriespercent;
}


// 600 calories, 5 calories per gram => 120 grams
// 750 calories, 10 calories per gram => 75 grams
// 1503 calories, 35 calories per gram => 43 grams

int getGrams(double calorierange, int caloriespergram )

{



const double caloriesbygram = calorierange/caloriespergram;




return caloriesbygram;

}



void printRange(ostream &output, string nutrient, double lowrange, double highrange, int gram){

output<<nutrient<< left<<setw(15);

output<< lowrange<< right<<setw(8);

output<<highrange<<right<<setw(8);

output<<gram<< right<<setw(8);


return ;
}
/*Here is an example of how to use a function. */
void /* void means it has no return, & print stuff is the function name */ printStuff(int cal, int lo, int hi){

 /*notice the formal parameters(int cal, int lo, int hi), are not the same as the actual parameters, which you can see in main. */

cout<<"Hello World"<<endl;
cout<<"The calories the user put in are: "<< cal <<endl;
cout <<"The low value the user put in is: "<<lo << endl;
cout <<"The high value entered: "<<hi <<endl;
}


So I went to a compiler and was trying to run the code I presented to you and got errors based on some of your function definitions. So I came back with nothing more than an example for you to get an idea of how it works. Note that its a void function and the input will be the variables you provided, but the values are different.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

using namespace std;

//my function 
void printStuff(int cal, int lo, int hi);

int main()
{


int calories =5;    /* notice I changed the values for you to see how this works */
int low = 10;
int high = 100;

printStuff(calories, low, high);  

//Notice 3 things here no need for function type, no need for variable type, and
//the actual parameters are different from the formal parameters. If you want //more help you need to read at a minimum:  //http://www.cplusplus.com/doc/tutorial/basic_io/  

return 0;
}


/*Here is an example of how to use a function. */
/* void means it has no return, & print stuff is the function name */
void  printStuff(int cal, int lo, int hi){ 

//notice the formal parameters(int cal, int lo, int hi), are not the same as the //actual parameters, which you can see in main. 

cout<<"Hello World"<<endl;
cout<<"The calories the user put in are: "<< cal <<endl;
cout <<"The low value the user put in is: "<<lo << endl;
cout <<"The high value entered: "<<hi <<endl;
}
Last edited on
I hope the lack of excessive space's and the comments help you see how some of the syntax works. Also very important that you do so more in-depth reading on this material. You used <fstream> variables with out the header & the user will only see the output and input if they are reading it from a file, and honestly I think some of that syntax was wrong. You seem very new to this, a recommendation if you don't mind. Ask specific questions. Know more about the basic's, " input/output, variables( & how they work), and play around with it all.
The more specific your question the more anyone can help you. NO ONE will do your work for you but a plethora of people will be willing to help.Also practice on your input and output and it will help with every program that you right from here on out. If you want to use files to input data, which is very common, read more on file streams and come back with questions. If you skip anything basic in programming it makes it either impossible or extremely difficult to learn the more complex stuff.

whats wrong with my program? It's suppose to calculate the amount of calories that should be coming from a person’s intake of carbohydrates, proteins, and fats. What am i missing?

The simple answer to your question is that you're not actually calling any of the functions that you've defined. Your main function does nothing but initialize some variables to zero.
I fixed some of it but when i tried to call my getName and getNumberCalories functions it gave me this error invalid user-defined conversion from std::istream{aka std::basic_istream<char>} what does that mean?


#include <iostream>
#include<string>
#include <iomanip>

using namespace std;

string getName( iostream *input, ostream *output);
int getNumberCalories(ostream &output, iostream &input);
int calcCalorieAmount(int numofcalories, double percentofcalories);
int getGrams(double calorierange, int caloriespergram );
void printRange(ostream &output, string nutrient, double lowrange, double highrange, int gram);



/**
*@brief Prompts the user to enter name and reads name
*
*@param &output displays the message on the print screen
*
*@param &input allows the user to input on the print screen
*
*@return the user name
*/

string getName(iostream *input, ostream *output)

{
string name;


*output << "Enter Name";

*input >> name;


return name;


}
/**
*@brief prompts user to enter the number of calories and reads the number of calories
*
*@param &output displays the message on the print screen
*
*@param &input allows the user to input on the print screen
*
*@return the number of calories
*/

int getNumberCalories(iostream *input, ostream *output)
{

int calories;

*output <<"Enter number of calories you are planning to eat a day: ";
*input >> calories;



return calories;

}



// 5000 35% => 1750
// 1237 29% => 358.73
// 2007 12% => 240.84


int calcCalorieAmount(int numofcalories, double percentofcalories)
{


double const caloriespercent = numofcalories*percentofcalories;



return caloriespercent;


}


// 600 calories, 5 calories per gram => 120 grams
// 750 calories, 10 calories per gram => 75 grams
// 1503 calories, 35 calories per gram => 43 grams

int getGrams(double calorierange, int caloriespergram )

{



const double caloriesbygram = calorierange/caloriespergram;


return caloriesbygram;

}



void printRange(ostream &output, string nutrient, double lowrange, double highrange, int gram)
{


output<<nutrient<< left<<setw(15);

output<< lowrange<< right<<setw(8);

output<<highrange<<right<<setw(8);

output<<gram<< right<<setw(8);


return ;
}




int main() //header function
{


int lowrange = 0;
int highrange = 0;
int caloriespergram = 0;


string name = getName(cin , cout );

int calories = getNumberCalories(cin, cout);


return 0;

}
You've already been asked once - can you please use code tags to make your code more readable? Aim4Erudite already posted a link to explain how.

You've defined your functions to take a pointer to a stream, but you're not passing pointers in - you're passing the streams themselves.
Topic archived. No new replies allowed.