{"id":72,"date":"2018-07-10T04:44:02","date_gmt":"2018-07-10T04:44:02","guid":{"rendered":"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=72"},"modified":"2019-05-14T11:46:02","modified_gmt":"2019-05-14T11:46:02","slug":"functions-in-c-ii","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/chapter\/functions-in-c-ii\/","title":{"rendered":"Functions in C++ II"},"content":{"raw":"\r\n<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/qDOnc-2h_50\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n\r\nIn earlier section, we have seen declaration and definition of the function. Declaration of a function includes stating to a compiler that the declared function is defined in the program.\r\n\r\n&nbsp;\r\n\r\n<strong>Function Prototyping and its Definition<\/strong>\r\n\r\n&nbsp;\r\n\r\nDeclaring a function is known as function prototyping. In C++, function prototyping is mandatory unlike C programs. Compiler error would be raised if a function is called without its prototype mentioned in the program before calling of the function. This is because In C, functions are identified only by their name. And in C++, a function\u2019s signature includes its name, parameters, and (for member functions) const. It does NOT include the return type. A function signature is what the compiler and linker use to identify a function.\r\n\r\n&nbsp;\r\n\r\nIf a programmer writes a function to add two integers, It can be named as add and prototype of which can be written as <strong>int add(int , int)<\/strong> if returns integer. A function prototype is useful while checking whether the function when called is called with correct set of arguments. Definition of function includes instructions to be executed whenever function is called. Instructions written within a function may make use of arguments supplied through calling function.\r\n\r\n&nbsp;\r\n\r\n<strong>Function Overloading<\/strong>\r\n\r\n&nbsp;\r\n\r\nAssume a programmer intends to add two floating point numbers or one integer and one float, and a function is called using 4 and 67.5 as arguments, the value 67.5 would be truncated to integer. Function with name add can also be intended to concatenate two strings where string1 and string2 are arguments and its concatenation shall be returned. Compiler would raise error if string is passed in the add function with prototype int add(int, int) and neither it would return string.\r\n\r\n&nbsp;\r\n\r\nFunction overloading can be defined as a mechanism which allows programmer to define function with same name but different types of arguments or number of arguments.\r\n\r\n&nbsp;\r\n\r\nThus in the case explained above three different functions can be written whose prototypes and definitions would be different but their name would remain same.\r\n\r\n&nbsp;\r\n\r\nIn C++ it is possible to define more than one function with the same name in the same scope. Hence in C++ it is possible to define following three functions in same scope.\r\n\r\n&nbsp;\r\n\r\nint Add(int x, int y);\r\n\r\n&nbsp;\r\n\r\nint Add(int x, int y, int z);\r\n\r\n&nbsp;\r\n\r\nfloat\u00a0 Add(float x, float y);\r\n\r\n&nbsp;\r\n\r\nThis phenomena of defining more than one function with the same name is called function overloading.\r\n\r\n&nbsp;\r\n\r\nWhen you call an overloaded function, the compiler determines the most appropriate definition to use by comparing the argument types you used to call the function with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution.\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-73 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-15.png\" alt=\"\" width=\"1422\" height=\"1523\" \/>\r\n\r\n&nbsp;\r\n\r\nBut Compiler will allow function overloading only when, it does not create any ambiguity and compiler can clearly do overload resolution. For example we cannot have following functions\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" style=\"height: 252px\" border=\"1\" width=\"679\">\r\n<tbody>\r\n<tr style=\"height: 28px\">\r\n<td style=\"width: 208px;height: 28px\">int<\/td>\r\n<td style=\"width: 427px;height: 28px\">dowork(int x, int y);<\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"width: 208px;height: 28px\">double dowork(int x, int y);<\/td>\r\n<td style=\"width: 427px\"><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"width: 208px;height: 28px\">Because<\/td>\r\n<td style=\"width: 427px;height: 28px\"><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"width: 208px;height: 28px\">int<\/td>\r\n<td style=\"width: 427px;height: 28px\">input1,input2;<\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"width: 208px;height: 28px\">dowork(input1,input2);<\/td>\r\n<td style=\"width: 427px;height: 28px\">\/\/ call can not determine which function is to be called<\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"width: 208px;height: 28px\"><\/td>\r\n<td style=\"width: 427px;height: 28px\"><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\nAlso following program too will create ambiguity for compiler and therefore will not successfully compile.\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-74\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-16.png\" alt=\"\" width=\"1516\" height=\"1523\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Default Arguments<\/strong>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nFunctions in C++ can have default arguments. That is we can provide default values for some arguments. For Example\r\n\r\n&nbsp;\r\n\r\nVoid Demo(int data, int value=10)\r\n\r\n{\r\n\r\n\u2026\u2026\u2026\u2026.\r\n\r\n&nbsp;\r\n\r\n\u2026\u2026\u2026\u2026.\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nAt the Time of calling we need not necessarily provide value for that argument. We can also call above function as follows\r\n\r\n&nbsp;\r\n\r\nDemo(20);\r\n\r\n&nbsp;\r\n\r\nDefault arguments are useful in situation where a arguments has the same value in most function calls.\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-76\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-18.png\" alt=\"\" width=\"1494\" height=\"1063\" \/>\r\n\r\nWhen the desired value is different from the default value,\r\n\r\n&nbsp;\r\n\r\nthe desired value can be specified explicitly in a function call as shown in the following program. This changes the default value of the argument to be overridden for the duration of function call. If in the function call all the arguments are not specified then the omitted arguments can take default value by providing them in the function declaration<img class=\"alignnone size-full wp-image-77\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-19.png\" alt=\"\" width=\"1567\" height=\"1260\" \/>\r\n\r\nDefault arguments remove the need of passing arguments, but they reduce readability. Therefor they should be used with caution.\r\n\r\n&nbsp;\r\n\r\n<strong>Making static variable default<\/strong>\r\n\r\n&nbsp;\r\n\r\nWe can also make static variable default; an interesting use of making static variable default can be seen as follows\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-78\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-20.png\" alt=\"\" width=\"1281\" height=\"910\" \/>\r\n\r\n<strong>Friend Functions<\/strong>\r\n\r\n&nbsp;\r\n\r\nData hiding is one of the important concepts of Object Oriented Programming. It says that a nonmember function cannot access an object's private or protected data. C++ however provides programmer flexibility to access object\u2019s private or protected data though a nonmember function, if that function is declared as friend of class. The complier understands that a given function is a friend function preceded by its keyword friend. The declaration of friend function should be made inside the body of class starting with keyword friend. The definition of function is similar to normal function. No keywords in used in function definition of friend function.\r\n\r\n&nbsp;\r\n\r\nFollowing Program illustrates use of friend function. A class named Room with length and breadth as its private data members and a constructor is defined. A non-member function totalTiles is declared within a class preceded by word friend. Argument of friend function totalTiles is object of type Room. The definition of totalTiles is defined outside the class where it is expected to calculate number of tiles of specific area required in a room. As friend function is capable of accessing private data members of class Room, it can calculate area of room and can return total number of tiles required for flooring.\r\n\r\n\/*Program : C++ program to demonstrate the working of friend function.*\/\r\n\r\n&nbsp;\r\n\r\n#include &lt;iostream&gt;\r\n\r\n#include&lt;cmath&gt;\r\n\r\n&nbsp;\r\n\r\nusing namespace std;\r\n\r\n&nbsp;\r\n\r\nclass Room\r\n\r\n{\r\n\r\nprivate:\r\n\r\n&nbsp;\r\n\r\nint length;\r\n\r\nint breadth;\r\n\r\npublic:\r\n\r\nRoom(int l, int b){ length = l; breadth = b; }\r\n\r\nfriend int totalTiles(Room);\u00a0 \/\/friend function\r\n\r\n&nbsp;\r\n\r\n};\r\n\r\nint totalTiles(Room a)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/function definition\r\n\r\n{\r\n\r\nint aTile;\r\n\r\n&nbsp;\r\n\r\ncout&lt;&lt;endl&lt;&lt; \"Enter area of Tile in square meters: \"; cin&gt;&gt; aTile;\r\n\r\n&nbsp;\r\n\r\n\/\/accessing private data from non-member function\r\n\r\n&nbsp;\r\n\r\nint areaRect= a. length * a. breadth;\r\n\r\nint nTiles = ceil(areaRect \/ aTile);\r\n\r\n&nbsp;\r\n\r\nreturn nTiles;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nint main()\r\n\r\n{\r\n\r\nRoom r(10,20);\r\n\r\n&nbsp;\r\n\r\ncout&lt;&lt;endl&lt;&lt; \" Tiles Required: \"&lt;&lt;totalTiles(r);\r\n\r\nreturn 0;\r\n\r\n}\r\n\r\n<img class=\"alignnone size-full wp-image-79\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-21.png\" alt=\"\" width=\"677\" height=\"99\" \/>\r\n\r\nExecution of Program\r\n\r\n&nbsp;\r\n\r\nThe above program gives us an idea about the concept of friend function. Logical usage of friend functions can solve complex problems.\r\n\r\n&nbsp;\r\n\r\nSuppose, programmer needs to operate on objects of two different classes then, friend function can be very helpful. There are ways to write a program without friend functions but programs\u00a0 would be longer, complex and hard to understand in that case. Following program illustrates utility of friend function in function overloading. Here we intends to find out number of tiles required for flooring a room defined through a class named Room, however tiles could be either square or rectangle. Two different classes exists for Square tiles and Rectangle tiles. A function named totalTiles is defined which accepts object of Square tiles and object of Room. totalTiles function would return number of tiles required for flooring. totalTiles function is overloaded with different set of arguments. Overloaded function totalTile accepts two arguments, first is object of class rectangleTile and other is object of room. Complier would accept overloading of totalTiles as object type is different in both the cases.\r\n\r\n&nbsp;\r\n\r\n\/* Program : C++ program to operate on Objects of two Different class using friend Function*\/\r\n\r\n&nbsp;\r\n\r\n#include &lt;iostream&gt;\r\n\r\n&nbsp;\r\n\r\n#include&lt;cmath&gt;\r\n\r\n&nbsp;\r\n\r\nusing namespace std;\r\n\r\n&nbsp;\r\n\r\n\/* Forward declaration of class squareTile and rectangleTile *\/\r\n\r\n&nbsp;\r\n\r\nclass squareTile;\r\n\r\nclass rectangleTile;\r\n\r\n&nbsp;\r\n\r\nclass Room\r\n\r\n{\r\n\r\nprivate:\r\n\r\nint length;\r\n\r\nint breadth;\r\n\r\n&nbsp;\r\n\r\npublic:\r\n\r\nRoom(int l, int b){ length = l; breadth = b; }\r\n\r\nint getroomArea() { return ( length * breadth); }\r\n\r\n&nbsp;\r\n\r\nfriend int totalTiles(squareTile sT, Room r); \/\/friend function friend int totalTiles(rectangleTile rT, Room r); \/\/friend function\r\n\r\n};\r\n\r\nclass squareTile\r\n\r\n{\r\n\r\nprivate:\r\n\r\nint length;\r\n\r\npublic:\r\n\r\nsquareTile(int l){length=l;}\r\n\r\nfriend int totalTiles(squareTile sT, Room r); \/\/friend function\r\n\r\n};\r\n\r\nclass rectangleTile\r\n\r\n{\r\n\r\nprivate:\r\n\r\nint length, breadth;\r\n\r\npublic:\r\n<div>\r\n\r\nrectangleTile(int l, int b){length=l;breadth = b;}\r\n\r\n&nbsp;\r\n\r\nfriend int totalTiles(rectangleTile rT, Room r); \/\/friend function\r\n\r\n};\r\n\r\n<\/div>\r\n<div>\r\n\r\nint totalTiles(rectangleTile rT, Room r) {\r\n\r\n\/\/function definition\r\n\r\n<\/div>\r\n<div>\r\n\r\nint areaRoom = r.getroomArea(); int areaTile=rT.length * rT.breadth; int nTiles = ceil(areaRoom \/ areaTile); return (nTiles);\r\n\r\n\/\/accessing private data from non-member function\r\n\r\n<\/div>\r\n<div>\r\n\r\n}\r\n\r\n<\/div>\r\n<div>\r\n\r\nint totalTiles(squareTile sT, Room r) {\r\n\r\n\/\/function definition\r\n\r\n<\/div>\r\n<div>\r\n\r\nint areaRoom = r.getroomArea();\r\n\r\nint areaTile=sT.length * sT.length;\r\n\r\nint nTiles = ceil(areaRoom \/ areaTile);\r\n\r\nreturn nTiles;\r\n\r\n\/\/accessing private data from non-member function\r\n\r\n<\/div>\r\n<div>\r\n\r\n}\r\n\r\nint main()\r\n\r\n{\r\n\r\nRoom r(50,20);\r\n\r\nrectangleTile rT(2,3);\r\n\r\nsquareTile sT(2);\r\n\r\n<\/div>\r\ncout&lt;&lt;endl&lt;&lt;\" Area of room is \" &lt;&lt; r.getroomArea(); cout&lt;&lt;endl&lt;&lt; \" Rectangle Tiles Required is \" &lt;&lt; totalTiles(rT, r);\r\n\r\ncout&lt;&lt;endl&lt;&lt; \" Square Tiles Required is \" &lt;&lt; totalTiles(sT, r); return 0;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nAbove program includes forward declaration of class squareTile and rectangleTile is to be made. It is because class Room is referencing object of class squareTile and rectangleTile while declaring following two friend functions.\r\n\r\n&nbsp;\r\n\r\n<strong>friend int totalTiles(squareTile sT, Room r);<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>friend int totalTiles(rectangleTile rT, Room r);<\/strong>\r\n\r\n&nbsp;\r\n\r\nExecution of program can be seen below\r\n\r\n<img class=\"alignnone size-full wp-image-81\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-23.png\" alt=\"\" width=\"677\" height=\"120\" \/>\r\n<p style=\"text-align: left\">Execution of program : demonstrating utility of friend function.<\/p>\r\n&nbsp;\r\n\r\n<strong>Properties of Friend function<\/strong>\r\n\r\n&nbsp;\r\n\r\nAs friend functions come into the category of special functions in C++, it is advantageous to understand scope of functionalities of friend function. Following are the properties of Friend Function.\r\n\r\n&nbsp;\r\n<ul>\r\n \t<li>Friend function of the class can be member of some other class.<\/li>\r\n \t<li>Friend function of one class can be friend of all the classes in one program, such a friend is referred as Global Friend.<\/li>\r\n \t<li>Friend can access the private or protected members of the class in which they are declared Friends are non-member functions and hence do not have \u201cthis\u201d pointer pointing to themselves.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\nFriend can be declared anywhere (in public, protected or private section) in the class.\r\n\r\n&nbsp;\r\n\r\n<strong>Pointer to Function<\/strong>\r\n\r\n&nbsp;\r\n\r\nWe can define pointer to function as follows\r\n\r\n&nbsp;\r\n\r\nReturn Type (*functionName) ( dataType arg1, dataType arg2, dataType arg3, \u2026\u2026\u2026\u2026..);\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nFor example we can define pointer to an integer function, which takes two parameter, int and float respectively as follows :\r\n\r\n&nbsp;\r\n\r\nint\u00a0\u00a0\u00a0 (*function_ptr) ( int\u00a0 x, float y);\r\n\r\nIf we have two functions\r\n\r\nnt\u00a0 doWork(int data1, float data2);\r\n\r\nint compute( int value1, float value2);\r\n\r\n&nbsp;\r\n\r\nWe can initialize function_ptr with any of the two functions\r\n\r\nfunction_ptr = doWork;\r\n\r\nfunction_ptr = compute;\r\n\r\n&nbsp;\r\n\r\n<strong>Pointer to Member Function<\/strong>\r\n\r\n&nbsp;\r\n\r\nWe can also define pointer to member functions as follows Time t;\r\n\r\nvoid\u00a0\u00a0 (Time :: *fptr)();\r\n\r\n&nbsp;\r\n\r\nHere fptr is defined as pointer to any void member function without parameter of class Time. It can be made to point to any member function of class Time which does not take any argument and has void as a return type. We can make it to point to readTime() and displayTime() member functions of class Time as follows, as both are void functions with no arguments\r\n\r\n&nbsp;\r\n\r\nfptr =\u00a0 &amp;Time :: readTime();\r\n\r\nfptr =\u00a0 &amp;Time :: displayTime();\r\n\r\n&nbsp;\r\n\r\nMember function can be called though pointer as follows t.(*fptr)();\r\n\r\n&nbsp;\r\n\r\nHere if fptr is pointing to readTime(), this call will result into invokation of readTime() function on object <strong>t<\/strong>. If fptr is pointing to displayTime(), this call will result into invokation of displayTime() function on object <strong>t.<\/strong>\r\n\r\n&nbsp;\r\n\r\nWe can also initialize pointer to member function at the time of declaration as follows Time t;\r\n\r\nvoid\u00a0\u00a0 (Time :: *fptr)() = &amp;Time :: readTime()\r\n\r\n&nbsp;\r\n\r\n<strong>Functions are powerful tools for creating modular design of program which is readable, effective , easy to modify, test, debug, and maintain\u2026\u2026..<\/strong>\r\n\r\n&nbsp;\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Functions in C++ II<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/qDOnc-2h_50\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\n<strong>Additional References<\/strong>\r\n\r\n&nbsp;\r\n\r\n1) Stanley Lippmann, \u201cC++ Primer\u201d, Pearson Education.\r\n\r\n2) Bjarne Stroustrup, \u201cThe C++ Programming Language\u201d, Pearson Education.\r\n\r\n3) Scott Mayer, \u201cEffective C++\u201d, Addison Wesley.\r\n\r\n4) Bhushan Trivedi , Programming with ANSI C++, 2\/e , Oxford University Press.\r\n\r\n5) Yashavant P. Kanetkar \u201cLet Us C++\u201d , Bpb Publications.\r\n\r\n6) Abhiram G. Ranade, \u201cAn Introduction to Programming through C++\u201d , McGraw Hill\r\n\r\n7) Ellis and B. Stroustrup \u201cAnnotated C++ Reference Manual\u201d, http:\/\/www.stroustrup.com\/arm.html\r\n\r\n8) Herbert Schildt, \u201cComplete Reference C++\u201d, McGraw Hill Publications.\r\n\r\n9) Ashok Kamthane, \u201cObject Oriented Programming with ANSI and Turbo C++\u201d, Pearson Education\r\n\r\n10) E Balaguruswami, \u201cObject Oriented Programming With C++\u201d, Tata McGraw Hill\r\n\r\n11) \u201cC++ FAQs\u201d, Pearson Education.\r\n\r\n&nbsp;","rendered":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/qDOnc-2h_50\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<p>In earlier section, we have seen declaration and definition of the function. Declaration of a function includes stating to a compiler that the declared function is defined in the program.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Function Prototyping and its Definition<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Declaring a function is known as function prototyping. In C++, function prototyping is mandatory unlike C programs. Compiler error would be raised if a function is called without its prototype mentioned in the program before calling of the function. This is because In C, functions are identified only by their name. And in C++, a function\u2019s signature includes its name, parameters, and (for member functions) const. It does NOT include the return type. A function signature is what the compiler and linker use to identify a function.<\/p>\n<p>&nbsp;<\/p>\n<p>If a programmer writes a function to add two integers, It can be named as add and prototype of which can be written as <strong>int add(int , int)<\/strong> if returns integer. A function prototype is useful while checking whether the function when called is called with correct set of arguments. Definition of function includes instructions to be executed whenever function is called. Instructions written within a function may make use of arguments supplied through calling function.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Function Overloading<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Assume a programmer intends to add two floating point numbers or one integer and one float, and a function is called using 4 and 67.5 as arguments, the value 67.5 would be truncated to integer. Function with name add can also be intended to concatenate two strings where string1 and string2 are arguments and its concatenation shall be returned. Compiler would raise error if string is passed in the add function with prototype int add(int, int) and neither it would return string.<\/p>\n<p>&nbsp;<\/p>\n<p>Function overloading can be defined as a mechanism which allows programmer to define function with same name but different types of arguments or number of arguments.<\/p>\n<p>&nbsp;<\/p>\n<p>Thus in the case explained above three different functions can be written whose prototypes and definitions would be different but their name would remain same.<\/p>\n<p>&nbsp;<\/p>\n<p>In C++ it is possible to define more than one function with the same name in the same scope. Hence in C++ it is possible to define following three functions in same scope.<\/p>\n<p>&nbsp;<\/p>\n<p>int Add(int x, int y);<\/p>\n<p>&nbsp;<\/p>\n<p>int Add(int x, int y, int z);<\/p>\n<p>&nbsp;<\/p>\n<p>float\u00a0 Add(float x, float y);<\/p>\n<p>&nbsp;<\/p>\n<p>This phenomena of defining more than one function with the same name is called function overloading.<\/p>\n<p>&nbsp;<\/p>\n<p>When you call an overloaded function, the compiler determines the most appropriate definition to use by comparing the argument types you used to call the function with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-73 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-15.png\" alt=\"\" width=\"1422\" height=\"1523\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-15.png 1422w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-15-280x300.png 280w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-15-768x823.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-15-956x1024.png 956w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-15-65x70.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-15-225x241.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-15-350x375.png 350w\" sizes=\"auto, (max-width: 1422px) 100vw, 1422px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>But Compiler will allow function overloading only when, it does not create any ambiguity and compiler can clearly do overload resolution. For example we cannot have following functions<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\" style=\"height: 252px; width: 679px;\">\n<tbody>\n<tr style=\"height: 28px\">\n<td style=\"width: 208px;height: 28px\">int<\/td>\n<td style=\"width: 427px;height: 28px\">dowork(int x, int y);<\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"width: 208px;height: 28px\">double dowork(int x, int y);<\/td>\n<td style=\"width: 427px\"><\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"width: 208px;height: 28px\">Because<\/td>\n<td style=\"width: 427px;height: 28px\"><\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"width: 208px;height: 28px\">int<\/td>\n<td style=\"width: 427px;height: 28px\">input1,input2;<\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"width: 208px;height: 28px\">dowork(input1,input2);<\/td>\n<td style=\"width: 427px;height: 28px\">\/\/ call can not determine which function is to be called<\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"width: 208px;height: 28px\"><\/td>\n<td style=\"width: 427px;height: 28px\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>Also following program too will create ambiguity for compiler and therefore will not successfully compile.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-74\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-16.png\" alt=\"\" width=\"1516\" height=\"1523\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-16.png 1516w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-16-150x150.png 150w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-16-300x300.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-16-768x772.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-16-1019x1024.png 1019w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-16-65x65.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-16-225x226.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-16-350x352.png 350w\" sizes=\"auto, (max-width: 1516px) 100vw, 1516px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Default Arguments<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Functions in C++ can have default arguments. That is we can provide default values for some arguments. For Example<\/p>\n<p>&nbsp;<\/p>\n<p>Void Demo(int data, int value=10)<\/p>\n<p>{<\/p>\n<p>\u2026\u2026\u2026\u2026.<\/p>\n<p>&nbsp;<\/p>\n<p>\u2026\u2026\u2026\u2026.<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>At the Time of calling we need not necessarily provide value for that argument. We can also call above function as follows<\/p>\n<p>&nbsp;<\/p>\n<p>Demo(20);<\/p>\n<p>&nbsp;<\/p>\n<p>Default arguments are useful in situation where a arguments has the same value in most function calls.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-76\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-18.png\" alt=\"\" width=\"1494\" height=\"1063\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-18.png 1494w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-18-300x213.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-18-768x546.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-18-1024x729.png 1024w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-18-65x46.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-18-225x160.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-18-350x249.png 350w\" sizes=\"auto, (max-width: 1494px) 100vw, 1494px\" \/><\/p>\n<p>When the desired value is different from the default value,<\/p>\n<p>&nbsp;<\/p>\n<p>the desired value can be specified explicitly in a function call as shown in the following program. This changes the default value of the argument to be overridden for the duration of function call. If in the function call all the arguments are not specified then the omitted arguments can take default value by providing them in the function declaration<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-77\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-19.png\" alt=\"\" width=\"1567\" height=\"1260\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-19.png 1567w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-19-300x241.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-19-768x618.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-19-1024x823.png 1024w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-19-65x52.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-19-225x181.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-19-350x281.png 350w\" sizes=\"auto, (max-width: 1567px) 100vw, 1567px\" \/><\/p>\n<p>Default arguments remove the need of passing arguments, but they reduce readability. Therefor they should be used with caution.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Making static variable default<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>We can also make static variable default; an interesting use of making static variable default can be seen as follows<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-78\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-20.png\" alt=\"\" width=\"1281\" height=\"910\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-20.png 1281w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-20-300x213.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-20-768x546.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-20-1024x727.png 1024w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-20-65x46.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-20-225x160.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-20-350x249.png 350w\" sizes=\"auto, (max-width: 1281px) 100vw, 1281px\" \/><\/p>\n<p><strong>Friend Functions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Data hiding is one of the important concepts of Object Oriented Programming. It says that a nonmember function cannot access an object&#8217;s private or protected data. C++ however provides programmer flexibility to access object\u2019s private or protected data though a nonmember function, if that function is declared as friend of class. The complier understands that a given function is a friend function preceded by its keyword friend. The declaration of friend function should be made inside the body of class starting with keyword friend. The definition of function is similar to normal function. No keywords in used in function definition of friend function.<\/p>\n<p>&nbsp;<\/p>\n<p>Following Program illustrates use of friend function. A class named Room with length and breadth as its private data members and a constructor is defined. A non-member function totalTiles is declared within a class preceded by word friend. Argument of friend function totalTiles is object of type Room. The definition of totalTiles is defined outside the class where it is expected to calculate number of tiles of specific area required in a room. As friend function is capable of accessing private data members of class Room, it can calculate area of room and can return total number of tiles required for flooring.<\/p>\n<p>\/*Program : C++ program to demonstrate the working of friend function.*\/<\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;iostream&gt;<\/p>\n<p>#include&lt;cmath&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>using namespace std;<\/p>\n<p>&nbsp;<\/p>\n<p>class Room<\/p>\n<p>{<\/p>\n<p>private:<\/p>\n<p>&nbsp;<\/p>\n<p>int length;<\/p>\n<p>int breadth;<\/p>\n<p>public:<\/p>\n<p>Room(int l, int b){ length = l; breadth = b; }<\/p>\n<p>friend int totalTiles(Room);\u00a0 \/\/friend function<\/p>\n<p>&nbsp;<\/p>\n<p>};<\/p>\n<p>int totalTiles(Room a)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/function definition<\/p>\n<p>{<\/p>\n<p>int aTile;<\/p>\n<p>&nbsp;<\/p>\n<p>cout&lt;&lt;endl&lt;&lt; &#8220;Enter area of Tile in square meters: &#8220;; cin&gt;&gt; aTile;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/accessing private data from non-member function<\/p>\n<p>&nbsp;<\/p>\n<p>int areaRect= a. length * a. breadth;<\/p>\n<p>int nTiles = ceil(areaRect \/ aTile);<\/p>\n<p>&nbsp;<\/p>\n<p>return nTiles;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>int main()<\/p>\n<p>{<\/p>\n<p>Room r(10,20);<\/p>\n<p>&nbsp;<\/p>\n<p>cout&lt;&lt;endl&lt;&lt; &#8221; Tiles Required: &#8220;&lt;&lt;totalTiles(r);<\/p>\n<p>return 0;<\/p>\n<p>}<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-79\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-21.png\" alt=\"\" width=\"677\" height=\"99\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-21.png 677w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-21-300x44.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-21-65x10.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-21-225x33.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-21-350x51.png 350w\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" \/><\/p>\n<p>Execution of Program<\/p>\n<p>&nbsp;<\/p>\n<p>The above program gives us an idea about the concept of friend function. Logical usage of friend functions can solve complex problems.<\/p>\n<p>&nbsp;<\/p>\n<p>Suppose, programmer needs to operate on objects of two different classes then, friend function can be very helpful. There are ways to write a program without friend functions but programs\u00a0 would be longer, complex and hard to understand in that case. Following program illustrates utility of friend function in function overloading. Here we intends to find out number of tiles required for flooring a room defined through a class named Room, however tiles could be either square or rectangle. Two different classes exists for Square tiles and Rectangle tiles. A function named totalTiles is defined which accepts object of Square tiles and object of Room. totalTiles function would return number of tiles required for flooring. totalTiles function is overloaded with different set of arguments. Overloaded function totalTile accepts two arguments, first is object of class rectangleTile and other is object of room. Complier would accept overloading of totalTiles as object type is different in both the cases.<\/p>\n<p>&nbsp;<\/p>\n<p>\/* Program : C++ program to operate on Objects of two Different class using friend Function*\/<\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;iostream&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>#include&lt;cmath&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>using namespace std;<\/p>\n<p>&nbsp;<\/p>\n<p>\/* Forward declaration of class squareTile and rectangleTile *\/<\/p>\n<p>&nbsp;<\/p>\n<p>class squareTile;<\/p>\n<p>class rectangleTile;<\/p>\n<p>&nbsp;<\/p>\n<p>class Room<\/p>\n<p>{<\/p>\n<p>private:<\/p>\n<p>int length;<\/p>\n<p>int breadth;<\/p>\n<p>&nbsp;<\/p>\n<p>public:<\/p>\n<p>Room(int l, int b){ length = l; breadth = b; }<\/p>\n<p>int getroomArea() { return ( length * breadth); }<\/p>\n<p>&nbsp;<\/p>\n<p>friend int totalTiles(squareTile sT, Room r); \/\/friend function friend int totalTiles(rectangleTile rT, Room r); \/\/friend function<\/p>\n<p>};<\/p>\n<p>class squareTile<\/p>\n<p>{<\/p>\n<p>private:<\/p>\n<p>int length;<\/p>\n<p>public:<\/p>\n<p>squareTile(int l){length=l;}<\/p>\n<p>friend int totalTiles(squareTile sT, Room r); \/\/friend function<\/p>\n<p>};<\/p>\n<p>class rectangleTile<\/p>\n<p>{<\/p>\n<p>private:<\/p>\n<p>int length, breadth;<\/p>\n<p>public:<\/p>\n<div>\n<p>rectangleTile(int l, int b){length=l;breadth = b;}<\/p>\n<p>&nbsp;<\/p>\n<p>friend int totalTiles(rectangleTile rT, Room r); \/\/friend function<\/p>\n<p>};<\/p>\n<\/div>\n<div>\n<p>int totalTiles(rectangleTile rT, Room r) {<\/p>\n<p>\/\/function definition<\/p>\n<\/div>\n<div>\n<p>int areaRoom = r.getroomArea(); int areaTile=rT.length * rT.breadth; int nTiles = ceil(areaRoom \/ areaTile); return (nTiles);<\/p>\n<p>\/\/accessing private data from non-member function<\/p>\n<\/div>\n<div>\n<p>}<\/p>\n<\/div>\n<div>\n<p>int totalTiles(squareTile sT, Room r) {<\/p>\n<p>\/\/function definition<\/p>\n<\/div>\n<div>\n<p>int areaRoom = r.getroomArea();<\/p>\n<p>int areaTile=sT.length * sT.length;<\/p>\n<p>int nTiles = ceil(areaRoom \/ areaTile);<\/p>\n<p>return nTiles;<\/p>\n<p>\/\/accessing private data from non-member function<\/p>\n<\/div>\n<div>\n<p>}<\/p>\n<p>int main()<\/p>\n<p>{<\/p>\n<p>Room r(50,20);<\/p>\n<p>rectangleTile rT(2,3);<\/p>\n<p>squareTile sT(2);<\/p>\n<\/div>\n<p>cout&lt;&lt;endl&lt;&lt;&#8221; Area of room is &#8221; &lt;&lt; r.getroomArea(); cout&lt;&lt;endl&lt;&lt; &#8221; Rectangle Tiles Required is &#8221; &lt;&lt; totalTiles(rT, r);<\/p>\n<p>cout&lt;&lt;endl&lt;&lt; &#8221; Square Tiles Required is &#8221; &lt;&lt; totalTiles(sT, r); return 0;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Above program includes forward declaration of class squareTile and rectangleTile is to be made. It is because class Room is referencing object of class squareTile and rectangleTile while declaring following two friend functions.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>friend int totalTiles(squareTile sT, Room r);<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>friend int totalTiles(rectangleTile rT, Room r);<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Execution of program can be seen below<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-81\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-23.png\" alt=\"\" width=\"677\" height=\"120\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-23.png 677w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-23-300x53.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-23-65x12.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-23-225x40.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-23-350x62.png 350w\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" \/><\/p>\n<p style=\"text-align: left\">Execution of program : demonstrating utility of friend function.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Properties of Friend function<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>As friend functions come into the category of special functions in C++, it is advantageous to understand scope of functionalities of friend function. Following are the properties of Friend Function.<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>Friend function of the class can be member of some other class.<\/li>\n<li>Friend function of one class can be friend of all the classes in one program, such a friend is referred as Global Friend.<\/li>\n<li>Friend can access the private or protected members of the class in which they are declared Friends are non-member functions and hence do not have \u201cthis\u201d pointer pointing to themselves.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Friend can be declared anywhere (in public, protected or private section) in the class.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Pointer to Function<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>We can define pointer to function as follows<\/p>\n<p>&nbsp;<\/p>\n<p>Return Type (*functionName) ( dataType arg1, dataType arg2, dataType arg3, \u2026\u2026\u2026\u2026..);<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>For example we can define pointer to an integer function, which takes two parameter, int and float respectively as follows :<\/p>\n<p>&nbsp;<\/p>\n<p>int\u00a0\u00a0\u00a0 (*function_ptr) ( int\u00a0 x, float y);<\/p>\n<p>If we have two functions<\/p>\n<p>nt\u00a0 doWork(int data1, float data2);<\/p>\n<p>int compute( int value1, float value2);<\/p>\n<p>&nbsp;<\/p>\n<p>We can initialize function_ptr with any of the two functions<\/p>\n<p>function_ptr = doWork;<\/p>\n<p>function_ptr = compute;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Pointer to Member Function<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>We can also define pointer to member functions as follows Time t;<\/p>\n<p>void\u00a0\u00a0 (Time :: *fptr)();<\/p>\n<p>&nbsp;<\/p>\n<p>Here fptr is defined as pointer to any void member function without parameter of class Time. It can be made to point to any member function of class Time which does not take any argument and has void as a return type. We can make it to point to readTime() and displayTime() member functions of class Time as follows, as both are void functions with no arguments<\/p>\n<p>&nbsp;<\/p>\n<p>fptr =\u00a0 &amp;Time :: readTime();<\/p>\n<p>fptr =\u00a0 &amp;Time :: displayTime();<\/p>\n<p>&nbsp;<\/p>\n<p>Member function can be called though pointer as follows t.(*fptr)();<\/p>\n<p>&nbsp;<\/p>\n<p>Here if fptr is pointing to readTime(), this call will result into invokation of readTime() function on object <strong>t<\/strong>. If fptr is pointing to displayTime(), this call will result into invokation of displayTime() function on object <strong>t.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>We can also initialize pointer to member function at the time of declaration as follows Time t;<\/p>\n<p>void\u00a0\u00a0 (Time :: *fptr)() = &amp;Time :: readTime()<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Functions are powerful tools for creating modular design of program which is readable, effective , easy to modify, test, debug, and maintain\u2026\u2026..<\/strong><\/p>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Functions in C++ II<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/qDOnc-2h_50\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><strong>Additional References<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>1) Stanley Lippmann, \u201cC++ Primer\u201d, Pearson Education.<\/p>\n<p>2) Bjarne Stroustrup, \u201cThe C++ Programming Language\u201d, Pearson Education.<\/p>\n<p>3) Scott Mayer, \u201cEffective C++\u201d, Addison Wesley.<\/p>\n<p>4) Bhushan Trivedi , Programming with ANSI C++, 2\/e , Oxford University Press.<\/p>\n<p>5) Yashavant P. Kanetkar \u201cLet Us C++\u201d , Bpb Publications.<\/p>\n<p>6) Abhiram G. Ranade, \u201cAn Introduction to Programming through C++\u201d , McGraw Hill<\/p>\n<p>7) Ellis and B. Stroustrup \u201cAnnotated C++ Reference Manual\u201d, http:\/\/www.stroustrup.com\/arm.html<\/p>\n<p>8) Herbert Schildt, \u201cComplete Reference C++\u201d, McGraw Hill Publications.<\/p>\n<p>9) Ashok Kamthane, \u201cObject Oriented Programming with ANSI and Turbo C++\u201d, Pearson Education<\/p>\n<p>10) E Balaguruswami, \u201cObject Oriented Programming With C++\u201d, Tata McGraw Hill<\/p>\n<p>11) \u201cC++ FAQs\u201d, Pearson Education.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"author":4,"menu_order":8,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-jyoti-pareek"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-72","chapter","type-chapter","status-publish","hentry","contributor-dr-jyoti-pareek"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/72","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":5,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/72\/revisions"}],"predecessor-version":[{"id":393,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/72\/revisions\/393"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/72\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/media?parent=72"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapter-type?post=72"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/contributor?post=72"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/license?post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}