Besides the problems you are having with the mechanics of compiling and running programs, you need to review some basics on how to declare, define, and call functions. Maybe the following will help.
Here is an example of a very simple function:
Code:
void display(int n)
{
cout<<n<<endl;
}
All functions require a return type. In the display() function above, the return type is 'void', which stands for 'nada', 'nothing', 'zip'. That means, the function doesn't return anything. Functions also can have things called 'parameters'. The parameter in the function above is 'int n'. That means the function is expecting any integer to be sent to it. The integer value that is sent to the function will be stored in the variable 'n'.
Here is what the function looks like as part of a program:
Code:
#include <iostream> //contains cout
using namespace std;
//makes it so you don't have to write 'std::cout'
//instead you can just write 'cout'
//function:
void display(int n)
{
cout<<n<<endl;
}
int main()
{
int num = 10;
display(num);
return 0;
}
In main(), the function is called by this line:
display(num);
That instructs the program to send the value contained in the variable 'num' to the function named display(), and then execute the code in the function. Since num is equal to 10, 10 is sent to the display() function. The value 10 is then stored in the variable 'n', which is the function parameter. Then, the code in the function is executed. After the last line in the function is executed, execution returns to the place in main() where the function was called, and then the statements thereafter in main() are executed.
Here is another example of a function:
Code:
double multiplyBy3(int x)
{
double temp = x * 3.0;
return temp;
}
This time the return type is double(a double is a number with a decimal point like 2.5). The return type has to be specified as 'double' because the function returns a double value in this line:
return temp;
The multiplyBy3() function also has an int parameter, so it must be sent an int.
Adding the multiplyBy3() function to the previous program looks like this:
Code:
#include <iostream> //contains cout
using namespace std;
//makes it so you don't have to write 'std::cout'
//instead you can just write 'cout'
double multiplyBy3(int x)
{
double temp = x * 3.0;
return temp;
}
void display(int n)
{
cout<<n<<endl;
}
int main()
{
int num = 10;
display(num);
int number = 5;
double result = multiplyBy3(number);
cout<<result<<endl;
return 0;
}
The function multiplyBy3() is called in main() by this line:
double result = multiplyBy3(number);
The function is sent the int value stored in number. Since number is equal to 5, 5 is sent to the multiplyBy3 function, and then 5 is stored in the variable 'x', which is the function parameter. Then, the code in the function is executed. When the return statment is executed:
return temp;
the function gets the value in temp, and sends it back to main(). Since temp equals 15.0, 15.0 is sent back to main(). When a function returns a value, the function call is replaced by the return value. So, this line in main():
double result = multiplyBy3(number);
becomes:
double result = 15.0;
Then, the line:
cout<<result<<endl;
displays the return value. Note that you cannot do this to display 'result':
display(result);
The display() function requires an int to be sent to it, not a double, and result is type double. Therefore, you would get an error if you tried that: the types have to match.