Simple exercise, lots of errors?
Jan 29, 2012 at 11:12am UTC
Functions .... very simple...
suppose i had to write a program for a math problem and it took 1000 lines of code..... but i must also do the math problem 3 times.....
Now
1 2 3 4 5 6 7 8
void main(){
1000 lines
+
1000 lines
+
1000 lines
}
3000 lines of crap , endless.
i can just make a function
1 2 3
void myMathSolution(){
1000 lines
}
then call it 5 times....
1 2 3 4 5 6 7 8 9 10 11 12
void main(){
myMathSolution();
myMathSolution();
myMathSolution();
myMathSolution();
myMathSolution();
// hell i can loop it 1000 more times
for (int x = 1000; x>0;x --;)
myMathSolution();
)
look how small my code is and i solved the math problem a 1000 times...
plus i can copy paste the function in another program and run it again for another program in the future...
that is all a function really is for...
Last edited on Jan 29, 2012 at 11:15am UTC
Jan 29, 2012 at 2:35pm UTC
If you have over a hundred lines for any function, you're doing something very wrong or doing something that other functions could do. ;)
Jan 29, 2012 at 2:46pm UTC
Hey iHateBooks. I modified your program to make it work correctly. That function
void result (void )
as to be a
double
. Here you go:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <iostream>
using namespace std;
double result(double );
int main(void )
{
double celcius;
cout << "Please input a celcius value: " ;
cin >> celcius;
cout << celcius << " celcius is equal to " << result(celcius) << " fahrenheit!\n" ;
system("PAUSE" );
return 0;
}
double result(double temp)
{
double result;
result = 1.8 * temp + 32;
return result;
}
Last edited on Jan 29, 2012 at 2:47pm UTC
Jan 29, 2012 at 4:54pm UTC
If you have over a hundred lines for any function, you're doing something very wrong or doing something that other functions could do. ;)
It took a thousand lines of code just for me to add two numbers once.... wonder what i did wrong :)
Topic archived. No new replies allowed.