Functions 2.pptx
Document Details
Uploaded by GutsyFibonacci
Tags
Full Transcript
SLIDE 1 Functions in C++ By Dr. A. AMEER RASHED KHAN, M.Sc., M.Sc.,(Psyho) M.Phil., PhD., Assistant Processor, Department of Computer Application FUNCTION PROTOTYPE A function prototype specifies the function's name, data typ...
SLIDE 1 Functions in C++ By Dr. A. AMEER RASHED KHAN, M.Sc., M.Sc.,(Psyho) M.Phil., PhD., Assistant Processor, Department of Computer Application FUNCTION PROTOTYPE A function prototype specifies the function's name, data type of return value, parameters and all other information without the function body. It allows the compiler to ensure that function calls match the declared signature. It provides all information that the C++ compiler needs to know to translate calls to the function correctly. INLINE FUNCTION Inline Function is a Inbuilt Function in C++. An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. using inline functions can make your program faster because they eliminate the overhead associated with function calls. The only difference between a normal C++ function and an inline C++ function is that the inline C++ function will not involve a stack call. It is declared with the keyword inline. CALL BY VALUE & CALL BY REFERENCE Call by Value passes a copy of the variable. Call by Reference passes the variable itself. In Call by Value, the actual arguments and passed parameters of a function refer to different memory locations. In Call by Reference, the actual arguments and passed parameters of a function refer to the same memory location. DIFFERENCE BETWEEN THE CALL BY VALUE AND CALL BY REFERENCE Feature Call by Value Call by Reference Value Passed In this method, the value of the variable is passed to In call by reference memory address of the function. the variable is passed. Scope of In this method, the original value remains unchanged In this method, the changes made are Changes even when we make changes in the function. reflected in the original variable. Performance It may require extra memory and time to copy so less It is more memory and time efficient as efficient. compared to “call by value”. Memory The memory addresses of the actual and formal The actual and the formal parameters Location parameters are different. point at the same memory address. Applications Mainly used to pass values for small data or when we It is used when we want to modify the do not want to change original values. original value or save resources. DEFAULT ARGUMENTS A default argument is a value that is automatically used by the function if no argument is provided for that parameter during the function call. In case any value is passed, the default value is overridden. The values passed in the default arguments are not constant. These values can be overwritten if the value is passed to the function. If not, the previously declared value retains. During the calling of function, the values are copied from left to right. All the values that will be given default value will be on the right. DEFAULT ARGUMENTS CONSTANT ARGUMENT In C++, constant arguments in a function indicate that the values passed to the function as parameters cannot be modified inside the function. A constant argument is the one whose modification cannot take place by the function. Furthermore, in order to make an argument constant to a function, the use of a keyword const can take place like- int sum (const int a, const int b). It shows the compile-time error because we update the assigned value of the num 25 by 10. FUNCTION OVERLOADING Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. In Function Overloading “Function” name should be the same and the arguments should be different. Function overloading can be considered as an example of a polymorphism feature in C++. If multiple functions having same name but parameters of the functions should be different is known as Function Overloading. FUNCTION OVERLOADING If we have to perform only one operation and having same name of the functions increases the readability of the program. Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the function such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you to understand the behavior of the function because its name differs. The parameters should follow any one or more than one of the following conditions for Function overloading: Parameters should have a different type Parameters should have a different number ADVANTAGES AND DISADVANTAGES OF FUNCTION OVERLOADING The main advantage of function overloading is that it improves code readability and allows code reusability. The use of function overloading is to save memory space, consistency, and readability. It speeds up the execution of the program Code maintenance also becomes easy. Function overloading brings flexibility to code. The function can perform different operations and hence it eliminates the use of different function names for the same kind of operations. Function declarations that differ only in the return type cannot be overloaded is the disadvantage of function overloading FRIEND FUNCTIONS A friend function is a special function in C++ that in spite of not being a member function of a class has the privilege to access the private and protected data of a class. A friend function is a non-member function or ordinary function of a class, which is declared as a friend using the keyword “friend” inside the class. By declaring a function as a friend, all the access permissions are given to the function. The keyword “friend” is placed only in the function declaration of the friend function and not in the function definition or call. A friend function is called like an ordinary function. It cannot be called using the object name and dot operator. However, it may accept the object as an argument whose value it wants to access. A friend function can be declared in any section of the class i.e. public or private or protected. ADVANTAGES/ DISADVANTAGES OF FRIEND FUNCTIONS A friend function is able to access members without the need of inheriting the class. The friend function acts as a bridge between two classes by accessing their private data. It can be used to increase the versatility of overloaded operators. It can be declared either in the public or private or protected part of the class. ------------------------------------------------------------------------------------------------ Friend functions have access to private members of a class from outside the class which violates the law of data hiding. Friend functions cannot do any run-time polymorphism in their members.