Things in parenthesis aren't functions.
Code:
int main()
{
int x = 3;
if(x == 5)
cout << "x is equal to 5";
else
cout << "x does not equal 5";
}
(x==5) is a conditional statement.. If this is true then run the next line of code. Otherwise x does not equal 5 so run the statement after the else statement.
Code:
bool isEqual(int variable)
{
if(variable == 5)
return true;
else
return false;
}
Code:
int main()
{
int x = 3;
bool result;
result = isEqual(x);
if(result == true)
cout << "x is equal to 5";
else
cout << "x does not equal 5";
}
The first thing my function will do is return true or false. bool.
The function is called isEqual you can call it whatever you want. The function takes in an int paremeter and renames it variable.
So when my main program calls isEqual(x) The function takes x and represents it as variable. Function performs on this parameter passed in then returns the result.
Function is code independent and reusable.
You can call isEqual as many times in your main program without having to rename it.
ex:
Code:
int main()
{
int x = 3;
int y = 5;
bool result;
result = isEqual(x); // Pas in x this time x = 3;
if(result == true)
cout << "x is equal to 5";
else
cout << "x does not equal 5";
result = isEqual(y); // Pass in y this time. y = 5
if(result == true)
cout << "y is equal to 5";
else
cout << "y does not equal 5";
}
Functions are not equations.. They just perform a task. The can be used to display output, gather input, draw a character to a screen, make a character move, etc..
Code:
int main()
{
x=x+5;
y=5+x;
if (y > 10)
}
The above has nothing to do with functions and no functions need to be defined.