Sei sulla pagina 1di 6

Explanation Predefined functions are functions that are built into C++ Language to perform some standard operations.

The functions that are stand alone used for general purposes and that are not depended on any classes are stored in the Standard Function Library. Predefined functions in the Standard Function Library are listed based on different categories. absReturns the absolute value of the number. List of basic types of standard library functions:

I/O Functions. String and Character Functions Mathematical Functions Time,Date and Localization Functions. Dynamic Allocation Function Utility Functions

I/O Explanation Character based Input / Output (I/O) functions supported by C++ are based on its predecessor C. All the functions supported by the C++ Standard Function Library are termed as ANSI C I/O system. To use these predefined I/O functions in a C++ program, the header file <cstdio.h> should be included. Most of these functions set a built in global integer variable "errno" when an error is encountered. This variable stores the information about the error occured, so that it can be reffered by C++ programs. 2 Explanation Standard Function Library has a numerous set of string and character handling functions. The string function operate on the null-terminated arrays. The header file required for string function is "<cstring.h> and for character use the header file "<cctype>". Parameters passed to character functions are integers that are lower order bytes. These function convert the arguments to unsigned char, that will converted to integers at the time of call. The array overflow has to be taken care by the coders to prevent any issues with C++ program.

3. Standard Function Library has a numerous set of Mathematical Functions. The header file required for mathematical functions are "<cmath>" or "<math.h>". There are three macros used by Mathematical functions they are HUGE_VAL, EDOM, ERANGE that are declared using the header "<cerrno>" or the file "errno.h". In a function routine a large result or an overflow occurs it returns "HUGE_VAL" and the "errno" is set to "ERANGE" for a range error. When an underflow happens the function returns zero and sets the "errno" to "ERANGE". All the angles returned by cmath and math.h functions are in radians. Following table lists few Mathematical functions of C++ Standard function Library. This function operated on "double" datatype, but now C++ also supports "float" and "long double". 4 Explanation Standard Function Library defines several functions related to Date and Time. The header file required for the date and time functions is "<ctime>" or "<time.h>". This header file defines three time related types clock_t, time_t and tm. The first two types represent the system time and date as an integer value. But the structure type "tm" holds the date time values individually like second, minute etc. This "ctime" header also defines a macro "CLOCKS_PER_SEC" which is the number of ticks of the system clock per second.

5. Explanation Dynamic Allocation Function are used to allocate space or memory dynamically or at run time. The header file used by the dynamic allocation functions is "<cstdlib>". Different C++ compilers support these functions in different variants, to accommodate various options and environmental differences. Following table lists some basic dynamic memory allocation function to allocate space. 6. Explanation The standard function has several utility functions for number of conversions, variable-length, argument processing, sorting and searching, and random number generation. To use these function the header <cstdlib> is required. This header defines the types "div_t", "ldiv_t", "size_t". Following table lists the macros supported by <cstdlib> header. Following table lists macros supported by utility functions of C++ Standard function Library.

EXIT_SUCCESS Local variables are those that are declared inside a function. Only the statements that are within the block of the code can refer these variables. Example:

#include <iostream.h> int test() { int x; x = 200; return x; } int main( ) { int x; x = 100; cout << "Value of X in main is:" << x << '\n'; cout << "Value of X in Test is:" << test() << '\n'; return 0; }
Result: Value of X in main is:100 Value of X in Test is:200 In the above example the variable "x" is declared locally in both functions. So the values returned by the "x" depends on how it's assigned values in each individual functions. This is the local variables declaration in C++.

Explanation
A variable defines a location in the memory that holds a value which can be modified later. Syntax: type variable_list;

In the above syntax "type" refers to any one of the C++ data types. Declaring Variables: Variables in C++ should be declared before they are used in the code block. It is better to declare it right at the place where its used as this makes the programming much easier. This kind of declaration reduces the errors in the program. Example:

#include <iostream.h> void main() { int n = 10; cout << "Integer value is:"<< n << '\n'; }
Result: Integer value is: 10 In the above example "n" is an integer variable declared using return type "int" Variables can also be intialized dynamically at the place of declaration using the expression as below. Example: float area = 3.14159 * rad * rad In the above example the variable "area" is declared dynamically when it is used. Naming variables in C++: Always the first character of a variable must be a letter or an underscore. The variable name cannot start with a digit. Other characters should be either letters, digits, underscores. There is no limit for the length of variables. Declared keywords cannot be used as a variable name. Upper and lower case letters are distinct.

Explanation
Variables that are declared outside the scope of a function, so that it can be used anywhere in the program is known asGlobal Variables. Example:

#include <iostream.h> float pi = 3.14; int main( ) { int area,rad; rad = 100; area = pi*rad*rad; cout << "Area of a Circle:" << area << '\n'; return 0; }
Result: Area of a Circle: 31400 In the above example "pi" is declared as global variable outside any function. But the "pi" value is used inside the main function.

