Overloads two absolute value functions. - C++ Function

C++ examples for Function:Function Overload

Description

Overloads two absolute value functions.

Demo Code

#include <iostream>
using namespace std;
#include <iomanip>  
int abs1(int i)
{
  if (i < 0) {
    return (i * -1);
  }/*from   w w w.  j a  v a 2 s.co  m*/
  else {
    return (i);
  }
}
float abs1(float x)
{
  if (x < 0.0)
  {
    return (x * -1.0);
  }
  else
  {
    return (x);
  }
}
int main()
{
  int ians;
  float fans;
  int i = -15;
  float x = -64.53F;
  ians = abs1(i);
  cout << "Integer absolute value of -15 is " << ians << "\n";
  fans = abs1(x);
  cout << "Float absolute value of -64.53 is " << setprecision(2) << fans << "\n";
  return 0;
}

Result


Related Tutorials