A Quick Guide for C++ #5
Functions
FUNCTIONS:
If the programs are complex and lengthy, they can be modularized into subprograms. The subprograms are called as functions. The subprogram can be developed
independently compiled and tested. They can be reused in other programs also.
A function is a named group of statements developed to solve a sub-problem and returns always a value to other functions when it is called.
Types of functions
There are two types of functions:
i. Library functions
ii. User-defined functions.
i. Library functions
A standard library is a collection of pre-defined functions and other programming elements which are accessed through header files
Header files are the files containing standard functions that a program may use . The header files should be written within angled brackets and its functions are included into our programs by #include directive
ii. User-defined functions
We can create our own functions or sub-programs to solve our problem. Such functions are normally referred to as user defined functions.
A user-defined function is a complete and independent program, which can be used (or invoked) by the main program or by the other sub-programs. The user-defined functions are written to perform definite calculations, after performing their task they send back the result to the calling program or sub-program.
Different header files
As said earlier, header files are the files containing standard functions that our programs may use. C++ contains many header files and is listed below.
stdio.h
This header file contains functions and macros to perform standard I/O operations. When we include the header file iostream.h, the header file stdio.h automatically included into our program.
string.h
This header file declares functions to manipulate strings.
stdlib.h
This header file is used to declare conversion routines, search/sort routines and other miscellaneous things.
iostream
This header file contains C++ streams and i/o routines.
iomanip.h
This header file contains functions and macros for input/output manipulators for creating parameterized manipulations.
math.h
This header file declares prototypes for the mathematical functions and error handlers.
The functions are used to perform mathematical calculations.
Mathematical library functions
C++ provides many mathematical functions. These functions can be used in mathematical expressions and statements. The various functions are ceil(), expo(), fabs(), floor(), log(), pow() etc.
Character and String functions
A character is any single character enclosed within single quotes. Some functions accept a character as argument. The argument is processed as an int by
using its ASCII code. To use these functions, the header file ctype.h should be included.
Inputting single character
We can input a character using the function get()
char ch;
cin = get(ch);
OR
char ch;
ch = cin.get();
Outputting single character
cout.put(ch);
put() function is used to display single character.
The general form is
Example:
char ch;
ch = cin.get();
cout.put(ch);
String functions
A string is sequence of characters enclosed within double quotes. Strings are manipulated as one-dimensional array of characters and terminated by null ('\0')character. C++ provides many functions to manipulate strings. To use these functions, the header file string.h should be included.
char string-name[size];
Declaring a string variable
The general form to declare a string is:
string-name is the name of the string variable
Size is the number of characters in the string. The size helps the compiler to allocate required number of memory locations to store the string
Example: char st[50];
Initializing a string
Like other variables, strings can also be initialized when they are declared.
Example: char s[10] ="Karnataka";
There are only 9 characters in the string. The null character automatically appended to the end of the string.
Inputting a string
C++ provides the function getline() to read a string.
cin.getline(string, size);
Example cin.getline(st, 25);
Outputting a string
C++ provides the function write() to output a string.
cout.write(string, size);
Example: cout.write(st, 25);
Some string manipulation functions are given below:
strlen() function
This function returns the length of the string. i.e., the number of characters present in the string, excluding the null character.
The general form is
variable = strlen(string);
Example:
l = strlen("Empress");// Returns 7.
strcat() function
This function is used to concatenate two strings. The process of combining two strings to form a string is called as concatenation.
strcpy() function
A string cannot be copied to another string by using assignment statement. The function strcpy() is used to copy a string into another string.
strcmp() function
This function is used to alphabetically compare a string with another string
This function is case-sensitive. i.e., it treats the uppercase letters and lowercase letters as different.
strcmpi() function
iostream
This header file contains C++ streams and i/o routines.
iomanip.h
This header file contains functions and macros for input/output manipulators for creating parameterized manipulations.
math.h
This header file declares prototypes for the mathematical functions and error handlers.
The functions are used to perform mathematical calculations.
Mathematical library functions
C++ provides many mathematical functions. These functions can be used in mathematical expressions and statements. The various functions are ceil(), expo(), fabs(), floor(), log(), pow() etc.
Character and String functions
A character is any single character enclosed within single quotes. Some functions accept a character as argument. The argument is processed as an int by
using its ASCII code. To use these functions, the header file ctype.h should be included.
Inputting single character
We can input a character using the function get()
char ch;
cin = get(ch);
OR
char ch;
ch = cin.get();
Outputting single character
cout.put(ch);
put() function is used to display single character.
The general form is
Example:
char ch;
ch = cin.get();
cout.put(ch);
String functions
A string is sequence of characters enclosed within double quotes. Strings are manipulated as one-dimensional array of characters and terminated by null ('\0')character. C++ provides many functions to manipulate strings. To use these functions, the header file string.h should be included.
char string-name[size];
Declaring a string variable
The general form to declare a string is:
string-name is the name of the string variable
Size is the number of characters in the string. The size helps the compiler to allocate required number of memory locations to store the string
Example: char st[50];
Initializing a string
Like other variables, strings can also be initialized when they are declared.
Example: char s[10] ="Karnataka";
There are only 9 characters in the string. The null character automatically appended to the end of the string.
Inputting a string
C++ provides the function getline() to read a string.
cin.getline(string, size);
Example cin.getline(st, 25);
Outputting a string
C++ provides the function write() to output a string.
cout.write(string, size);
Example: cout.write(st, 25);
Some string manipulation functions are given below:
strlen() function
This function returns the length of the string. i.e., the number of characters present in the string, excluding the null character.
The general form is
variable = strlen(string);
Example:
l = strlen("Empress");// Returns 7.
strcat() function
This function is used to concatenate two strings. The process of combining two strings to form a string is called as concatenation.
strcpy() function
A string cannot be copied to another string by using assignment statement. The function strcpy() is used to copy a string into another string.
strcmp() function
This function is used to alphabetically compare a string with another string
This function is case-sensitive. i.e., it treats the uppercase letters and lowercase letters as different.
strcmpi() function
This function is used to alphabetically compare a string with another string. This function is not case sensitive i,e it treats uppercase and lowercase letters as same.
strrev() function
This function is used to reverse the characters in a string.
strrev() function
This function is used to reverse the characters in a string.
Comments
Post a Comment