Using Built-in Functions in C++: Easy Guide

Introduction to Built-in Functions in C++

Imagine you’re building a robot, and you have a toolbox full of tools. In C++, built-in functions are just like those tools. They are pre-made functions that are already available for you to use, and they perform common tasks. You don’t have to create them from scratch; they’re ready to go right away! It’s like having a set of handy tools that help you calculate numbers, work with strings, or get input from users.

In this article, we’ll explore the importance of built-in functions, why we need them, and how we can use them effectively in C++ programming. Let’s get started and make our coding adventures easier!

What is Exactly Built-in Function in CPP

Built-in functions in programming are predefined functions provided by the programming language itself. These functions serve specific purposes and are readily available for programmers to use without needing to create them from scratch. They are part of the core features of the language and are designed to perform common tasks efficiently. Programmers can simply call these functions by their names and pass any required arguments to achieve specific operations. Built-in functions save time and effort by eliminating the need to write complex code for routine tasks, making programming more convenient and productive.

For example, functions like “cout” to display output, “cin” to take input, “sqrt” to calculate square roots, and “strlen” to find the length of a string are all built-in functions. They save time and effort, making coding faster and more efficient.

How to Use Built-in Functions in C++

Using built-in functions in C++ is straightforward. Here’s how you can do it in easy-to-understand points:

  • Function Name: Identify the built-in function you want to use. Examples include ‘pow()‘, ‘sqrt()‘, ‘max()‘, ‘min()‘, etc.
  • Include Necessary Header: For most built-in functions, you need to include the appropriate header file. For example, ‘<cmath>‘ for mathematical functions.
  • Pass Parameters: Determine the required parameters for the function. They can be variables or constants. Pass them within parentheses.
  • Receive Results: Assign the result of the function to a variable if needed. Most functions return a value.
  • Display or Use Results: You can print the result using cout or use it further in your program.

Remember, built-in functions are tools to simplify your coding process, so you don’t need to write complex algorithms for common tasks.

List of top 10 inbuilt functions in C++

The 10 most used inbuilt functions of C++ will help you to save time and make code concise as well during competitive programming.

FunctionDescription
pow()Calculate power (exponentiation)
sqrt()Calculate square root
min()Determine minimum of two values
max()Determine a minimum of two values
swap()Swap values of two variables
gcd()Determine a maximum of two values
toupper()Convert character to uppercase
tolower()Convert character to lowercase
floor()Round up to the nearest integer
ceil()Calculate the greatest common divisor
List of top 10 inbuilt functions in C++

These functions are fundamental in C++ programming, making tasks like input/output, string manipulation, and data manipulation much more manageable.

1. pow()

This function helps you calculate the value of a number raised to another number. You provide two numbers as input (they can be decimals or integers), and the result you get is also a number. It’s like saying “number1 raised to the power of number2”.

Header file:

pow() function is defined inside the cmath header file.
#include <cmath>

Syntax:  

pow(base, exponent)
The result of this function will be baseexponent 

Time Complexity:  O(exponent).

Below is an example to illustrate the working of pow() method in C++:

C++
#include <iostream>
#include <cmath> // Including the <cmath> header for mathematical functions

int main() {
    double base = 2.0; // Define the base value
    double exponent = 3.0; // Define the exponent value
    
    // Using the pow() function to calculate the result
    double result = pow(base, exponent);
    
    // Displaying the calculation and result
    std::cout << "Calculating " << base << " raised to the power of " << exponent << std::endl;
    std::cout << "The result is: " << result << std::endl;
    
    return 0; // Indicating successful program execution
}

Output:

C++
Calculating 2 raised to the power of 3
The result is: 8

2. sqrt() 

This function is used to calculate the square root of a given number. It can take either a floating point or an integer value as input. The function then calculates the square root and returns the result, rounded to the appropriate data type.

Header file:

sqrt  function is defined inside cmath header file.
#include <cmath>

Syntax:

sqrt(N);

Time Complexity: θ(log(N))

Below is an example to illustrate the working of sqrt() method in C++:

C++
#include <iostream>
#include <cmath> // Include the cmath header for sqrt()

using namespace std;

int main() {
    double number = 25.0; // The number to find the square root of
    
    double squareRoot = sqrt(number); // Using the sqrt() function
    
    cout << "The square root of " << number << " is: " << squareRoot << endl;
    
    return 0;
}

