{"id":123593,"date":"2020-12-04T12:14:22","date_gmt":"2020-12-04T06:44:22","guid":{"rendered":"https:\/\/www.aplustopper.com\/?p=123593"},"modified":"2020-12-04T12:14:22","modified_gmt":"2020-12-04T06:44:22","slug":"plus-two-computer-application-notes-chapter-3","status":"publish","type":"post","link":"https:\/\/www.aplustopper.com\/plus-two-computer-application-notes-chapter-3\/","title":{"rendered":"Plus Two Computer Application Notes Chapter 3 Functions"},"content":{"rendered":"

Kerala Plus Two Computer Application Notes Chapter 3 Functions<\/h2>\n

String handling using arrays: A string is a combination of characters hence char data type is used to store string. A string should be enclosed in double quotes. In C++ a variable is to be declared before it is used.Eg. \u201cBVM HSS KALPARAMBU\u201d.<\/p>\n

Memory allocation for strings: To store \u201cBVM\u201d an array of char type is used. We have to specify the size. Remember each and every string is end with a null (\\0) character. So we can store only size-1 characters in a variable. Please note that \\0 is treated as a single character. \\0 is also called as the delimiter, char school_name[4]; By this we can store a maximum of three characters.<\/p>\n

\"Plus<\/p>\n

Consider the following declarations
\nchar my_name[10]=\u201dAndrea\u201d;
\nchar my_name2[]=\u201dAndrea\u201d;
\nchar str[ ]=\u201dHello World\u201d<\/p>\n

In the first declaration 10 Bytes will be allocated but it will use only 6+1 (one for \u2018\\0\u2019) = 7 Bytes the remaining 3 Bytes will be unused. But in the second declaration the size of the array is not mentioned so only 7 Bytes will be allocated and used hence no wastage of memory. Similarly in the third declaration the size of the array is also not mentioned so only 12( one Byte for space and one Byte for\u2018\\0\u2019) Bytes will be allocated and used hence no wastage of memory.<\/p>\n

Input \/ output operations on strings<\/p>\n

Consider the following code<\/p>\n

# include<iostream>\r\nusing namespace std;\r\nint main()\r\n{\r\ncharname[20];\r\ncout<<\u201cEnter your name:\u201d; cin>>name;\r\ncout<<\u201cHello \u201c<<name;\r\n}<\/pre>\n

If you run the program you will get the prompt as follows:
\nEnter your name: Alvis Emerin
\nThe output will be displayed as follows and the \u201cEmerin\u201d will be truncated.
\nHello Alvis<\/p>\n

This is because of cin statement that will take upto space. Here space is the delimiter. To resolve this gets() function can be used. To use gets() and puts() function the header file stdio.h must be included. gets() function is used to get a string from the keyboard including spaces.
\nputs() function is used to print a string on the screen. Consider the following code snippet that will take the input including the space.<\/p>\n

# include<iostream>\r\n#include<cstdio>\r\nusing namespace std;\r\nint main()\r\n{\r\ncharname[20];\r\ncout<<\u201cEnter your name:\u201d;\r\ngets(name);\r\ncout<<\u201cHello\u201d<<name;\r\n}<\/pre>\n

More console functions
\nInput functions<\/p>\n

\"Plus<\/p>\n

Output functions<\/p>\n

\"Plus<\/p>\n

Stream functions for I\/O operations: Some functions that are available in the header file iostream.h to perform I\/O operations on character and strings(stream of characters). It transfers streams of bytes between memory and objects. Keyboard and monitor are considered as the objects in C++.<\/p>\n

Input functions: The input functions like get( ) (to read a character from the keyboard) and getline() (to read a line of characters from the keyboard) is used with cin and dot(.) operator.<\/p>\n

\"Plus<\/p>\n

Eg.<\/p>\n

# include<iostream>\r\nusing namespace std;\r\nint main()\r\n{\r\nchar str[80], ch= 'z';\r\ncout<<\u201center a string that end with z:\u201d;\r\ncin.getline(str, 80, ch);\r\ncout<<str;\r\n}<\/pre>\n

If you run the program you will get the prompt as follows:
\nEnter a string that end with z: Hi I am Jobi. I am a teacherz. My school is BVM HSS
\nThe output will be displayed as follows and the string after \u2018z\u2019 will be truncated.
\nHi I am Jobi. I am a teacher<\/p>\n

Output function: The output functions like put() (to print a character on the screen) and write() (to print a line of characters on the screen) is used with cout and dot(.) operator.<\/p>\n

\"Plus<\/p>\n

Complex programs are divided into smaller subprograms. These subprograms are called functions.
\nEg. main(), clrscr(), sqrt(), strlen(),…<\/p>\n

Concept of modular programming: The process of converting big and complex programs into smaller programs is known as modularisation. These small programs are called modules or subprograms or functions. C++ supports modularity in programming called functions.<\/p>\n

Merits of modular programming<\/p>\n