{"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":"
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
<\/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>\nIf 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>\nThis 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>\nMore console functions
\nInput functions<\/p>\n<\/p>\n
Output functions<\/p>\n
<\/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
<\/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>\nIf 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>\nOutput 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
<\/p>\n
Complex programs are divided into smaller subprograms. These subprograms are called functions.
\nEg. main(), clrscr(), sqrt(), strlen(),…<\/p>\nConcept 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
Demerits of modular programming
\nWhile dividing the program into smaller ones extra care should be taken otherwise the ultimate result will not be right.<\/p>\n
Functions in C++<\/span> Predefined functions String functions<\/span> b) strcpy() – It is used to copy the second string into the first string. c) strcat() – It is used to concatenate the second string into first one. d) strcmp() – it is used to compare two strings and returns an integer. Eg.<\/p>\n It returns a +ve integer.<\/p>\n e) strcmpi() – It is same as strcmp() but it is not case sensitive. That means uppercase and lowercase are treated as same. It returns 0. That is both are the same.<\/p>\n Mathematical functions. b) sqrt() – To find the square root of a number. c) pow() – To find the power of a number. Character functions<\/span> b) islower() – To check whether a character is in lowercase or not. If the character is in lowercase it returns a value 1 otherwise it returns 0. c) isalpha() – To check whether a character is an alphabet or not. If the character is an alphabet it returns a value 1 otherwise it returns 0. d) isdigit() – To check whether a character is a digit or not. If the character is a digit it returns a value 1 otherwise it returns 0. e) isalnum() – To check whether a character is an alphanumeric or not. If the character is an alphanumeric it returns a value 1 otherwise it returns 0. f) toupper() – It is used to convert the given character into uppercase. g) tolower() – It is used to convert the given character into lowercase. User defined functions<\/span><\/p>\n Syntax:<\/p>\n Different types of User-defined functions.<\/p>\n Prototype of functions<\/span><\/p>\n Consider the following codes<\/p>\n Method 1<\/span><\/p>\n Method 2<\/span><\/p>\n In method 1 the function is defined before the main function. So there is no error. Method 2<\/span><\/p>\n Functions with default arguments<\/span> Methods of calling functions: Two types call by value and call by reference. Scope and life of variables and functions Here the variable s is declared inside the function sum and has a local scope;<\/p>\n b) Global scope – A variable declared outside of all blocks can be used anywhere in the program.<\/p>\n Here the variable s is declared outside of all functions and we can use variable s anywhere in the program.<\/p>\n Kerala Plus Two Computer Application Notes Chapter 3 Functions 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. Memory allocation for […]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[42728],"tags":[],"yoast_head":"\n
\nSome functions that are already available in C++ are called pre defined or built in functions.
\nIn C++, we can create our own functions for a specific job or task, such functions are called user-defined functions.
\nA C++ program must contain a mainO function. A C++ program may contain many lines of statements(including so many functions) but the execution of the program starts and ends with main() function.<\/p>\n
\nTo invoke a function that requires some data for performing the task, such data is called parameter or argument. Some functions return some value back to the called function.<\/p>\n
\nTo manipulate string in C++ a header file called string.h must be included.
\na) strlen() – to find the number of characters in a string(i.e. string length).
\nSyntax: strlen(string);
\nEg.
\ncout<<strlen(\u201cComputer\u201d); It prints 8.<\/p>\n
\nSyntax: strcpy(string1, string2);
\nEg.
\nstrcpy(str, \u201cBVM HSS\u201d);
\ncout<<str; It prints BVM HSS.<\/p>\n
\nSyntax: strcat(string1, string2)
\nEg.
\nstrcpy(str1, \u201cHello\u201d);
\nstrcpy(str2, \u201cWorld\u201d);
\nstrcat(str1, str2);
\ncout<<str1; It displays the concatenated string \u201cHello World\u201d<\/p>\n
\nSyntax: strcmp(string1, string2)<\/p>\n\n
#include<iostream>\r\n#include<cstring>\r\nusing namespace std;\r\nint main()\r\n{\r\nchar str1[10], str2[10];\r\nstrcpy(str1, \u201cKiran\u201d);\r\nstrcpy(str2, \u201cJobi\u201d);\r\ncout<<strcmp(str1, str2);\r\n}<\/pre>\n
\nEg. \u201cANDREA\u201d and \u201cAndrea\u201d and \u201candrea\u201d these are same.<\/p>\n#include<iostream>\r\n#include<cstring>\r\nusing namespace std;\r\nint main()\r\n{\r\nchar str1[10], str2[10];\r\nstrcpy(str1, \"Kiran\u201d);\r\nstrcpy(str2, \"KIRAN\u201d);\r\ncout<<strcmpi(str1, str2);\r\n}<\/pre>\n
\nTo use mathematical functions a header file called math.h must be included
\na) abs() – To find the absolute value of an integer.
\nEg. cout<<abs(-25); prints 25.
\ncout<<abs(+25); prints 25.<\/p>\n
\nEg. cout<<sqrt(49); prints 7.<\/p>\n
\nSyntax. pow(number1, number2)
\nEg. cout<<pow(2, 10); It is equivalent to 210. It prints 1024.<\/p>\n
\nTo manipulate the character in C++ a header file called ctype.h must be included.
\na) isupper() – To check whether a character is in uppercase or not. If the character is in uppercase it returns a value 1 otherwise it returns 0.
\nSyntax: isupper(char ch);<\/p>\n
\nSyntax: islower(char ch);<\/p>\n
\nSyntax: isalpha(char ch);<\/p>\n
\nSyntax: isdigit(char ch);<\/p>\n
\nSyntax: isalnum(char ch);<\/p>\n
\nSyntax: toupper(char ch);<\/p>\n
\nSyntax: tolower(char ch);<\/p>\nReturn type Function_name(parameter list)\r\n{\r\nBody of the function\r\n}<\/pre>\n
\n
\n
# include<iostream>\r\nusing namespace std;\r\nint sum(int n1, int n2)\r\n{\r\nreturn(n1+n2);\r\n}\r\nint main()\r\n{\r\nint n1, n2;\r\ncout<<\u201cEnter 2 numbers:\u201d; cin>>n1>>n2;\r\ncout<<\u201cThe sum is \u201c<<sum(n1, n2);\r\n}<\/pre>\n
#include<iostream>\r\nusing namespace std;\r\nint main()\r\n{\r\nint n1, n2;\r\ncout<<\u201cEnter 2 numbers:\u201d; cin>>n1>>n2;\r\ncout<<\u201cThe sum is \u201c<<sum(n1, n2);\r\n}\r\nint sum(int n1, int n2)\r\n{\r\nretum(n1+n2);\r\n}<\/pre>\n
\nIn method 2 the function is defined after the main function and there is an error called \u201cfunction sum should have a prototype\u201d. This is because the function is defined after the main function. To resolve this a prototype should be declared inside the main function as follows.<\/p>\n# include<iostream>\r\nusing namespace std;\r\nint main()\r\n{\r\nint n1, n2;\r\nint sum(int, int);\r\ncout<<\u201cErrter 2 numbers:\"; cin>>n1>>n2;\r\ncout<<\u201cThe sum is \u201c<<sum(n1, n2);\r\n}\r\nint sum(int n1, int n2)\r\n{\r\nreturn(n1+n2);\r\n}<\/pre>\n
\nWe can give default values as arguments while declaring a function. While calling a function the user doesn\u2019t give a value as arguments the default value will be taken. That is we can call a function with or without giving values to the default arguments.<\/p>\n
\n1. Call by value: In the call by value method, the copy of the original value is passed to the function, if the function makes any change will not affect the original value.
\n2. Call by reference: In the call by reference method, the address of the original value is passed to the function, if the function makes any change will affect the original value.<\/p>\n
\na) Local scope – A variable declared inside a block can be used only in the block. It cannot be used in any other block.
\nEg.<\/p>\n#include<iostream>\r\nusing namespace std;\r\nint sum(int n1, int n2)\r\n{\r\nint s;\r\ns=n1+n2;\r\nreturn(s);\r\n}\r\nint main()\r\n{\r\nint n1, n2;\r\ncout<<\u201cEnter 2 numbers:\"; cin>>n1>>n2;\r\ncout<<\u201cThe sum is \u201c<<sum(n1, n2);\r\n}<\/pre>\n
# include<iostream>\r\nusing namespace std;\r\nint s;\r\nint sum(int n1, int n2)\r\n{\r\ns=n1+n2;\r\nreturn(s);\r\n}\r\nint main()\r\n{\r\nint n1, n2;\r\ncout<<\u201cEnter 2 numbers:\"; cin>>n1>>n2;\r\ncout<<\u201cThe sum is \u201c<<sum(n1, n2);\r\n}<\/pre>\n
Plus Two Computer Application Notes<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"