Output:

C++
The square root of 25 is: 5

3. min()

This function is used to determine the smaller of two given numbers. It requires two numbers of the same type as inputs and provides the value of the smaller number as the output.

Header file:

This function is defined in algorithm header file.
#include <algorithm>

Syntax:

min(value1, value2);

Time Complexity: O(1)

Below is an example to illustrate the working of min() method in C++:

C++
#include <iostream>
#include <algorithm> // Include the necessary header for the min() function

using namespace std;

int main() {
    int a = 10, b = 7;
    
    int minimum = min(a, b); // Using the min() function
    
    cout << "The minimum of " << a << " and " << b << " is: " << minimum << endl;

    return 0;
}

Output:

C++
The minimum of 10 and 7 is: 7

4max()

This function is used to determine the larger value among two given values. It requires two values of the same type as input, and it gives back the greater value among those two.

Header file:

This function is defined in algorithm header file.
#include <algorithm>

Syntax:

max(value1, value2); 

Time Complexity: O(1).

Below is an example to illustrate the working of max() method in C++:

C++
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int num1 = 15, num2 = 28;

    int maxValue = max(num1, num2);

    cout << "The maximum value between " << num1 << " and " << num2 << " is: " << maxValue << endl;

    return 0;
}

Output:

C++
The maximum value between 15 and 28 is: 28

5. swap()

This function is designed to exchange the values of two numbers. It requires two values of the same data type as input and then switches their values between each other.

Header file:

#include <algorithm> (until C++11)
#include <utility> (since C++11)
#include <string_view> (since C++17)

Syntax:

swap(value1, value2);

Time Complexity: O(1)

Below is an example to illustrate the working of swap() method in C++:

C++
#include <iostream>
using namespace std;

int main() {
    int a = 5, b = 10;

    cout << "Before swapping:" << endl;
    cout << "a = " << a << ", b = " << b << endl;

    // Swapping the values of 'a' and 'b'
    swap(a, b);

    cout << "After swapping:" << endl;
    cout << "a = " << a << ", b = " << b << endl;

    return 0;
}

Output:

C++
Before swapping:
a = 5, b = 10
After swapping:
a = 10, b = 5

6. gcd() 

This function calculates the Greatest Common Divisor (GCD) of two numbers. It requires two values of the same data type as input and provides the GCD as its result.

Header file:

This function is defined in algorithm header file for C++14
#include <algorithm>
#include <numeric> (for C++17)

Syntax:

 __gcd(value1, value2);  [for C++14]
gcd(value1, value2);   [for C++17]

Time Complexity: O(log(max(value1, value2)))).

Below is an example to illustrate the working of gcd()  method in C++:

C++
#include <iostream>
#include <algorithm> // Required for gcd function

using namespace std;

int main() {
    int num1 = 12, num2 = 18;

    // Using the gcd function to find the greatest common divisor
    int gcd_result = gcd(num1, num2);

    cout << "GCD of " << num1 << " and " << num2 << " is: " << gcd_result << endl;

    return 0;
}

Output:

C++
GCD of 12 and 18 is: 6

7. toupper() 

This function is used for converting a lowercase character to uppercase.

Header file:

This function is defined in cctype header file 
#include <cctype>

Syntax:

toupper(‘ch’);   where ch is lower case character.

Time Complexity: O(1).

Below is an example to illustrate the working of toupper()  method in C++:

C++
#include <iostream>
#include <cctype> // Include the necessary header for toupper()

int main() {
    char ch = 'a'; // Declare a character variable

    // Convert the character to uppercase using toupper()
    char upperCh = toupper(ch);

    // Print the original and uppercase characters
    std::cout << "Original character: " << ch << std::endl;
    std::cout << "Uppercase character: " << upperCh << std::endl;

    return 0;
}

Output:

C++
Original character: a
Uppercase character: A

8. tolower()

This function is used for converting an uppercase character to a lowercase.

Header file:

This function is defined in cctype header file.
#include <cctype>

Syntax:

tolower(ch);   where ch is an uppercase character.

Time Complexity: O(1).

Below is an example to illustrate the working of tolower()  method in C++:

C++
#include <iostream>
#include <cctype>
using namespace std;

