Array: It Is a Derived Data Type s1
Total Page:16
File Type:pdf, Size:1020Kb
Function- It groups a number of program statements into a single unit and gives it a name. This unit can be invoked from other parts of the program. Functions are the main building blocks of C++ programs. By separating and coding parts of your program in separate module.
FUNCTION Use: Making the program modular and compact(reduction in program size) and avoid ambiguity.
FUNCTION Prototype: It is a declaration that tells the program about type of the return value of the function and the number and type of each arguments. It is also known as FUNCTION declaration. FUNCTION Prototype are required for those functions which are intended to be called be fore they are defined. It helps compiler in static type checking whether the FUNCTION call is proper or not.
FUNCTION call: A function can be called from another function by using its name. The FUNCTION name must be followed by a set of actual parameters, enclosed in parenthesis separated by commas.
FUNCTION Definition: A FUNCTION declaration plus the body of the function(statements contained in it) make up the definition of the FUNCTION
Formal parameters/variable: It is a list of variables that will accept different types of values being supplied to the function by the calling program. They are declared after the FUNCTION name and inside parenthesis i.e. the parameters that appear in FUNCTION definition.
Actual variables/parameter: The variables associated with function name during FUNCTION call are called actual variables i..e parameters that appear in FUNCTION call.
Pass By Value: When an argument (local variable) is passed by value, a copy of the variable’s value is assigned to the receiving functions parameter. i.e copy of actual are made in formal and if formal parameters are changed the changes will not be reflected back in actual parameters.
Pass By Reference: When you pass an argument by reference or by address, the variables address is assigned to the receiving function’s parameter, if the receiving function changes the variable, it is also changed in the calling function i.e changes made in formal parameters are reflected back in actual parameters(Arrays are always passed by reference).
Use of return- 1)It is a jump statement 2) It transfers the control back to the calling function 3) If you want to return a value from a function to its calling function, put the return value after return statement.4) There can be as many return statements as you want but at a time one will be executed 5) return can return only one value.
Scope Rules of Variables: The scope of a variable means how the different parts of the program can access that variable. There are four types of variables:
1. LOCAL Variable: The variables which are declared inside any function are called local variable. Its scope is then limited to the function itself. The variable is said to be accessible or visible within the function only and has a local scope. E.g. void main(){ int b; // b is local variable for main()} Thus variables that are declared inside an opening and terminating curly brace are termed as local variables.
2. GLOBAL Variable: The variables which are declared globally (before main() or outside of any function in C+ +) are called global variable. The usual place of declaration is in the beginning of the program file i.e. before all function definitions so that it is visible to all functions and exists till the program terminated. e.g. #include Input-Output Functions C supports 3 main categories of I/O functions: stream, console(it comprises of both keyboard-std. Input device and VDU-std. Output device) and low level. We have already used 2 C++ iostream functions cout and cin. Include By default all stream I/Os are buffered. Buffered I/O: Whenever we read from the keyboard, a block of data is stored in a buffer called input buffer. The input stream, therefore, reads from the buffer with the help of a pointer until the input buffer is empty. As soon as the buffer becomes empty, the next block of data is transferred into the buffer from I/O device. Similarly, data written by an output stream pointer is not directly written onto the device, but into an output buffer. As soon as the output buffer becomes full, a block of data is written on to the I/O device making the output buffer empty. Input Buffer Block of data Data Stream I/O Devices Pointers Output Buffer Block of data Data Recursion- In C++ a function can invoke itself, this is called recursion. A function is said to be recursive if there exists a statement in its body for the function call itself. We must take care that there exists a reachable termination condition inside the function so that function may not be invoked endlessly. E g. int fact(int num){if(num= =0) return 1; else return (num * fact(num-1));} Default Arguments- C++ allows a function to assign a parameter the default value in case no argument for that parameter is specified in the function call. E.g. void pattern(char M, int B=2){for(int I=0;I It initializes the random number generator with a random number. random(int x)- 5. Selection Sort(in ascending 6. Insertion Sort: (in ascending order) 7. Inserting an element NUM at order) Kth position Note: the size of the for(i=0;i