Explanation
Conditional operator is used to return a result based on a expression. This is the only operator that has three operands which also be used instead of "If else" statement for ease of use. Conditional operator is also known as "Ternary Operator". Syntax: Expr1? Expr2:Expr3; Example:

#include <iostream.h> using namespace std; void main() { int n = 10; int r = (n<100) ? 500 : 400; cout << "Value of r is ::" << r << '\n'; }
Result: Value of r is:: 500

In the above example the integer "r" is assigned a value based on the expression "n<100". Since "r" is true the first value "500" is assigned to the integer "r".

Explanation

The increment and decrement operator are used to increase or decrease the value of an operand by "1" or simply, to add or subtract integer variable. These operators can be used add or subtract an operand before the operation or after the operation, which is known as post increment or pre increment of integer variable and vice versa. Example:

#include <iostream.h> using namespace std; void main( ) { int i = 3, k,l, n=6,m; //Example for post and pre increment k = i++; l = ++i; cout << "Post Increment value for 3 is::" << k << endl; cout << "Pre Increment value for 4 is::" << l << endl; //Example for post and pre decrement m=--n; cout << "Pre Decrement value of 6 is::" << m << endl; for( int x = 1; x > 0;x-- ) { cout << "Value of x before Post Decrement::" << x << "\n"; x--; cout << "Value of x after Post Decrement::" << x << "\n"; } }
Result: Post Increment value for 3 is::4 Pre Increment value of 4 is::5 Pre Decrement value of 6 is::5 Value of x before Post Decrement::1 Value of x after Post Decrement::0 In the above example the difference is that k will be assigned 3 first then its post-incremented to become "4" . In the next scenario "k" already has the pre-incremented value of i that is "5".The pre-decrement operator is used to decrement the value of n even before assignment.Using the post-decrement operator the value of "x" is decremented after the assignment using a loop. Example:

#include <iostream.h> using namespace std; void main( ) { for( int x = 1; x > 0;x-- ) { cout << "Post decrement value of x is::" << x << "\n"; } }
Result: Post Decrement value of x is::5 Post Decrement value of x is::4 Post Decrement value of x is::3 Post Decrement value of x is::2 Post Decrement value of x is::1 In the the above example the given value is post-decremented after the assigned value of "5".

Assignment Operators that are used to assign the operator on the left the value on the right. The basic assigment operator is the "=" operator. Compound Assignment Operators: These operators are used modify the current value stored in a variable. Some of the compound assignment operators are +=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=. Example:

#include <iostream.h> int main( ) { int x = 13; int y = 6; int z = 3; x %= y; z += y; cout << "The current value of x::" << x << '\n' ; cout << "The current value of y::" << z << '\n' ; return 0; } Result: The current value of x::1 The current value of y::9 In the above example the first the modulus is calculated with "x%=y", which is equivalent to "x=x%y". Same way instead of using "z=z+y" the compound assigment operator "+=" is used.

Here is the syntax template of a value-returning function prototype:


returnDataType functionName(DataTypeOfParameterList);

Here is the syntax template of a value-returning function definition:


returnDataType functionName(DataTypeWithParameterList) { Statement . . . return value-returning-expression; }

With void functions, we use the function name as a statement in our program to execute the actions that function performs. With value-returning functions, the actions result in the return of a value, and that value can be used in an expression. For example, let's write a function that returns the smallest of three input values.

Example
// Program PrintMin prints the smallest of three input values. #include <iostream> using namespace std; int Minimum(int, int, int); // function prototype // Post: Minimum returns the minimum of three distinct values. int main() { int one, two, three; int MIN; cout << "Input three integer values. Press return." << endl; cin >> one >> two >> three; MIN = Minimum(one, two, three); // function call cout << "The minimum value of the three numbers is " << MIN << endl; return 0; } //************************************************************ //Function definition with 3 parameters //************************************************************ int Minimum(int first, int second, int third) // Post: Returns minimum of three distinct int values. { if (first <= second && first < third) return first; else if (second <= first && second < third) return second; else return third; }

The function prototype declares a function of data type int. This means that the value returned to the calling function is of type int. Because a value-returning function always sends one value back to the calling function, we designate the type of the value before the name of the function. We call this data type the function return data type or function type. In the above example, the function invocation/call occurs in the output statement of the main function. The Minimum function is invoked/called and the returned value is immediately sent to the output stream. There are three parameters first, second and third. The three arguments sent from main are one, two and three. Note: in the function prototype, only the datatypes of the parameter list are needed. In the function heading (or function definition), however, both the parameters and their datatype are necessary.

Potrebbero piacerti anche