Computer Science STUDY MATERIAL 12th Standard

Computer Science STUDY MATERIAL 12th Standard 23
Dr.HenryEvans Profile Pic
Dr.HenryEvans,Germany,Professional
Published Date:10-07-2017
Your Website URL(Optional)
Comment
केय व यालय संगठन Kendriya Vidyalaya Sangathan STUDY MATERIAL (Computer Science) CLASS-XII 2014-15 KENDRIYA VIDYALAYA SANGATHAN GURGAON REGION SECTOR-14, OLD DELHI GURGAON ROAD, GURGAON (HARYANA)- 122001 1 Unit-I Objective Oriented Programming in C++ Review of C++:- C++ Character Sets: Letters A-Z , a-z Digits 0-9 Special Symbols Space + - / \ ( ) = = . „ “ , ; : % & ? _ = = White Spaces Blank spaces, horizontal tab, carriage return Any of the 256 ASCII character Other Characters Token:-The smallest individual unit in a program is known as token. Tokens used in C++ are: KEYWORDS : Keywords are the certain reserved words that convey a special meaning to the compiler. These are reserve for special purpose and must not be used as identifier name.eg for , if, else , this , do, etc. IDENTIFIERS: Identifiers are programmer defined names given to the various program elements such as variables, functions, arrays, objects, classes, etc.. It may contain digits, letters and underscore, and must begin with a letter or underscore. C++ is case sensitive as it treats upper and lower case letters differently. The following are some valid identifiers: Pen time580 s2e2r3 _dos _HJI3_JK LITERALS: The data items which never change their value throughout the program run. There are several kinds of literals: • Integer literals • Character literals • Floating literals • String literals Integer literals : Integer literals are whole numbers without any fractional part. An integer literal must have at least one digit and must not contain any decimal point. It may contain either + or - sign. A number with no sign is assumed as positive. C++ allows three types of integer literals: (i) Decimal Integer Literals:- An integer literal without leading 0 (zero) is called decimal integer literals e.g., 123, 786 , +97 , etc. (ii) Octal Integer Literals:- A sequence of octal digit starting with 0 (zero) is taken to be an octal integer literal ( zero followed by octal digits). e.g., 0345, 0123 , etc. (iii) Hexadecimal Integer Literals :- Hexadecimal Integer Literals starts with 0x or 0X followed by any hexa digits. e.g., 0x9A45, 0X1234, etc. 7 Character literals: Any single character enclosed within single quotes is a character literal. e.g ‘ A’ , ‘3’ Floating literals: Numbers which are having the fractional part are referred as floating literals or real literals. It may be a positive or negative number. A number with no sign is assumed to be a positive number. e.g 2.0, 17.5, -0.00256 String Literals: It is a sequence of character surrounded by double quotes. e.g., “abc” , “23”. PUNCTUATORS: The following characters are used as punctuators which are also known as separators in C++ ( ) , ; : ……….. = Punctuator Name Function Brackets These indicates single and multidimensional array subscripts () Parenthesis These indicate function calls and function parameters. Braces Indicate the start and end of compound statements. ; Semicolon This is a statement terminator. , Comma It is used as a separator. : Colon It indicates a labeled statement Asterisk It is used as a pointer declaration … Ellipsis These are used in the formal argument lists of function prototype to indicate a variable number of arguments. = Equal to It is used as an assigning operator. Pound sign This is used as preprocessor directives. OPERATORS: An operator is a symbol or character or word which trigger some operation (computation) on its operands. (i) Unary operators: Those which require only one operand to operate upon. e.g. unary - , unary + , ++ , - - . (ii) Binary operators: Binary operators require two operands to operate upon. e.g. +, , /, -, etc. (iii) Ternary Operator :Ternary operator require three operands to operate upon. Conditional operator (? :) is a ternary operator in C++. 8 Structure of a C++ program: Structure of a C++ program A C++ program to prints a string on the screen includeiostream.h includeiostream.h void main () void main () Statement_1; cout “Kendriya Vidyalaya”; Statement_2; : : The program produces following output: Kendriya Vidyalaya COMMENTS IN A C++ PROGRAM.: Comments are the pieces of code that compiler ignores to compile. There are two types of comments in C++. 1. Single line comment: The comments that begin with // are single line comments. The Compiler simply ignores everything following // in the same line. 2. Multiline Comment : The multiline comment begin with / and end with / . This means everything that falls between / and / is consider a comment even though it is spread across many lines. Input Output (I/O) operations In C++: Input & Output operations are supported by the istream (input stream) and ostream (output stream) classes. The predefined stream objects for input, output are : (i) The cout Object: The identifier cout is a predefined object of ostream class that represents the standered output stream in C++ and tied to slandered output. cout stands for console output . cout sends all output to the standard output device i.e. monitor. The syntax of cout is as follows: cout data; Where data may be a variable or constant or string etc. e.g. cout a ; ( here a can be any variable) Output Operator (): The output operator () is also known as ‘stream insertion’ or ‘put to’ operator. It directs the contents of the variable (or value) on its right to the object on its left (i.e., cout). (ii) The cin Object : The cin object is an istream class object tied to slandered input. cin stands for console input. cin object used to get input from the keyboard. When a program reaches the line with cin, the user at the keyboard can enter values directly into variables. The syntax of cin is as follows: cin variablename; e.g cin ch; ( here ch can be any variable) 9 input Operator (): The input operator () is also known as extraction or ‘get from’ operator . It extracts (or takes) the value from the keyboard and assign it to the variable on its right. CASCADING OF OPERATOR: When input or output ( or ) are used more than one time in a single statement then it is called as cascading of operators. e.g cout roll age endl; DATA TYPES IN C++: Data types are means to identify the types of data and associated operations of handling it. Data types in C++ are of two types: 1. Fundamental or Built-in data types . 2. Derived data types. 1. Fundamental or Built-in data types: These data types are already known to compiler. These are the data types those are not composed of other data types. There are following fundamental data types in C++: (i) :- int data type is used for integer value. An identifiers int data type (for integer) declare as int cannot have fractional part. (iii) An identifiers declare as char can store a character. char data type (for characters):- (iv) float data type (for floating point numbers):- An identifier declare as float can hold a floating point number. (v) double data type (for double precision floating point numbers):- The double data type is also used for handling floating point numbers but it occupies twice as much memory as float and store numbers with much larger range and precision. There are following four data type modifiers in C++ , which may be Data Type Modifiers:- used to modify the fundamental datatypes to fit various situations more precisely: (i) signed (ii) unsigned (iii) long (iv) short signed, unsigned, long, short data type modifiers may be apply to char & int data types. However you may also apply long to double Data Type Size (in Bytes) Range char 1 -128 to 127 unsigned char 1 0 to 255 Signed char 1 Same as char Int 2 -32768 to 32767 unsigned int 2 0 to 65535 signed int 2 Same as int short (or short int) 2 -32768 to 32767 long (or long int) 4 -2147483648 to 2147483647 -38 38 float 4 3.4 x 10 to 3.4 x 10 – 1 (upto 7 digits of precision) -308 308 double 8 1.7 x 10 to 1.7 x 10 – 1 (upto 15 digits of precision) -4932 4932 long double 10 3.4 x 10 to 1.1 x 10 – 1 (upto 19 digits of precision) 10 2. Derived Data Types:- These are the data types that are composed of fundamental data types. e.g., array, class, structure, etc. Variables:-A named memory location, whose contains can be changed with in program execution is known as variable. OR A variable is an identifier that denotes a storage location, which contains can be varied during program execution. Declaration of Variables:- All variables must be declared before they are used in executable statements. Variable declaration reserves memory required for data storage and associates it with a name. Syntax for variable declaration is: datatypes variable_name1, variable_name2, variable_name3,……………. ; e.g., int num; int num, sum, avg; We can also initialize a variable at the time of declaration by using following syntax: datatypes variable_name = value; e.g., int num = 0; Constant:- A named memory location, whose contains cannot be changed with in program execution is known as constant. OR A constant is an identifier that denotes a storage location, which contains cannot be varied during program execution. Syntax for constant declaration is: const datatypes constant_name = value ; e.g., const float pi = 3,14f ; Formatted Output (Manipulators) : Manipulators are the operators used with the insertion operator to format the data display. The most commonly used manipulators are endl and setw. 1. The endl manipulator :The endl manipulator, when used in a output statement , causes a line feed to be inserted. It has same effect as using new line character “\n”. e.g., cout “ Kendriya Vidyalaya Sangathan”endl; cout “ Human Resource and Development”; The output of the above code will be Kendriya Vidyalaya Sangathan Human Resource and development 2. The setw( ) Manipulator : The setw( ) manipulator sets the width of the field assign for the output. It takes the size of the field (in number of character) as a parameter. The output will be right justified e.g., the code : coutsetw(6)”R” ; Generates the following output on the screen (each underscore represent a blank space) _ _ _ _ _ R In order to use these manipulator , it is must to include header file iomanip.h 11 Operators:- Arithmetic operators :-Those operators are operates only on numeric data types operands are known as arithmetic operators. Operator Operation Example Unary - Result is the negation of If a=5 then – a means -5. operand’s value (Reverse the If a = - 4 then – a means 4. sign of operand’s value) Unary + The result is the value of its If a=5 then +a means 5. operand If a = - 4 then +a means -4. + Addition ( it adds two 4+20 results is 24. (Addition Operator) numbers) If a = 5 then a + 5 results 10. - Subtraction ( Subtract the 14 – 3 evaluates to 12. ( Subtraction Operator) second operand from first) If a = 2 , b = 3 then b – a evaluates 1. Multiplies the values of its 34 evaluates to 12. (Multiplication Operator) operands If a=2, b=3 then ab evaluates to 6. / Divides its first operand by the 100/5 evaluates 20. (Division Operator) second If a =10 , b = 5 then a/b evaluates 2 % It produce the remainder of 19%6 evaluates to 1. (Modulus Operator) dividing the first operand by If a = 14 , b = 3 then a%b second evaluates to 2. Modulus operator requires that both operands be integer and second operand be non-zero. Increment and Decrement Operators (++ , - -) : The increment operator (++) adds 1 to its operand and decrement operator () subtract one from its operand. In other word a = a + 1; is same as ++a; or a++; & a = a – 1 ; is same as a; or a; Both the increment & decrement operators comes in two version : (i) Prefix increment/decrement :- When an increment or decrement operator precedes its operand, it is called prefix increment or decrement (or pre-increment / decrement). In prefix increment/decrement , C++ perform the increment or decrement operation before using the value of the operand. e.g., If sum = 10 and count =10 then Sum = sum +(++count); First count incremented and then evaluate sum = 21. (ii) Postfix increment/decrement :- When an increment or decrement operator follows its operand, it is called postfix increment or decrement (or post-increment / decrement). In postfix increment/decrement , C++ first uses the value of the operand in evaluating the expression before incrementing or decrementing the operand’s value. e.g., If sum = 10 and count =10 then Sum = sum +(count++); First evaluate sum = 20 , and then increment count to 11. 12 Relational Operator: These operators are used to compare two values. If comparison is true, the relational expression results into the value 1 and if the comparison is false its result will be 0. The six relational operators are: Operator Meaning = = Equal to = Not equal to Less than = Less than or equal to Greater than = Greater than or equal to Logical Operators : In addition to the relational operator, C++ contains three logical operators. Relational operators often are used with logical operators to construct more complex decision making expressions. Operators Use Return True if && (Logical AND) op1 && op2 op1 and op2 are both true (Logical OR) op1 op2 Either op1 or op2 is true (Logical NOT) op1 op1 is false (it is unary operator) Assignment Operator: C++ offers an assignment operator (=) to assign a value to an identifier. The assignment statement that make use of this operator are written in the form : var = expression ; where var generally represents a variable and expression may be a constant or a variable or an expression. C++ offers special shorthand operators that simplify the coding of a certain type of assignment statement . e.g., a = a + 10 ; can be written as a+=10 ; This shorthand works for all binary arithmetic operators. The general form of this shorthand is Var = var operator expression ; is same as var operator = expression ; Following are some examples of C++ shorthands: x -=10 ; equivalent to x = x -10 ; x=3 ; equivalent to x = x 3 ; x/=2 ; equivalent to x = x/2 ; x%=z equivalent to x = x % z ; Conditional operator ( ? : ) The conditional operator (? :) is a ternary operator i.e., it require three operands. The general form of conditional operator is: expression1? expression2: expression3 ; Where expression1 is a logical expression , which is either true or false. If expression1 evaluates to true i.e., 1, then the value of whole expression is the value of expression2, otherwise, the value of the whole expression is the value of expression3. For example 13 min = ab? a : b ; Here if expression (ab ) is true then the value of a will be assigned to min otherwise value of b will be assigned to min. Comma operator ( , ) The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the rightmost expression is considered. For example, the following code: a = (b =3 , b +2 ); Would first assign the value 3 to b, and then assign b+2 to variable a. So, at the end, variable a would contain the value 5 while variable b would contain value 3. sizeof() This operator returns the size of its operand in bytes. The operand may be an expression or identifier or it may be a data type. a= sizeof (char); This will assign the value 1 to a because char is a one-byte long type. -An expression in C++ is any valid combination of operators, constants, and Expressions: variables. Pure Expressions:-If an expression have all operand of same data types then it is called a pure expression. Mixed Expressions :-If an expression have operands of two or more different data types then it is called a mixed expression. Arithmetic Expressions:-Arithmetic expression can either be integer expressions or real expressions. Sometimes a mixed expression can also be formed which is a mixture of real and integer expressions. Integer Expressions:- Integer expressions are formed by connecting all integer operands using integer arithmetic operators. Real Expressions:- Real expressions are formed by connecting real operands by using real arithmetic operators. Logical Expressions:- The expressions which results evaluates either 0 (false) or 1 (true) are called logical expressions. The logical expressions use relational or Logical operators. The process of converting one predefined data type into another is called Type Conversion:- type conversion. C++ facilitates the type conversion in two forms: (i) Implicit type conversion:- An implicit type conversion is a conversion performed by the compiler without programmer’s intervention. An implicit conversion is applied generally whenever different data types are intermixed in an expression. The C++ compiler converts all operands upto the data type of the largest data type’s operand, which is called type promotion. (ii) Explicit type conversion :- An explicit type conversion is user-defined that forces an expression to be of specific data type. :- The explicit conversion of an operand to a specific type is called type Type Casting casting. 14 Type Casting Operator - (type) :-Type casting operators allow you to convert a data item of a given type to another data type. To do so , the expression or identifier must be preceded by the name of the desired data type , enclosed in parentheses . i. e., (data type) expression Where data type is a valid C++ data type to which the conversion is to be done. For example , to make sure that the expression (x+y/2) evaluates to type float , write it as: (float) (x+y/2) Precedence of Operators:- Operator precedence determines which operator will be performed first in a group of operators with different precedence. For instance 5 + 3 2 is calculated as 5 + (3 2), giving 11 Statements:-Statements are the instructions given to the Computer to perform any kind of action. Null Statement:-A null statement is useful in those case where syntax of the language requires the presence of a statement but logic of program does not give permission to do anything then we can use null statement. A null statement is nothing only a ;. A null (or empty statement have the following form: ; // only a semicolon (;) Compound Statement :-A compound statement is a group of statements enclosed in the braces . A Compound statement is useful in those case where syntax of the language requires the presence of only one statement but logic of program have to do more thing i.e., we want to give more than one statement in place of one statement then we can use compound statement. St-1; St-2; : : Statement Flow Control:-In a program , statements may be executed sequentially, selectively, or iteratively. Every programming language provides three constructs: 1. Sequence Constructs 2. Selection Constructs 3. Iteration Constructs Sequence Construct:-The sequence construct means the statements are being executed sequentially. It represents the default flow of statements. Selection Construct:- The selection construct means the execution of statement(s) depending on a condition. If a condition is true, a group of statements will be execute otherwise another group of statements will be execute. 15 Looping or Iteration Statements:-Looping the iteration construct means repetition of set of statements depending upon a condition test. The iteration statements allow a set of instructions to be performed repeatedly until a certain condition is true. Body of Loop Loop condition There are two types of loops:- 1. Entry-controlled loop :-In entry-controlled loop first of all loop condition is checked and then body of loop is executed is condition is true. If loop condition is false in the starting the body of loop is not executed even once. 2. Exit-controlled loop :-In exit-controlled loop first body of loop is executed once and then loop condition is checked. If condition is true then the body of loop will be executed again. It means in this type of loop, loop body will be executed once without checking loop condition. Selection Statements :-There are two types of selection statements in C++ : 1. if statement 2. switch statement 1. if Statement : If statement have three forms 16 (a) if … else Statement :- It is useable, when we have to performs an action if a condition is True and we have to perform a different action if the condition is false. The syntax of if…else statement is: if ( conditional expression ) statement-1 or block-1; // statements to be executed when conditional expression is true. else statement-2 or block-2; // statements to be executed when conditional expression is false. If the conditional expression is evaluated to true then the statement-1 or block-1 (statement under if ( ) block ) will be executed otherwise the statement-2 or block-2 (statements under else block) would be executed. if there exists only one program statement under if( ) block then we may omit curly braces . (b) Simple if statement:- The else part in if … else statement is optional, if we omit the else part then it becomes simple if statement. This statement is usable, when we have to either perform an action if a condition is True or skips the action if the condition is false. The syntax of simple if statement is: if ( conditional expression ) statement-1 or block-1; // statements to be executed when conditional expression is true. Here statement-1 or block-1 will be executed only if conditional expression evaluates true. if there exists only one program statement under if( ) block then we may omit curly braces . (c) The if-else-if ladder :-This statement allows you to test a number of mutually exclusive cases and only execute one set of statements for which condition evaluates true first. The syntax is: if ( condition -1 ) statement-1; // do something if condition-1 is satisfied (True) else if ( condition – 2 ) statement-3 ; // do something if condition -2 is satisfied (True) else if (condition – 3 ) statement-3 ; // do something if condition- 3 is satisfied (True) : : // many more n-1 else - if ladder may come : else if( condition – n ) statement-n ; // do something if condition – n is satisfied (True) else statement-m ; // at last do here something when none of the // above conditions gets satisfied (True) 17 in syntax is known as a place holder, it is not a part of syntax, do not type it while writing program. It only signifies that anything being kept there varies from program to program. is also not a part of syntax , it is used to mark optional part of syntax i.e. all part of syntax between is optional. In the above syntax there are ladder of multiple conditions presented by each if( ) , all of these conditions are mutually exclusive. If one of them would evaluates true then the statement followed that condition will be executed and all the conditions below it would not be evaluated (checked). Say suppose if condition-3 gets satisfy (i.e. evaluates true value for the condition), then statement-3 gets executed and all other conditions below it would be discarded. If none of the n if ( ) conditions gets satisfied then the last else part always gets executed. It is not compulsory to add an else at the last of the ladder. We can also write a group of statement enclosed in curly braces (as a compound statement) in place of any statement ( statement-1. Statement-2,……., statement-n) if required in above syntax. Nested if Statement:- If an if statement is written in the if or else clause of another if statement then it is known as nested if. Some possible syntax of nested if statements given below: Syntax 1:- if ( outer- condition ) if ( inner-condition ) //some statements to be executed // on satisfaction of inner if ( ) condition. // end of scope of inner if( ) //some statements to be executed // on satisfaction of outer if ( ) condition. // end of the scope of outer if( ) Syntax 2:- if ( outer- condition ) if ( inner-condition ) //some statements to be executed // on satisfaction of inner if ( ) condition. else // statements on failure of inner if( ) 18 //some statements to be executed // on satisfaction of outer if ( ) condition. else // statements on failure of outer if( ) 2. switch Statement :-This is multi-branching statement. Syntax of this statement is as follows: switch (expression/variable) case value_1: statement -1; break; case value_2: statement -2; break; : : case value_n: statement -n; break; default: statement -m Note: expression/variable should be integer or character type only. When the switch statement is executed, the expression/variable is evaluated and control is transferred directly to the statement whose case label value matches the value of expression/ variable. If none of the case label value matches the value of expression/variable then only the statement following the default will be executed. If no default statement is there and no match is found then no action take place. In this case control is transferred to the statement that follows the switch statement. Loops in C++:-There are three loops or iteration statements are available in C++ 1. for loop 2. while loop 3. do…. while loop 1. The for Loop:For loop is a entry control loop the syntax of for loop is : for(initialization_expression(s); loop_Condition; update_expression) Body of loop Working of the for Loop:- 1. The initialization_expression is executed once, before anything else in the for loop. 2. The loop condition is executed before the body of the loop. 3. If loop condition is true then body of loop will be executed. 4. The update expression is executed after the body of the loop 19 5. After the update expression is executed, we go back and test the loop condition again, if loop_condition is true then body of loop will be executed again, and it will be continue until loop_condition becomes false. Example: for (int i = 0; i 7; i++) cout i i endl; Interpretation: 2 An int i is declared for the duration of the loop and its value initialized to 0. i is output in the body of the loop and then i is incremented. This continues until i is 7. 2. while Loop:- while loop is also an entry controlled loop. The syntax of while loop is : while (loop_condition) Loop_body Where the Loop_body may contain a single statement, a compound statement or an empty statement. The loop iterates (Repeatedly execute) while the loop_condition evaluates to true. When the loop_condition becomes false, the program control passes to the statement after the loop_body. In while loop , a loop control variable should be initialized before the loops begins. The loop variable should be updated inside the loop_body. 3. do-while loop:- do-while loop is an exit-controlled loop i.e. it evaluates its loop_condition at the bottom of the loop after executing its loop_body statements. It means that a do-while loop always executes at least once. The syntax of do-while loop is: do Loop_body while (loop_condition); In do-while loop first of all loop_body will be executed and then loop_condition will be evaluates if loop_condition is true then loop_body will be executed again, When the loop_condition becomes false, the program control passes to the statement after the loop_body. Nested Loops :-Any looping construct can also be nested within any other looping construct . Let us look at the following example showing the nesting of a for( ) loop within the scope of another for( ) loop : for(int i = 1 ; i=2 ; i++) Outer for( ) loop for( int j = 1 ; j=3 ; j++) Inner for( ) loop cout i j endl ; For each iteration of the outer for loop the inner for loop will iterate fully up to the last value of inner loop iterator. The situation can be understood more clearly as : st 1 Outer Iteration i= 1 20 st 1 Inner Iteration j = 1 , output : 1 1 = 1 nd 2 Inner Iteration j = 2 , output : 1 2 = 2 rd 3 Inner Iteration j = 3 , output : 1 3 = 3 nd 2 Outer Iteration i= 2 st 1 Inner Iteration j = 1 , output : 2 1 = 1 nd 2 Inner Iteration j = 2 , output : 2 2 = 4 rd 3 Inner Iteration j = 3 , output : 2 3 = 6 You can observe that j is iterated from 1 to 2 every time i is iterated once. Jump Statements:-These statements unconditionally transfer control within function . In C++ four statements perform an unconditional branch : 1. return 2. goto 3. break 4. continue 1. return Statement:- The return statement is used to return from a function. It is useful in two ways: (i) An immediate exit from the function and the control passes back to the operating system which is main’s caller. (ii) It is used to return a value to the calling code. 2. goto statement :- A goto Statement can transfer the program control anywhere in the program. The target destination of a goto statement is marked by a label. The target label and goto must appear in the same function. The syntax of goto statement is: goto label; : label : Example : a= 0; start : cout“\n” ++a; if(a50) goto start; 3. break Statement :- The break statement enables a program to skip over part of the code. A break statement terminates the smallest enclosing while, do-while, for or switch statement. Execution resumes at the statement immediately following the body of the terminated statement. The following figure explains the working of break statement: 21 4. continue Statement:- The continue is another jump statement like the break statement as both the statements skip over a part of the code. But the continue statement is somewhat different from break. Instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code in between. The following figure explains the working of continue statement: Functions :- Function is a named group of programming statements which perform a specific task and return a value. There are two types of functions:- 1. Built-in (Library )functions 2. User defined functions Built-in Functions (Library Functions) :- The functions, which are already defined in C++ Library ( in any header files) and a user can directly use these function without giving their definition is known as built-in or library functions. e.g., sqrt( ), toupper( ), isdigit( ) etc. 22 Following are some important Header files and useful functions within them : stdio.h (standard I/O function) gets( ) , puts( ) ctype.h (character type function) isalnum( ) , isalpha( ), isdigit ( ), islower (), isupper ( ), tolower ( ), toupper( ) string.h ( string related function ) strcpy ( ), strcat ( ), strlen( ), strcmp( ) , strcmpi( ) , strrev( ),strupr( ) , strlwr( ) math.h (mathematical function) fabs ( ), pow ( ), sqrt ( ), sin ( ), cos ( ), abs ( ) stdlib.h randomize ( ), random ( ) The above list is just few of the header files and functions available under them , but actually there are many more. The calling of library function is just like User defined function , with just few differences as follows: i) We don't have to declare and define library function. ii) We must include the appropriate header files , which the function belongs to, in global area so as these functions could be linked with the program and called. Library functions also may or may not return values. If it is returning some values then the value should be assigned to appropriate variable with valid datatype. gets( ) and puts( ) : these functions are used to input and output strings on the console during program run-time. gets( ) accept a string input from user to be stored in a character array. puts() displays a string output to user stored in a character array. isalnum( ) , isalpha( ), isdigit( ) : checks whether the character which is passed as parameter to them are alphanumeric or alphabetic or a digit ('0' to '9') . If checking is true functions returns 1. islower ( ),isupper ( ), tolower ( ), toupper( ) : islower( ) checks whether a character is lower case , isupper( ) check whether a character is upper case . tolower( ) converts any character passed to it in its lower case and the toupper( ) convert into upper case. fabs ( ), pow ( ), sqrt ( ), sin ( ), cos ( ), abs ( ) : randomize ( ), random ( ) : The above functions belongs to header file stdlib.h . Let us observe the use of these functions : randomize( ) : This function provides the seed value and an algorithm to help random( ) function in generating random numbers. The seed value may be taken from current system’s time. random(int ) : This function accepts an integer parameter say x and then generates a random value between 0 to x-1 for example : random(7) will generate numbers between 0 to 6. To generate random numbers between a lower and upper limit we can use following formula random(U – L +1 ) + L where U and L are the Upper limit and Lower limit values between which we want to find out 23 random values. For example : If we want to find random numbers between 10 to 100 then we have to write code as : random(100 -10 +1) + 10 ; // generates random number between 10 to 100 User-defined function :- The functions which are defined by user for a specific purpose is known as user-defined function. For using a user-defined function it is required, first define it and then using. Function Prototype :- Each user define function needs to be declared before its usage in the program. This declaration is called as function prototype or function declaration. Function prototype is a declaration statement in the program and is of the following form : Return_type function_name(List of formal parameters) ; Declaration of user-defined Function: In C++ , a function must be defined, the general form of a function definition is : Return_type function_name(List of formal parameters) Body of the function Where Return_type is the data type of value return by the function. If the function does not return any value then void keyword is used as return_type. List of formal parameters is a list of arguments to be passed to the function. Arguments have data type followed by identifier. Commas are used to separate different arguments in this list. A function may be without any parameters, in which case , the parameter list is empty. statements is the function's body. It is a block of statements surrounded by braces . Function_name is the identifier by which it will be possible to call the function. e.g., int addition (int a, int b) int r ; r=a+b ; return (r) ; Calling a Function:- When a function is called then a list of actual parameters is supplied that should match with formal parameter list in number, type and order of arguments. Syntax for calling a function is: function_name ( list of actual parameters ); e.g., include iostream int addition (int a, int b) int r; r=a+b; return (r); void main ( ) int z ; z = addition (5,3); cout "The result is " z; The result is 8 24 Call by Value (Passing by value) :- The call by value method of passing arguments to a function copies the value of actual parameters into the formal parameters , that is, the function creates its own copy of argument values and then use them, hence any chance made in the parameters in function will not reflect on actual parameters . The above given program is an example of call by value. Call by Reference ( Passing by Reference) :- The call by reference method uses a different mechanism. In place of passing value to the function being called , a reference to the original variable is passed . This means that in call by reference method, the called function does not create its own copy of original values , rather, its refers to the original values only by different names i.e., reference . thus the called function works the original data and any changes are reflected to the original values. // passing parameters by reference include iostream.h void duplicate (int& a, int& b, int& c) a=2; b=2; c=2; void main () int x=1, y=3, z=7; duplicate (x, y, z); cout "x=" x ", y=" y ", z=" z; output :x=2, y=6, z=14 The ampersand (&) (address of) is specifies that their corresponding arguments are to be passed by reference instead of by value. Constant Arguments:-In C++ the value of constant argument cannot be changed by the function. To make an argument constant to a function , we can use the keyword const as shown below: int myFunction( const int x , const int b ); The qualifier const tell the compiler that the function should not modify the argument. The compiler will generate an error when this condition is violated. Default Arguments :- C++ allows us to assign default value(s) to a function’s parameter(s) which is useful in case a matching argument is not passed in the function call statement. The default values are specified at the time of function definition. e.g., float interest ( float principal, int time, float rate = 0.70f) Here if we call this function as: si_int= interest(5600,4); then rate =0.7 will be used in function. 25

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.