Skip to main content

Functions

Standard Library Functions

Introduction

So, imagine you're cooking, and you have your recipe (those are like the functions you create and define). But sometimes, you use pre-made ingredients or tools (those are like the standard library functions).

In C++, there are these pre-made functions that are already there for you to use without having to explain how they work. They are built-in functions that are already defined in library files. We call them "Standard Library Functions ". It's like having a magic tool that can do something specific and you just need to know how to use it. We can use them directly in our program.

For example, in the C++ programming language, there's a tool called cout. It's like a magical printer that prints whatever you want on the screen. You don't need to know exactly how the printer works; you just need to know how to tell it what to print.

So, when you write cout<< "Hello World ! ", you're using this pre-made tool to print "Hello World !" on the screen. You didn't have to create the printing tool; it's already part of the C++ toolkit.

And there are many other tools like this. These tools are like helpers you can call on whenever you need them, and you don't have to worry about how they do their job – you use them!

Maths Library Functions

Imagine you have a super calculator in C++ that can do all sorts of math tricks. Well, all these math tricks are neatly packed inside a special box called the " cmath header file".

So, when you want to use these math tricks, you just ask the cmath header file . For example, if you want to find the square root of a number, there's a trick called sqrt() inside the header file that you can use.

Many of the common mathematical library functions are defined inside the cmath header file. So, if we use code #include <cmath> in our program, we can use them directly in our program. It's like having a treasure box of math tools, and cmath is the key to opening that box.

Now, let's go through some of the cool math tricks one by one.

Power of a number

There's a function called pow() that lets you find the power of a number. It's like telling the calculator, "Hey, raise this number to that power for me!". This function is used to find the power of a number. It takes two arguments (base value and power value) and returns the power raised to the base.

Let's try it out in a simple program:

#include <iostream>
#include <cmath>
using namespace std;
 
int main() {
 
    int num, power;
    cout << "Enter the base number"<<endl;
    cin >> num;
    cout << "Enter the power"<<endl;
    cin >> power;
 
    // find the power of number raised to power
    int result = pow(num, power);
  
    cout << "You answer is " << result;
 
    return 0;
}

Now, when you run this program, it will ask you for a base number and a power. You can enter any number and it will magically tell you the result of raising the base to that power.

For example:

Enter the base number
5
Enter the power
3
Your answer is 125

Give it a try with different numbers!

Square root of a number

There is also a standard function called sqrt() that lets you calculate the square root. It's like telling the calculator, "Hey, give me the square root of this number!"

Let's try it out with a simple program:

#include <iostream>
#include <cmath>
using namespace std;
 
int main() {
 
    int num, power;
    cout << "Enter the  number for which you want to find square root"<<endl;
    cin >> num;
 
    // find the power of number raised to power
    int result = sqrt(num);
  
    cout << "You answer is " << result;
 
    return 0;
}

Now, when you run this program, it will ask you for a number, and it will magically tell you the square root of that number.

For example:

Enter the  number for which you want to find square root
144
You answer is 12

Minimum Value

The min(x, y) function is like saying, "Calculator, what's the smaller number between x and y?"

For example

#include <iostream>
#include <cmath>
using namespace std;
 
int main() {
 
    int num1, num2;
    cout << "Enter the  numbers "<<endl;
    cin >> num1;
    cin >> num2;
 
    int result = min(num1, num2);
    cout << "You answer is " << result;
 
    return 0;
}

Output

Enter the numbers
65
43
You answer is 43

Finding the Absolute Value

The abs(x) function is like saying, "Calculator, make x positive no matter what."

For example:

#include <iostream>
#include <cmath>
using namespace std;
 
int main() {
 
    int num;
    cout << "Enter the  number  "<<endl;
    cin >> num;

    int result = abs(num);
    cout << "You answer is " << result;
 
    return 0;
}

Your output will be as follows

Enter the number 
-8
Your answer is 8

The output will be 8 because the absolute value of -8 is 8.

Let's see one more standard library function which does not come under cmath header file.

Convert a Character to Uppercase

The toupper() function is used to convert a character to uppercase.This function is available in cctype header file in C++.

Let's see an example,

#include <iostream>
#include <cctype>
using namespace std;
 
int main() {
 
    char alphabet = 'f';
 
    // convert 'f' to uppercase
    char uppercase = toupper(alphabet);
    cout << "The uppercase of 'f' is " << uppercase;
 
    return 0;
}

Output

The uppercase of 'f' is " F

Note: There are several standard functions like these in C++. But one thing that we have to keep in mind is that we don't need to memorise all of them. As we practice questions we use different standard functions and it comes into our practice. Also I would suggest take a paper and maintain the list of the standard function which you use frequently while doing questions.

Learn More: Thank Me later !! 😉

Have you noticed one thing that we have used different header files such as cmath header file for mathematical computations (power of number, square root of number etc. ) and cctype for converting a character to uppercase? So, do we need to remember different header files for different functions ?? Absolutely not !!

I will tell you a header file that has all standard library functions. You can use it in your programming journey and make your life much easier.

<bits/stdc++.h> in C++ is a header file that includes every standard library. This reduces the pain of writing all the necessary header files and saves you time. Further when we cover DSA you will come across this header file.

See you in the next article with more energy and excitement ✌️ till then Happy Learning !! 😀