Function part 3 (built in functions)

Estimated reading: 3 minutes 97 Views

Built-in functions in C++ are functions that are provided by the compiler and are available to use in any C++ program. These functions are typically used for common tasks such as input/output, mathematical operations, and string manipulation.

Here are some of the most common built-in functions in C++:

  • Mathematical functions:
    • abs(): Returns the absolute value of a number.
    • sqrt(): Calculates the square root of a number.
    • pow(): Raises a number to a power.
    • sin(): Calculates the sine of an angle in radians.
    • cos(): Calculates the cosine of an angle in radians.
    • tan(): Calculates the tangent of an angle in radians.
    • floor(): Returns the greatest integer less than or equal to a number.
    • ceil(): Returns the smallest integer greater than or equal to a number.
  • String manipulation functions:
    • strlen(): Returns the length of a string.
    • strcpy(): Copies one string to another string.
    • strcat(): Concatenates two strings.
    • strcmp(): Compares two strings and returns an integer value indicating whether they are equal, less than, or greater than each other.
  • Input/output functions:
    • cin: Reads data from the standard input stream.
    • cout: Writes data to the standard output stream.
    • cerr: Writes data to the standard error stream.
    • fopen(): Opens a file for reading or writing.
    • fclose(): Closes a file.

Example 1:

cout << sqrt(18) << endl;

Output:

4.24264

Example 2:

cout << abs(-18) << endl;

Output:

18

Example 3:

cout << mod(10.5,2) << endl;

Output:

0.5

Example 4:

cout << floor(10.2) << endl;

Output:

10

Example 5:

cout << floor(-10.2) << endl;

Output:

-11

Example 6:

cout << ceil(10.2) << endl;

Output:

11

Example 7:

#include <iostream>

using namespace std;

int main()
{
    cout << sqrt(abs(pow(-3,2))) << endl;

    return 0;
}

To use a built-in function, you simply need to include the appropriate header file. So you might have a problem executing this program. you would need to include the <cmath> header file.

The <cmath> header file contains declarations for a number of mathematical functions, such as sqrt(), pow(), sin(), cos(), and tan(). These functions are useful for performing common mathematical operations, such as calculating the square root of a number, raising a number to a power

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    cout << sqrt(abs(pow(-3,2))) << endl;

    return 0;
}

Output:

3

Example 8:

#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

int main()
{
    cout << max(10,5) << endl;

    return 0;
}

You would need to include the <algorithm> header file to use any of the algorithms provided by the C++ standard library. These algorithms include sorting, searching, merging, and many more.

Output:

10

Example 9:

#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

int main()
{
    cout << max(15,max(10,5)) << endl;

    return 0;
}

Output:

15

Example 10:

#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

int main()
{
    
    int x = 0, y = 10;
    swap(x,y);
    cout << "x= " << x << "y= " << y << endl;

    return 0;
}

Output:

x= 10y= 0

Built-in functions can be a very useful tool for C++ programmers. By using built-in functions, you can avoid having to write your own code for common tasks.

 

 

Share this

Function part 3 (built in functions)

Or copy link

CONTENTS
English