Hey everyone, im back again with another question.I know this topic has been brought up before, and i have searched and read all the topics but still a bit confused. Majority of the topics seems to be archived and wont let me reply either, besides didnt want to seem as if i was hijacking a thread to ask my questions..So thats why i created a new topic.
Anyhow, onto the problem. You all know I am a noob and taking c++ classes, so far we have only met twice and this week being a holiday, i cant get a hold of my instructor for help. Basically one of our future assignments asks to build a program to convert minutes to hours and minutes. From all the information i have gathered, I am able to achieve the solution by using just 1 function :
compiled and tested.. it works.
problem now is, instructor wants us to have a separate function, int convert_from_minutes() well, being that i am a noob and working on a assignment that is 4 weeks in the future, I am having problems.
I am not too sure what the "return" expression should be, I kind of understand why the "return" is set there but not exactly sure its purpose when having more than 1 function. Basically, how would i utilize the second function so that i can use its value in the "main" function? Of course there is more the assignment as in using conditional operators, but i think i can figure that out myself. As of right now I am having a hard time passing values from second function to main function?
Many thanks in advance and apologize for a repost on a repetitive topic.
You should look at the tutorial here first. It has a pretty good overview of how functions work. If you still have questions after reading that, post again. http://cplusplus.com/doc/tutorial/functions/
First of all, you should put indentation in your code. It is a good practice and makes the code readable. The rule is simple. Every time you put a '{', go one 'tab' more towards right. And on every '}' come back one 'tab' left. You can identify very easily which '{' corresponds to which '}' and the code-block in between.
Secondly, I think you have to learn about local and global variables. Any variable declared at the top of the code is global. They are declared outside the function and accessible from all functions. In your code 'userinput' should be such a variable as you want to access it both from 'main' and 'convert_from_minutes'. For beginners, it is tempting to create global variables. But they are really really messy. You cannot keep track which function is doing what operation on them if the code is long and complicated.
The solution is local variable. These variables are created locally and is accessible only from the function where they are created. In your code, the way 'userinput' is created in 'main' makes it local to 'main'.
Thus 'convert_to_minute' function should have its own copy of 'userinput'. This is done using function arguments. This is analogous to mathematical function f(x) where x is called the argument. So you define the function as
1 2 3 4 5 6 7 8 9 10 11
int convert_from_minutes (int userinput)
{
/***********************************************************
* userinput is local to this function.
* It would contain a copy of userinput from main.
* Changing its value will not affect the original variable.
************************************************************/
int minutes = userinput % 60;
return minute;
}
This means 'convert_from_minutes' expects one argument of type 'int' and returns an answer of type 'int' as well. You can call this function from 'main' in the following way
1 2 3 4 5 6 7 8 9 10 11 12 13
int main (void)
{
int userinput, minute;
/**** code to fill userinput ****/
minute = convert_from_minute (userinput);
// passing a copy of userinput and storing the returned value in minute.
/**** The rest of the code ****/
return 0;
}
If you have to return multiple answers, then you can return a 'structure' (possible only in C++, not in C). Hope this information is sufficient for you to figure out the rest. Enjoy coding :)
thank you!! you are right.. i forgot the (s). and yes it does work now. thank you all so kindly. After reading more about function prototype i finally understood some of the stuff going on.. thanks again. now that i got it running, going to mess around with it and satisfy my curiosity.. thanks again everyone!!
Personally, I would make my convert_from_minutes function do ALL the work, not just half. That way, all the calculation is done by the function. The way you have done it works just fine, I am simply offering another solution to the problem. I like to figure out different ways to do the same thing.
You could make your function like this:
void convert_from_minutes(int*, int*);
Of course, if your instructor requires the function to return an 'int', then this will not fit that requirement and you are probably at the best solution.
One other method that would fit the 'required to return an int' would be this:
int convert_from_minutes(int*);
In this variation you would pass the 'minutes' variable's address to the function. The function would return a new int holding the number of hours and the 'minutes' variable would be modified to now hold the remainder of minutes. Then in your main function you could use it as follows: