Types of Functions
- Built-in (library) functions
- User-defined functions
C++ Built-in Functions
These functions are part of the compiler package. These are part of standard library made available by the compiler. For example, exit(), sqrt(), pow(), strlen() etc. are library functions (built-in functions).
Built-in functions to use function without declaring and defining it. To use built-in functions in a program,Only include that header file where the required function is defined. Here is an example. This program uses built-in function named strlen() of string.h header file to find the length of the string.
Example of getline() and strlen() Function
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str[80];
cout<<"Enter any string (line):\n";
cin.getline(str, 80);
int len = strlen(str);
cout<<"\nLength of the string is: "<<len;
system("PAUSE");
return 0;
};
Here is the sample run of the above C++ program, finds the length of the string entered by the user
Output Result:
C++ User-defined Functions
The user-defined functions are created by the programmer. These functions are created as per requirements of your program. Here is an example:
#include<iostream>
#include<cstring>
using namespace std;
void start_msg(void); // function declaration
int main()
{
start_msg(); // function calling
int num;
cout<<"Enter a number: ";
cin>>num;
cout<<"You entered: "<<num;
system("PAUSE");
return 0;
};
void start_msg(void) // function definition
{
cout<<"Welcome to SOFTHUBSOLUTION\n";
cout<<"This is C++ Function Types Tutorial\n\n";
}
Here is the sample output of the above C++ program: