Output function issue..?

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
32
33
34
35
#include <iostream>
using namespace 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.
Last edited on
1
2
3
4
5
calculateABS (int x)
{
 return x;
}


is this a little better..? Or am I still wayy off..? lol
Can anyone tell me if I have down the right output function please..?
1
2
3
4
calculateABS (int x)
{
 return x;
}


This returns the exact value that was entered into the function.
So is this a correct Output Function..?
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.
Well I'm trying to find the output function of that program so, If your saying I need to
1
2
3
4
5
6
calculateABS (int x)
{
cout << x << " " << x * -x << endl; 
return x;
}


Ok...Is this better now...? I just wish I knew when to use what and when everytime..??
Added in the return type for the function which I missed before :)

1
2
3
4
5
int calculateABS (int x)
{
cout << x << " " << x * -x << endl; 
return x;
}


what this is doing ... let's say x is -5.
this function outputs the following

-5 -25


then returns -5 to the calling function.

Last edited on
soo basically that wont work is what your saying..??
Not if you want to have this function return the absolute value of the number entered back to the statement where you called it in main.

Absolute value would be along these lines.

1
2
3
4
5
6
7
int calculateABS (int x)
{
//if x is non-negative
//  return x
//else (x is negative)
//  return the opposite of x
}

Last edited on
1
2
3
4
5
6
7
8
int calculateABS (int x)
{
if (x > 0)
return x;
else (x < 0)
return -x;
}



Well thank you for helping me out with this...It's been hell for me honestly, so is this what you mean..?? hahaha
I'd include 0 in that first check and you don't need the (x<0) next to else, but basically you've got it.

1
2
3
4
5
6
7
int calculateABS (int x)
{
if (x >= 0)
return x;
else
return -x;
}
Oh ok that makes it soo much simpler...(: How do you know how to do this shart..??
Practice practice practice.... Oh, and don't forget some practice.
Topic archived. No new replies allowed.