#include <iostream>
usingnamespace std;
// CalculateABS.cpp
int main()
{
// prototype
int calculateABS(int);
int num1 = -4;
int num2 = -9F;
int num3 = -423456;
cout << "The absolute value of the number " << num1;
// function call
num1 = showabs(num1);
cout << " is " << num1 << endl;
cout << "The absolute value of the number " << num2;
// function call
num2 = showabs(num2);
cout << " is " << num2 << endl;
cout << "The absolute value of the number " << num3;
// function call
num3 = showabs(num3);
cout << " is " << num3 << endl;
cin.get();
return 0;
}
//-------------------------------------------------------------------
// Output Function - code the calculateABS() function below
//-------------------------------------------------------------------
So this is what I come up with on my output function but something is missing..
1 2 3 4 5
calculateABS (int num1, int num2, int num3)
{
cout << showabs;
}
I know I for sure screwed this one up...any ideas..?
It seems that you've got a conceptual error about how function and their parameters work.
let's simplify the things a little.^{*}
- there are no global variables (if you don't know what that is, it doesn't matter, they don't exist)
- there are no functions overloads. If a function says that it receives 2 parameters, then it can only be called with 2 parameters (no 0, no 1, no 42, just 2)
- functions are identified by their name, they all know each other.
- what happens in a function, stays in a function. think of them as black-boxes, you can't see inside them.
- a function knows about its parameters, the variables that it creates, and the names of other functions. that's all its world.
- parameters are copied in the function call. so you do not receive the variable, only its value. they are not related in any way.
- a function may return a value
now take a look at your code.
in main() you are trying to call `showabs()' that takes one parameter. Such function does not exist.
apart, you are trying to create a calculateABS() function that takes 3 parameters. It's trying to use a `showabs' variable (it is a variable and not a function because there is no parenthesis). Such variable does not exist.
also, the parameters are not used
^{*}remember that it's a simplification, those rules are not valid (and may be illegal) in C or C++. The idea is for you to learn the concepts, then apply them to the implementation that you are using it.
Was that what you wanted the function to do? I wasn't sure if you were just trying to understand the fundamental of returning a value from a function, or trying to perform some other steps as well.
If you want the function to return the absolute value of the number entered, it's going to have a little more logic in it. If the number entered is positive, then the absolute value is the positive value of that number, so returning the same value is fine. But if the number entered is negative - returning that same negative value isn't correct, you need to remove the negative sign to get the absolute value.