int main() {
    char ch = 'A';

    // Converting uppercase to lowercase using tolower()
    char lowercase = tolower(ch);

    cout << "Original character: " << ch << endl;
    cout << "Lowercase character: " << lowercase << endl;

    return 0;
}

Output:

C++
Original character: A
Lowercase character: a

9. floor()

This function gives you the highest integer value that is equal to or less than a given decimal number. It takes a decimal number as input and provides an integer output.

Header file:

floor function is defined in cmath header file
#include <cmath>

Syntax:

floor(value);

Time Complexity: O(1)

Below is an example to illustrate the working of floor()  method in C++:

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

int main() {
    double num = 7.8;

    // Using the floor() function to round down the number
    double result = floor(num);

    cout << "Original number: " << num << endl;
    cout << "Number after using floor(): " << result << endl;

    return 0;
}

Output:

C++
Original number: 7.8
Number after using floor(): 7

10. Ceil()

This function is the opposite of ‘floor()‘. It gives you the smallest integer that is greater than or equal to the provided number. It works with floating-point numbers and gives you an integer result.

Header file:

ceil function is defined in cmath header file
#include <cmath>

Syntax:

ceil(value);

Time Complexity: O(1)

Below is an example to illustrate the working of Ceil()  method in C++:

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

int main() {
    double num = 7.4;

    // Using the ceil() function to round up to the nearest integer
    double roundedUp = ceil(num);

    cout << "Original Number: " << num << endl;
    cout << "Rounded Up: " << roundedUp << endl;

    return 0;
}

Output:

C++
Original Number: 7.4
Rounded Up: 8

A Problem to Solve

Imagine you’re making a program to study weather information. You want to figure out the average temperature, discover the highest and lowest temperatures, and organize the data by date. With the help of built-in functions, you can do these tasks easily and without much effort.

Pros and Cons of Using Built-in Functions

ProsCons
Time-saving: Built-in functions save time and effort by providing ready-made solutions for common tasks.Limited customization: Built-in functions may not meet specific requirements or allow extensive customization.
Code simplicity: Using built-in functions makes your code shorter and more concise.Limited control: Built-in functions offer predefined functionality, limiting control over the internal implementation.
Code readability: Built-in functions contribute to code readability, making it easier to understand and maintain.Dependency: Relying too much on built-in functions can lead to difficulties when working with unfamiliar or specialized code.
Reliability: Built-in functions are thoroughly tested and reliable, reducing the chances of introducing errors.Learning curve: Understanding the proper usage and behavior of built-in functions may require some learning and practice.
Pros and Cons of Using Built-in Functions

Key Takeaways

  • Pre-Defined Functions: Built-in functions are pre-defined in C++, ready to use for common tasks.
  • Efficiency: They are optimized for efficiency, making your code run faster.
  • Readability: Using built-in functions can make your code easier to understand and maintain.
  • Wide Range of Tasks: These functions cover various operations like math, string manipulation, data conversion, etc.
  • Enhanced Programming: Learning to leverage built-in functions enhances your programming skills and productivity.

Conclusion

In summary, using built-in functions in C++ is like having a set of helpful tools at your disposal. These tools are already designed and ready to use, so you don’t need to create them from scratch. They make your programming tasks easier and faster, letting you focus on the specific things your program needs to do. It’s like having a recipe with pre-made ingredients – you can cook up a great program without spending too much time on every little detail. So, embrace these functions to make your coding journey smoother and your programs more efficient.

FAQs

  • What are built-in functions in C++?
    Built-in functions in C++ are pre-defined functions provided by the C++ Standard Library that perform common tasks.
  • Why are built-in functions important in C++?
    Built-in functions are important in C++ because they save time and effort. They make your code shorter, easier to read, and less prone to errors.
  • How do you use built-in functions in C++?
    To use a built-in function in C++, you need to include the appropriate library in your code. Then, you can call the function in your code.
  • What are some examples of built-in functions in C++?
    Some examples of built-in functions in C++ include sqrt() for calculating the square root of a number, rand() for generating random numbers, and sort() for sorting data.
  • What are the pros and cons of using built-in functions in C++?
    The pros of using built-in functions in C++ include making your code more efficient and easier to read. The cons include the need to understand how these functions work and when to use them.
Avatar Of Deepak Vishwakarma
Deepak Vishwakarma

Founder

RELATED Articles

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.