Unit C, Chapter-12 Functions
Unit C, Chapter-12 Functions
UNIT C
CHAPTER 12
FUNCTIONS
INTRODUCTION
In all our earlier programs, we were used only main() function to solve small problems. If the problem involves
a lengthy and complex algorithm, naturally, the length of the main() is also increase and cannot be managed
easily. Large programs can be made manageable by dividing it into smaller sub-programs or modules called
functions.
Defn: A function is a self contained block of independent statements designed to perform a specific task and
return a single value.
a) Built-in functions /Library functions: A standard library it has a collection of predefined functions
which are accessed through header files.
OR
Are those which are already created by C++ compiler.
Ex: clrscr(), getch() cout(), cin(), strlen(), strcat(), isalpha(), toupper() and so on.
b) User-defined functions (UDF’s): Is a complete and independent programs, which are created by the
user to solve their own problem.
Ex: add(), sub(), multi(), largest(), main() and so on.
Header file:
Are the files containing standard functions that our program may use.
1. stdio.h
It contains functions and macros to perform standard input output operations.
fgets() fputc() fscanf()
fwrite() scanf() printf()
fclose() getchar() putchar()
2. iostream.h
It contains C++ streams and I/O routines
open() close() get()
getline() read() write()
3. conio.h
It contains functions to display I/O on display screen
clrscr()
getch()
4. string.h
It contains functions to manipulate strings and memory manipulation routines.
strlen() strcat() strcmp()
strcpy() strstr() strrev()
strupr() strlwr() memcpy()
5. stdlib.h
Used to declare conversion routines
free() malloc()
calloc() realloc()
6. iomanip.h
It contains functions and macros for I/O manipulators
setfile() endl setw()
setprecision() doc Hex
7. math.h
It contains list of mathematical functions
sqrt() sin() tan()
pow() exp() ceil()
floor() asin() acos()
INTRODUCTION:
In early day’s, computers are used for process numerical type of data. Today, computers are frequently used for
processing non-numerical type of data such as character data. So, string helps us to discuss how such data are
stored and processed by the computer.
String Defn: String is a collection of character terminated or ending by NULL character ( “\0”).
Syntax:
<Char> <String-name> [size];
NOTE: The C++ compiler will append Null character ‘\0’ at the end of each string to indicate ending of each
string
STRING INITILIZATION:
Assign each character in a string, when they are declared in the program
Syntax:
<Char> <string-name> [size] = {initializer list};
Case 2: Group of characters including NULL (\0) character separated by comma and enclosed between a pair
of single quotes
Ex: char str[12] = {‘p’ , ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’, ‘m’, ‘i’, ‘n’, ‘g’, '\0'};
Here, str is a string variable of size 12. It can store maximum 12 characters including NULL
character.
Inputting a string
cin.getline() Is a string input function used to accept string in character by character basis until it finds
newline.
General syntax
cin.getline(string, size);
Ex:
char name[20];
cin.getline(name,20);
Outputting a string
cout.write() is a string function used to display string character by character basis until it finds null (\0)
character.
GS
cout.write(string ,size);
Ex:
char name[20]=”C++ Programming”;
cout.write(name,10);
STRING OPERATIONS:
The various operations can be performed on the strings are
1) To find the Length of string. strlen()
2) Concatenation of two strings. strcat()
3) Copy one string to another string. strcpy()
4) Comparison of two strings. strcmp()
5) Finding substring. strstr()
3) strcpy( ) function:
It copies the content of one string to another string.
4) strcmp( ) Function:
Used to compare two strings for equality. Comparison is done by character by character basis.
Syntax: strcmp(str1, str2);
Example
Note: To use all the string functions in C++, user must include string.h library file in the beginning of program.
Strrev( ) Function:
Reverse all the characters in a string.
Syntax: strrev(string);
Ex: strrev(“ABCD”);
Output: DCBA
b) Conversion function: Name begin with the prefix to, and are used to convert one type of character to
another.
Note: To use all the character functions in C++, user must include ctype.h library file in the beginning of
program.
Difference between character and a string
Character String
Characters are enclosed within pair of single Strings are enclosed within pair of double
quotes quotes.
char str[10]= ‘A’; char str[10]=”NEWEXPERT”;
Declared using char keyword Declared using char keyword
<ctype.h> header file is used to manipulate <string.h> header file is used to manipulate
character. string
NOTE:
Palindrome: Read a string from left to right & right to left it will give same meaning
Ex: MALAYALAM, MADAM, LIRIL & etc.
---*******---