Ok well let's start with the basics. I'm gonna refer to my snippet I just posted.
Function signatures goes as follows
type name(parameters if any) |
Type can be any data type, primitive or types that you make. It basically is just the data type that the function will return back to the caller. We'll assume that you call everything from main() for now. Since I made my function up there as bool type, that function is expecting a
If you don't supply the proper return type, it probably won't even compile. You can also have functions as void, which you have up there. Void expects no return type, which also means you can't have a return statement in that function.
Now, let's put all this into context. You are trying to test rather 4 parameters are all different. You take this input in main and store into some variables. You will then pass these variables into this new function we made like so
Like I said in the above code, this will
pass by value, the values of the variables you sent, and store these values into the variables defined in your function parameters. So now, you have 4 new variables with the same values that the user just gave you. Using these values you can now test them all against each other, and if there are any similarities you can return false, because they are not all different. If they ARE all different, you return true.
Now what return does is returns a value back to the caller. You are already familiar with this, just don't know it. You know how at the end of main you have
return 0;
?
Well guess what, that is returning the integer of 0 back to the caller, which is your OS. When you start a program, your OS invokes (calls) the main() function. See? You've been doing this all along, just weren't aware of it.
So let's try this
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
|
#include <iostream>
//This tells the compiler that there is a function somewhere in here that will be implemented with this name
bool testReturns(int a, int b);
int main()
{
int x;
int y;
std::cout << "Enter two numbers, and we'll send it to that function.\n";
std::cin >> x >> y;
std::cout << "Ok cool, now we'll compare them and return true (1) if they are different, or false (0), if they are the same\n";
//Now here is going to be our function call.
//When you call a function, you only need to supply the name and proper arguments
//What this will do is call this function, the function will do it's work, and
//then it will send back a 1 or 0, for true or false respectively.
std::cout << testReturns(x,y);
return 0;
}
//When you implement your function, you need to make sure the signature of it matches what you put earlier
bool testReturns(int a, int b)
{
if(a==b)
return true;
else
return false;
}
|
Now you should run this and watch what happens when you put in some various integer values.