*cough* *cough*
Sorry. I'm just suffering from a slight cold here, so sorry if my head's a little bit foggy tonight. I'm not getting nearly enough oxygen.
First, I'm assuimg blahh is just a placeholder and not what you actually put in your class. :)
Second, you need to end class declarations with a ;. I just thought I'd pick at that.
Third, you shouldn't be defining classes inside a function, but for educational purposes, let's consider that. What's the difference when you declare a variable outside a function and inside a function?
Fourth, you can give default values to data members inside a class when you make an instance of it, yes. Such a thing is done with a default constructor (<- Google term, hint hint).
Fifth, void is to be used in place of a type, stating that a function doesn't return anything with a type void example() {return 0;}//error . It can also be used to create an extremely dangerous form of pointer (void*).
Unless Greet is the name of your class, it's the same reason you can't (or at least shouldn't) define a function without a type. All functions must return something, and if they don't you need to let the compiler know. main()'s the only exception I can think of, but that's not standards compliant, hehe.
You don't know the answer to "What's the difference when you declare a variable outside a function and inside a function"? Alright, that's something you really need to know. When you declare something outside of any functions, namespaces, classes, or the like, it's declared in the global namespace; in other words it's a perfectly valid symbol in any bit of code below it.
Examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int a; //This variable is in the global namespace.
void example1();
{
int b; //This is a local variable. It only exists inside example1().
b = 0;
a = 0;
return;
}
void example2();
{
b = 0; //Error. b isn't defined here.
a = 0; //But a is!
return;
}
ah ok, i wondered why i didnt have to use void with the main function. Can you give me an example of a function type please, because i havnt come across function types before as im a beginner. I assume the types are int, char and so on, but i dont understand why a function has to have a type, seeing as its not a variable
The "type" of a function is the type of what it returns with the return statement. main() is supposed to return an int, by all rights. If a function returns an int, then you declare in advance that it returns an int int example(), and if it's an std::string*, declare that in advance instead std::string* example(). If it returns nothing, use void. As far as I know you can't declare two different return types like intchar example();, although if you're clever there's a way to do something similar. Basically, the template to use is: returntype name(args)
Ok, thanks, but i dont understand the whole point about functions returning values, is it so that if the function returns a value then we know that the function has worked correctly
Ok, thanks, but i dont understand the whole point about functions returning values, is it so that if the function returns a value then we know that the function has worked correctly
Suppose you want a function to calculate the square of a number. You pass a number as the argument and get the square as a return value:
1 2 3
double square(double n) {
return n * n;
}
There are many cases in which you'll want to pass a function some data, have it process the data and return some meaningful value for you to use. In fact, whenever a function alters program state instead of just returning a value, it is said to produce side effects.
"It is said to produce side effects"? What side effects would these be? Sometimes you want a function to simply perform a set of instructions or to change variables. You don't always want it to return a value. And you might want to use that function more than once, which means you can just call that function instead of rewriting the code within the function again.
"It is said to produce side effects"? What side effects would these be? Sometimes you want a function to simply perform a set of instructions or to change variables. You don't always want it to return a value. And you might want to use that function more than once, which means you can just call that function instead of rewriting the code within the function again.
Ok, so far i have found out that "A return from the function allows you to assign the function to a variable"...ok so when i return a value to a function it allows me to assign that value to a variable...ok
but what is the point in this, instead of putting return a; so that it can be put into a variable, why not just put char exampleVariable = a; ?
ahh, when we return the value we are returning it to the function, so the function is kinda a variable in a way, it has a value.. right? and thats it? we return values so that the function can hold a value and use it in some way?
If you have code which is repeated many times, you can put it in a function so that it is only written once and can be called whenever you need it. I don't think you understand functions yet though, they're certainly not variables. Take a look at the code example below, the numbers show the order that things happen.
1 2 3 4 5 6 7 8 9 10 11 12 13
int Multiply(int x, int y)
{
return ( x * y ); // 3 - the function calculates the value of x multiplied by y and 'returns' the result
}
int main()
{
int num; // 1 - declares a variable
num = Multiply(2,3); // 2 - the function is called - 4 - num is assigned the value returned by the function
return 0;
}
two different things happen on line 9, and the flow of the program skips into the function until it returns a value, then back to the place where the function was called
" I don't think you understand functions yet though, they're certainly not variables"
I do know what functions are, i have used them before, i said "kinda" a variable, by this i mean, when we put say return a; does that mean that the function then holds the value a which we put into a variable? and use...