Reputation: 1
My C++ class starts in fall and I was trying to do a little bit of learning before then so that I can have a leg up when I get to school.
Anyways I'm at a point where I'm learning user defined variables and I don't really understand what the difference is between that and just declaring a variable. I know there's something I'm missing here and I came hoping someone can clear this up for me. Just to be more specific, whats the difference between this:
#include<iostream>
#include <cmath>
void myfun(int);
int main()
{
using namespace std;
myfun(45);
system("pause");
return 0;
}
void myfun(int x)
{
using namespace std;
cout << "My favorite number is " << x << endl;
}
And this:
#include<iostream>
#include <cmath>
int main()
{
using namespace std;
int x = 45;
cout << "My favorite number is " << x << endl;
system("pause");
return 0;
}
I don't see a difference and the former looks like more of a process.
Upvotes: 0
Views: 1039
Reputation: 319
I recommend declaring namespace std at the start of your source file, after the needed headers. What your doing is declaring it in every different function and the std namespace functions will only be relevant in local scope.
A functions purpose it to perform a specific task in mind. eg a function called square() might just return a value times by itself. A variable is just a 'holder' if you will for a specific datatype.
Generally speaking functions should have only 1 main task in mind. Functions are useful so your main source file isn't crammed up with messy source code.
What your asking is whats the difference between a function and a variable. Im assuming your confused with the variable type in front of the function parameters.
int a(int value)
{
return ( value + 1 );
}
and
int a;
The variable type before the function name signifies what data type it will be returning. Eg in the previous code int a() would return a integer value. where as int a would just hold a value.
e.g.
int x = 5;
cout << a(x);
would return and print 6.
Upvotes: 0
Reputation: 35
In the second image, you are writing code within the main method of your program (ie. the part that runs when you compile/run your program), whereas, in the first image, you are writing the same code inside of a function/method (they're the same thing). This may not be the best example because your function only contains a couple lines of code, but the use of functions becomes infinitely more useful when you need to perform more complex operations. For example, if void myFun(int x) { ... } was supposed to be multiplied by 2 and then increased by 5 before being printed, it would look something like this:
void myfun(int x)
{
using namespace std;
x = x * 2;
x += 5;
cout << "My favorite number is " << x << endl;
}
So, imagine you want to do this for 3 different numbers: 45, 46, and 47... If you write the code out in your main method, you would have to write all of it out 3 times (or use a loop), but, either way, your main method starts go get pretty messy:
int main()
{
using namespace std;
int x = 45;
x = x * 2;
x += 5;
cout << "My favorite number is " << x << endl;
int x = 46;
x = x * 2;
x += 5;
cout << "My favorite number is " << x << endl;
int x = 47;
x = x * 2;
x += 5;
cout << "My favorite number is " << x << endl;
system("pause");
return 0;
}
By using a function, you can keep your code clean and save yourself from writing the same code over and over:
int main()
{
using namespace std;
//these three lines will do the same thing as the code above
myFun(45);
myFun(46);
myFun(47);
system("pause");
return 0;
}
void myfun(int x)
{
using namespace std;
x = x * 2;
x += 5;
cout << "My favorite number is " << x << endl;
}
All in all, both of your examples accomplish the same thing, but one will scale (stay clean and efficient as you increase the complexity of your code) much better than the other. If you find yourself writing the same bit of code more than a couple times, then you would want to consider writing a function for it and then calling that function whenever you need to run that bit of code.
Upvotes: 1
Reputation: 111
Functions are used to make the program more clean and readable, moreover ,using classes leads to functions , so that you can't reach classes without functions as in object oriented languages , C++ for example.
In addition, functions helps you using the 'recursive' solution for some problems which used in divide and conquer algorithms (when the function calls itself multiple of times).
As a programmer your program have to be tidy and readable.
Upvotes: 0
Reputation: 1172
Think about how you can output this:
My favorite number is 13
My favorite number is 23
My favorite number is 11
My favorite number is 25
My favorite number is 77
With a function, it is much easier, but very clumsy if you only use variables.
Upvotes: 5