In this chapter, we will talk about types of functions:
There are two types of Functions:
- Input Functions
- Output Functions
C offers many input/output functions.
Functions that are used to receive data from the computer memory & then to send it to the output devices are called Output Functions.
C language offers many output functions, such as:
(a) printf() Function
one of the most common output function in C is printf() . It is used to display data on the default output device (e.g. Monitor etc). It can be used to display messages, values of variables & to print the answers of arithmetic expressions.
printf(“escape sequence / format specifier”,variables/expressions);
escape sequences refers to a combination of characters beginning with a black slash ( \ ) followed by letters or special symbol. Escape sequences are used to format the output.
format specifier is the conversion specification code. It is used according to the format of output value.
variables/expressions is a list of quantities to be printed according to format specifiers.
(b) puts function
It is used to print a string on the screen.
Its syntax is:
- The string must be a string constant or string type variable.
- In case of string constant, the string is enclosed in double quotes.
- In case of string variable, it is written without using quotes.
(c) cout Function
In C++ program, the output on the screen can be obtained by using cout. It is basically an object. It is used along with insertion operator <<.
The general syntax of cout is:
cout<<variable or constant or expression or/and “string of characters”
Variable can be any variable whose value is to be displayed on the screen.
Constant value of constant can also be printed on the screen.
Expression Value obtained from any arithmetic expression can also be printed on the screen.
String of character A message or any string of character can be printed on the screen.
Note : It is to be noted that in order to use the cout<< we must include the header file iostream.h in the program.
Functions that are used to get data & then to assign it to variables are known as Input Functions. In C, there are several ways to assign data to variables.
The following are input functions in C:
(a) scanf() Function
It is used to accept values for variables during program execution. It allow users to run program with different input values without modifying any program statement. With scanf() function, a program will not be processed & result will not be displayed until you type the input values for variables.
It has the following general format:
scanf(“format specifier”,&var1, &var2, &var3………….);
“format specifier” specifies the format or variable through format specifiers. It is written within double quotes.
&var1, &var2, &var3,…………. are the list of variables separated by commas, into which the values are to be assigned . The symbol & used in scanf() represents tha address of memory location.
To input values into two integers variables, n1 & n2 of data type integer, the scanf() function is written as:
Here %d is a format specifier used for integer type variables.
(b) getch(), getche(), & getchar() functions
The getch(), getche(), & getchar() functions are used to read a single character from the keyboard. These do not require any arguments. Only a pair of parenthesis will be used with these functions.
getch() function reads a character from the keyboard with no echo i.e. without echoing (displaying) it on the screen.
getche() function reads a character from the keyboard with echo i.e. echoing (displaying) it on the screen.
getchar() function requires pressing the Enter Key after entering a character.
(c) gets Function
The gets function reads an entire string until user presses Enter key. It has the following general format:
It has the following general format:
Format Specifiers & Escape Sequences
Some useful Format Specifiers & Escape Sequences are given below:
\n to print new line
\t to print tab
That’s the end of Types of Functions.
Subscribe Our channels: