Cpp - Using standard function

Introduction

Standard function prototypes do not need to be declared, as they have already been declared in standard header files.

If the header file is included in the program's source code by #include directive, the function can be used immediately.

For example,

#include <cmath> 

Following this directive, the mathematical standard functions, such as sin(), cos(), and pow(), are available.

The following code shows how to do calculating powers with the standard function pow()

Demo

#include <iostream>    // Declaration of cout 
#include <cmath>       // Prototype of pow(), thus: 
                            // double pow( double, double); 
using namespace std; 

int main() /*ww  w .j  a va  2s.  c o  m*/
{ 
  double x = 2.5, y; 
  // Computes x raised to the power 3: 
  y = pow(x, 3.0);     // ok! 
  y = pow(x, 3);       // ok! The compiler converts the 
                       // int value 3 to double. 

  cout << "2.5 raised to 3 yields:       " 
        << y << endl; 

  // Calculating with pow() is possible: 
  cout << "2 + (5 raised to the power 2.5) yields: " 
        <<  2.0 + pow(5.0, x) << endl; 

  return 0; 
}

Result

Related Topic