A Block of code which runs after it’s called is termed as c++ function. Function in C++ is a group of instructions where input is accepted then calculation is performed and output the result.
List of C++ Functions
what are functions in c++
Built-in Functions
Built-in functions can also be termed as library functions. We need not write them ourselves as these functions are usually provided by C++. We are supposed to directly use these functions in our code.
These functions are generally found to be placed in the header files of C++. For e.g., <cmath>, is the header that is having in-built math functions, and <string> is the header file that has in-built string functions respectively.
An Example of using built-in functions in a program is as follows:-
#include <iostream>
#include <string>
using namespace std;
int main()
{
string rishName;
cout << "Enter the string to be input :";
getline (std::cin, rishName);
cout << "Your entered string : " << rishName << "!\n";
int risize = rishName.size();
cout<<"Size of your entered string : "<<risize<<endl;
}
Output:
Enter the string to be input: Rajshree Verma
Your entered string: Rajshree Verma
Size of your entered string: 14
The header files <iostream> and <string> are being used here. In <iostream> library, the data types and other input/output functions are usually defined. Getline, and size are string functions used which are a part of the <string> header.
User-Defined Functions
Users of C++ are also allowed to define their own functions. They are generally called user-defined functions. These functions can be declared anywhere in the program and then can be called from any part of the code.
Let us have a detailed discussion on user-defined functions.
The syntax for user-defined functions which is generally in use is as given below:
returnType functName(parameter1,parameter2,….parameterN)
{
Body(or code to be executed) of the function;
}
So as we have seen above, each function consists of:
- Return type: It is the desired value that the functions are supposed to return to the calling function after performing a specific mentioned task.
- functionName: It is the identifier that is used to name a function.
- Parameter List: These are denoted by parameter1, parameter2,…parameterN in the above syntax. The parameter list is generally optional which means we can have functions that will not have any parameter.
- Function body: The piece of code or a group of instructions that would carry out a specific task.
Inline Functions
In order to make a function call, before passing control to the function, it involves a compiler stores the state of the program on a stack internally.
Inline functions are the functions that are usually expanded at the time of running of the program, which saves the efforts to call the function and prevention from the stack modifications. But even if a function is made to as inline, the compiler is not supposed to guarantee that it will be expanded at time of running the program.Or it can be said that it completely depends on the compiler to decide whether to make the function inline or not.
We can see an example of an Inline Function in the code below.
inline int rishaddition(const int &ri,const int &sh){
return (ri+sh);
}
As we can see in the above code, we have to precede the function definition with the keyword “inline” for making the function inline.