Introduction to built-in Functions
The built-in functions are part of the C++ programming language. These are ready-made small program (or functions). The built-in functions are declared in different header files. C++ provides many built-in functions to solve various types of problems. The programmer uses built-in functions in writing program to perform various tasks. This saves the rogrammer’s time from writing his/her own functions. It is also reduce the source code of the program.
The built-in functions contains in a header file of programming language. Before using them in the program we have to declare header file that have these functions. For example, the “iostream” header file must be declared before using “clrscr()” function.
Functions Under “canio” Header File
What is clreol() Function
The word “clreol” is the short of clear end-of-line. The clreol() function is used to clear a line from the current cursor position up to the end of line. The other text printed on the screen is not cleared.
The declaration of clreol() function is as follows:
void clreol (void);
The declaration of this function indicates that no parameter is passed to this function. This function returns no value.
The general syntax to call clreol() function is as follows:
clreol () ;
Example Program of clreol() Function
gotoxy (0, 9);
What is getch() Function
The word “getch” stands for get character. The getch() function is used to get (input) a single character from keyboard during execution of the program. The entered character is not displayed on the screen. Similarly, the input process is automatically completed without pressing the Enter key. This function is normally used to pause the execution of program.
The declaration of getch() function is as follows:
int getch (void);
The declaration of this function indicates that no parameter is passed to this function. The output returned by this function is ‘int’ type. It returns the ASCII value of the input character. For example, if ‘A’ is entered, it returns ASCII value 65.
The general syntax to call getch() function is as follows:
[var =] getch();
The ‘ver‘ is of ‘int‘ type. The ASCII value of entered character is stored into it. The ‘var‘ can also be of ‘char‘ type. In this case, the entered character is stored into it. The use of [var =] is optional.
Example Program of getch() Function
The following program written in C++ that inputs a word of 8-characters length using “getch”. The entered characters are not seen on the screen during input process. The entered word should be displayed on the screen after the input process is completed.
using namespace std;
cout <<"Enter a word of 8 Characters:";
for (i = 0; i<=7; i++)
password [ i ] = getch();
password [ i ] = ' ';
cout<< "n You have entered word = "<<password;
What is kbhit() Function
The word “kbhit” stands for keyboard hit. The kbhit() function is used to check whether the user hit the keyboard or not during program execution. This function returns a non-zero (in the 1 form) value (true value) when any key on the keyboard is hit during the execution of the program. Otherwise, this function returns 0 (false value).
The declaration of kbhit() function
Generally, The kbhit() function is used as test condition in loop statement to terminate a loop if any key on the keyboard is press. For example:-
while (!kbhit() )
body of loop
The kbhit() Function Example Program
(A program that displays a message on the screen repeatedly until a key on the keyboard is hit.
int r = 1;
while ( !kbhit() )
if ( r== 24) r=1;
What is gotoxy() Function?
The declaration of the gotoxy() function is as follows:
The declaration of gotoxy() function indicates that two parameters both of “int” type are passed to this function. This output value returned by this function is nothing.
This general syntax to call gotoxy() function is given below:
gotoxy (x, y);
In the above syntax:
- x: It indicates the x-coordinate or column number on the screen. Its value can be 0 to 79.
- y: It indicates the y-coordinate or row number on the screen. Its value can be from 0 to 24 or 39.
Following statements will display “welcome” on the screen at row 16 and starting from column 5:
Functions Under “stdio” Header File
What is getchar() Function?
The word “getchar” is the short of get character. The getchar() function is similar to “getche” function of “canio.h” header file. It is also used to get a single character from a standard input device (i.e from keyboard) during program execution.
The main difference between getche() and getchar() function is that when getchar() function is used then we must have to pass Enter key to complete the input process.
The declaration of getchar() function is as follows:
Thet general syntax to call getchar() function is given below:-
Note: The use of [var =] is optional.
What is putchar() Function?
char: It represents the character to be displayed on the screen. It may be a character variable or character constant. In case of character constant, the character is enclosed in single quotation marks.
What is puts() Function?
The word “puts” is the short of put string. The puts() function is used to display string on the computer screen. The new line is automatically inserted after displaying the string.
The general syntax to call puts() function is as follows:
In the above syntax:
string: It represents the string to be displayed on the screen. It may be a string variable or string constant. In case of string constant, the string is enclosed in double quotation marks.
The following statement will display “welcome” on screen using puts() function:
This statement is equivalent to:
What is gets() Function?
The word “gets” stands for get string. The gets() function is used to input (or get) data into a string variable from the keyboard during program execution. Any type of characters, including spaces and special characters can be entered using this function. Enter key is pressed to complete the input process. When Enter key is pressed, a null character (‘