Types of Functions

13 – Types of Functions or Input/Output Functions in C

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.

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:

  • printf()
  • puts()
  • cout

(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.

Genral Format

printf(“escape sequence / format specifier”,variables/expressions);

Where,
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.

syntax:
Its syntax is:

puts(string);

  • 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 <<.

Syntax
The general syntax of cout is:

cout<<variable or constant or expression or/and “string of characters”

Where,
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.

Input Functions

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:

  • scanf()
  • getch()
  • getche()
  • getchar()
  • gets()

(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.

Format
It has the following general format:

scanf(“format specifier”,&var1, &var2, &var3………….);

Where,
“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.
For Example:
To input values into two integers variables, n1 & n2 of data type integer, the scanf() function is written as:

scanf(:%d,%d”,&a,&b);
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()

getch() function reads a character from the keyboard with no echo i.e. without echoing (displaying) it on the screen.

getche()

getche() function reads a character from the keyboard with echo i.e. echoing (displaying) it on the screen.

getchar()

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:

Syntax:
It has the following general format:
gets(Array Name)

Format Specifiers & Escape Sequences

Some useful Format Specifiers & Escape Sequences are given below:

Format Specifiers:

%d integer
%f float
%s string
%c character

Escape Sequences

\n to print new line
\t to print tab

That’s the end of Types of Functions.

Subscribe Our channels:

Click Here for more Articles:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.