{"id":68,"date":"2018-07-09T12:14:56","date_gmt":"2018-07-09T12:14:56","guid":{"rendered":"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=68"},"modified":"2019-05-14T11:44:26","modified_gmt":"2019-05-14T11:44:26","slug":"functions-in-c-i","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/chapter\/functions-in-c-i\/","title":{"rendered":"Functions in C++ I"},"content":{"raw":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/rLamcaQNIlU\" 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\n<div>\r\n\r\n<strong>Functions<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Functions in C++, offer similar solution for organizing large and complex program. While writing a program, the main program becomes too large and complex if programmer writes all the steps within it. The readability of program also turns to be complex. Testing or debugging becomes quite difficult. Instead, if he collects certain steps, for example finding square root or displaying record on the screen, bundles them in a routine and writes them in either a separate file or same file outside the main() function, the main programme would look much simpler. Functions facilitates partitioning a complex problem into set of sub-problems which are easily understandable, maintainable and manageable. Once the problem is divided into set of tasks, we can write function for each task. Each function implements a small task or particular aspect of a large program. A complete application can then be developed integrating all such functions.<\/p>\r\n&nbsp;\r\n\r\n<strong>Function can be defined as a group of statements that is given a unique and meaningful name within a program. It is created to perform a specific task.<\/strong>\r\n\r\n&nbsp;\r\n\r\nEvery C++ program has at least one function, which is main().One can define more functions as per the need.\r\n\r\n&nbsp;\r\n\r\nFunctions in C++ are very similar to functions in C except the return type default. The default return type in C is int whereas default return type in C++ is void. There are three steps to create and use a\r\n\r\n&nbsp;\r\n\r\nfunction in C++ program.\r\n\r\n&nbsp;\r\n\r\n1.\u00a0\u00a0\u00a0\u00a0\u00a0 Declaring a function\r\n\r\n2.\u00a0\u00a0\u00a0\u00a0\u00a0 Defining a function\r\n\r\n3.\u00a0\u00a0\u00a0\u00a0\u00a0 Calling a function\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us understand generalized structure of declaring, defining and calling a function along with an example. Assume that function to calculate <strong>x<\/strong><strong>y<\/strong> is intended, where x and y would be given by a user. Function would be given two values as input, x and y and it would return <strong>x<\/strong><strong>y<\/strong> as a result.<\/p>\r\n&nbsp;\r\n\r\n<strong>Declaring a function<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Declaration of a function includes stating to a compiler that the declared function is defined in the program, or its object module will be made available at the time of linking . Declaring a function<span style=\"text-align: initial;font-size: 1em\"> is also known as function prototyping. In C++, function prototyping is mandatory. Compiler error would be raised if a function is called without its prototype mentioned in the program before calling of the function. If a programmer writes a function to add two integers, It can be named as add and prototype of which can be written as int add(<\/span>int ,<span style=\"text-align: initial;font-size: 1em\"> int). This prototype specifies that add is a function which takes two integers as input and returns an integer value, which in this case will be <\/span>sum<span style=\"text-align: initial;font-size: 1em\"> of the given numbers. A function prototype is useful while checking whether the function when called is called with <\/span>correct<span style=\"text-align: initial;font-size: 1em\"> set of arguments. And the knowledge of return type will help <\/span>compliler<span style=\"text-align: initial;font-size: 1em\"> determine, that the call to the function is placed at correct place or not.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Defining a Function<\/strong>\r\n\r\n&nbsp;\r\n\r\nDefining function means implementing the function. The basic syntax for defining any function in C++ is: returnDataType functionName(dataType argument 1, dataType argument 2\u2026\u2026\u2026 dataType argument\u00a0 n)\r\n\r\n{\r\n\r\nfunction statements;\r\n\r\n}\r\n<ul>\r\n \t<li style=\"text-align: justify\">\u00a0returnDataType is the datatype of the value function would return back to the point where it is called. If function does not return any value, only control is returned back to the calling function. Return datatype in this case will be defined as void.<\/li>\r\n \t<li style=\"text-align: justify\">\u00a0 \u00a0 functionName is the identifier by which the function would be referred\/called.<\/li>\r\n \t<li style=\"text-align: justify\">\u00a0 \u00a0Parameters in the comma-separated list bound by the parentheses specifies argument along with the data type, that would be passed to the function from the calling function. To compute xy, the syntax to declare a function would be as follows:<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\nint power(int, int);\r\n\r\nTo compute xy, the syntax to define a function and its steps would be as follows:\r\n\r\nint power(int base, int exponent)\r\n\r\n{\r\n\r\nint result=1;\r\n\r\nfor (int i=1 ; i &lt;= exponent; i++)\r\n\r\n{\r\n\r\nresult=result * base;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nreturn result;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">In the above function, two integers are accepted by the function as per its definition, which are stored in base and exponent. The variable result is local variable with its scope limited to the function power. It is initialized to 1. A for-loop is written to multiply the number stored in base, exponent times. The final answer is stored in variable result.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Note the return statement of the function. In the above mentioned function statements return, returns result to the point where it would be called. A function at a time can return only a single value. Return statement is also an exit statement of function. Once the return statement is executed control would return to the calling point of the function and no other statements following return statement would be executed. Programmer should carefully find out the place where he expects exit from the function. If return statement is not mentioned all the sequential steps of the function would be executed and control would be returned back to the calling point.<\/p>\r\n&nbsp;\r\n\r\n<strong>Calling a function<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We have seen how to declare a function and define a C++ function. However steps inside the function body would never execute until they are called. To use a function, we will have to call or invoke that function. When a program calls a function, program control is transferred to the called function. This is known as context switching. As mentioned earlier, a called function would performs defined steps mentioned in function body and when its return statement is executed or control reaches the end of the function, program control is returned back to the main program. To call a function, you simply need to pass the required parameters along with function name, and if function returns a value, then you can store returned value. For example:<\/p>\r\n&nbsp;\r\n\r\nThe basic syntax for calling any function in C++ is:\r\n\r\n&nbsp;\r\n\r\nresult = functionName(actual arguments 1, actual argument 2, actual argument 3\u2026..)\r\n\r\n&nbsp;\r\n\r\nwhere,\r\n<ul>\r\n \t<li style=\"text-align: justify\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0result is a variable of datatype same as that of function return type specified in function declaration.<\/li>\r\n \t<li>\u00a0 \u00a0 \u00a0 \u00a0 FunctionName should be same as mentioned at the time of declaration<\/li>\r\n \t<li style=\"text-align: justify\">\u00a0 \u00a0 \u00a0 \u00a0 List of actual arguments includes list of parameters required for execution of steps mentioned in the function body at the time of definition.<\/li>\r\n<\/ul>\r\nTo compute <strong>x<\/strong><strong>y<\/strong>, the syntax to call a function and its steps would be as follows:\r\n\r\n&nbsp;\r\n\r\nanswer = power (x, y);\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>Advantage of Functions<\/strong>\r\n<ul>\r\n \t<li>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0Intellectual Manageability<\/li>\r\n \t<li>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0Modular Design<\/li>\r\n \t<li>\u00a0 \u00a0 \u00a0 \u00a0 Abstraction<\/li>\r\n \t<li>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0Readability<\/li>\r\n \t<li>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0Supports Stepwise refinement approach to program development<\/li>\r\n \t<li>\u00a0 \u00a0 \u00a0 \u00a0 Reusability<\/li>\r\n<\/ul>\r\n<strong>Disadvantage of Using Functions<\/strong>\r\n\r\n&nbsp;\r\n\r\nFunction call generates executional overhead due to context switching<strong>.<\/strong> To understand this we need to understand what is context of the function and what is context switching.\r\n\r\n&nbsp;\r\n\r\n<strong>Context of Function<\/strong>\r\n\r\n&nbsp;\r\n\r\nValues held in CPU registers, values of parameters and local variables at a given point of time forms the context for executing function\/program. When the execution of one function gets suspended for the execution of another function, it results into context switching.\r\n\r\n&nbsp;\r\n\r\n<strong>Context Switching<\/strong>\r\n\r\n&nbsp;\r\n\r\nOn function call\r\n<ul>\r\n \t<li>\u00a0 \u00a0Saving the context of calling program.<\/li>\r\n \t<li>\u00a0 Shifting control to called program<\/li>\r\n<\/ul>\r\nOn Completion of execution of called program\r\n<ul>\r\n \t<li>\u00a0 Returning the control to calling program.<\/li>\r\n \t<li>\u00a0 \u00a0 Restoring the context of calling program.<\/li>\r\n<\/ul>\r\nContext switching generates executional overhead but the advantages of function are significant and overpowering, hence this disadvantage of function can be ignored.\r\n\r\n&nbsp;\r\n\r\nAlso for simple functions this advantage can be overcome in C++ by declaring the function inline.\r\n\r\n&nbsp;\r\n\r\n<strong>Inline Functions<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Inline functions are functions for which call to a function is replaced by the code of called function at compile time. Therefore no function call is made at runtime, hence no executional overhead. However there will be some increase in the size of executable file. A function can be defined as inline by prefixing keyword inline to the function header.<\/p>\r\n<span style=\"font-size: 1em;text-align: initial\">Module 8 - Functions in C++ I<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\ninline swap(int &amp;num1, int &amp;num2)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nint temp;\r\n\r\ntemp = num1;\r\n\r\nnum1 = num2;\r\n\r\nnum2 = temp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nvoid main()\r\n\r\n{\r\n\r\nint\u00a0 x=10, y=20;\r\n\r\n\u2026\u2026\u2026\u2026\u2026\u2026.\r\n\r\n\u2026\u2026\u2026\u2026\u2026\u2026..\r\n\r\n&nbsp;\r\n\r\nswap(x,y)\r\n\r\n\u2026\u2026\u2026\u2026\u2026.\r\n\r\n\u2026\u2026\u2026\u2026\u2026.\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>Limitation of Inline Function<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">All functions can not be made inline. The request to make function inline will be ignored in the following cases:Too large or too Complex function. Recursive function.<\/p>\r\nFunction having Static Variable.\r\n\r\n&nbsp;\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&nbsp;\r\n<p style=\"text-align: justify\">Functions which are defined inside the class will automatically be considered for making them inline. If they are eligible( i.e they are not recursive, do not have static variables etc) to become inline they will be made inline.<\/p>\r\n&nbsp;\r\n\r\nclass Time\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nint hours;\r\n\r\nint minutes;\r\n\r\nint seconds;\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" style=\"width: 618px\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 240px\">public :<\/td>\r\n<td style=\"width: 378px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 240px\">void readTime();<\/td>\r\n<td style=\"width: 378px\">\/\/ will be made inline<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 240px\">{<\/td>\r\n<td style=\"width: 378px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 240px\">cin &gt;&gt; hours;<\/td>\r\n<td style=\"width: 378px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 240px\">cin &gt;&gt; minutes;<\/td>\r\n<td style=\"width: 378px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 240px\">cin &gt;&gt; seconds;<\/td>\r\n<td style=\"width: 378px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 240px\">}<\/td>\r\n<td style=\"width: 378px\"><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<div>\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 260.063px\"><\/td>\r\n<td style=\"width: 400.063px\">Module 8 - Functions in C++ I<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 260.063px\"><\/td>\r\n<td style=\"width: 400.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 260.063px\">void displayTime();<\/td>\r\n<td style=\"width: 400.063px\">\/\/ will be made inline<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 260.063px\">{<\/td>\r\n<td style=\"width: 400.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 260.063px\">cout &lt;&lt; hours;<\/td>\r\n<td style=\"width: 400.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 260.063px\">cout &lt;&lt; minutes;<\/td>\r\n<td style=\"width: 400.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 260.063px\">cout &lt;&lt; seconds;<\/td>\r\n<td style=\"width: 400.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 260.063px\">}<\/td>\r\n<td style=\"width: 400.063px\"><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n};\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Member functions defined outside the class can also be made inline by prefixing keyword inline to the function header.<\/p>\r\n&nbsp;\r\n\r\nclass Time\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nint hours;\r\n\r\nint minutes;\r\n\r\nint seconds;\r\n\r\n&nbsp;\r\n\r\npublic :\r\n\r\n&nbsp;\r\n\r\nvoid readTime();\r\n\r\n&nbsp;\r\n\r\nvoid displayTime();\r\n\r\n&nbsp;\r\n\r\n};\r\n\r\n<\/div>\r\n<div>\r\n\r\nInline void Time :: readTime();\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n\/\/ will be made inline\r\n\r\n<\/div>\r\n<div>\r\n\r\ncin &gt;&gt; hours;\r\n\r\ncin &gt;&gt; minutes;\r\n\r\ncin &gt;&gt; seconds;\r\n\r\n}\r\n\r\n<\/div>\r\n<div>\r\n\r\ninline void Time :: displayTime();\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n\/\/\u00a0 will be made inline\r\n\r\n<\/div>\r\n<div>\r\n\r\ncout &lt;&lt; hours;\r\n\r\ncout &lt;&lt; minutes;\r\n\r\n&nbsp;\r\n\r\ncout &lt;&lt; seconds;\r\n\r\n}\r\n\r\n};\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Non inline functions may be defined after its call but inline function must be fully defined before the call.<\/p>\r\n&nbsp;\r\n\r\n<strong>Parameter Passing in C++ Functions<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The parameters passed in C++ functions can be of any data type including objects, pointers and references. Also Functions can return any data type except arrays including objects, pointers and references.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In C++ except array all the data types by default are passed as values. In C we used pointers to achieve the effect of call by reference parameter passing. In C++ we can use pointers and reference variables to achieve the effect of call by reference parameter passing. We discussed all this in detail in Module 4.<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>Returning reference from C++ variables<\/strong>\r\n\r\n&nbsp;\r\n\r\nIn C++ we can also return references from the functions\r\n\r\nint\u00a0 &amp;max( int &amp;x, int &amp;y)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nIf ( x &gt; y )\r\n\r\nreturn x;\r\n\r\nelse\r\n\r\n&nbsp;\r\n\r\nreturn y;\r\n\r\n}\r\n\r\nvoid main()\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nInt value1, value2;\r\n\r\nInt maxvalue;\r\n\r\nmaxvalue = max(value1,value2);\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nMore interestingly we can place the function call to a function returning reference variable at the left hand side of operation. For example\r\n\r\nWe write following statement\r\n\r\nmax( value1, value2) =\u00a0 20;\r\n\r\nThis statement will assign 20 value1 or value2 depending on which one is larger.\r\n\r\n&nbsp;\r\n\r\n<strong>Static Functions<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Similar to static member variables, static member functions are not attached to any particular object, but remains common to a class. Following program demonstrates usage of static member function update_maxmarks(), which generates roll number which is next to previous generated roll no. Here it can be observed that static member function update_maxmarks(), access static integer max_marks() and updates it with the value provided as parameter. What is noticable is the ways it has been called in the main function. Although it seems that update_maxmarks(), is member function of Student class and hence can be called only if any object of Student class is created, it has been called without any object creation only by using class name as its prefix.<\/p>\r\n&nbsp;\r\n\r\nclass\u00a0 student\r\n\r\n{\r\n\r\nint rollno;\r\n\r\n&nbsp;\r\n\r\nchar\u00a0 name[20];\r\n\r\nint marks_obtained;\r\n\r\nstatic int max_marks;\r\n\r\npublic :\r\n\r\nvoid readdata();\r\n\r\n&nbsp;\r\n\r\nvoid displaydata();\r\n\r\nint\u00a0\u00a0 computePercentage();\r\n\r\n<\/div>\r\n<div>\r\n\r\nstatic int update_maxmarks(int\u00a0 mks) { total_marks = mks;}\r\n\r\n&nbsp;\r\n\r\n};\r\n\r\n&nbsp;\r\n\r\n\/\/\u00a0\u00a0 Initialize static data member int student :: max_marks = 0;\r\n\r\n&nbsp;\r\n\r\n<strong>Restrictions on static member Functions<\/strong>\r\n\r\n&nbsp;\r\n<ul>\r\n \t<li>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0They do not get this pointer implicitly passed<\/li>\r\n \t<li>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0They cannot access not static data member of the class<\/li>\r\n \t<li>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0They cannot be virtual<\/li>\r\n \t<li>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0They cannot be const or volatile.<\/li>\r\n<\/ul>\r\n<strong>Const Function<\/strong>\r\n\r\n&nbsp;\r\n\r\nConst member function is a function which cannot modify data members of the class.\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nclass Time\r\n\r\n{\r\n\r\nint hours;\r\n\r\nint minutes;\r\n\r\nint seconds;\r\n\r\npublic :\r\n\r\nvoid readTime()\r\n\r\n{\r\n\r\ncin &gt;&gt; hours;\r\n\r\ncin &gt;&gt; minutes;\r\n\r\ncin &gt;&gt; seconds;\r\n\r\n}\r\n\r\nvoid displayTime() const\r\n\r\n{\r\n\r\n&nbsp;\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 261.063px\">cout &lt;&lt;<\/td>\r\n<td style=\"width: 110.063px\">hours;<\/td>\r\n<td style=\"width: 51.0625px\"><\/td>\r\n<td style=\"width: 210.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 261.063px\">cout &lt;&lt; minutes;<\/td>\r\n<td style=\"width: 110.063px\"><\/td>\r\n<td style=\"width: 51.0625px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 261.063px\">cout &lt;&lt; seconds;<\/td>\r\n<td style=\"width: 110.063px\"><\/td>\r\n<td style=\"width: 51.0625px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 261.063px\">hours =<\/td>\r\n<td style=\"width: 110.063px\">12;<\/td>\r\n<td style=\"width: 51.0625px\">\/\/<\/td>\r\n<td style=\"width: 210.063px\">will not work<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 261.063px\">minutes<\/td>\r\n<td style=\"width: 110.063px\">= 35;<\/td>\r\n<td style=\"width: 51.0625px\">\/\/<\/td>\r\n<td style=\"width: 210.063px\">will not work<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 261.063px\">seconds<\/td>\r\n<td style=\"width: 110.063px\">= 40;<\/td>\r\n<td style=\"width: 51.0625px\">\/\/<\/td>\r\n<td style=\"width: 210.063px\">will not work<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n}\r\n\r\n};\r\n\r\n&nbsp;\r\n\r\nIn the above class function displayTime() has been declared as constant function , hence we will not be able to modify any of the data members of the class in function displyTime().\r\n\r\n<\/div>\r\n<strong>Mutable Data Type<\/strong>\r\n\r\n&nbsp;\r\n\r\nIf we wish to modify any data member in constant function, we can declare that data member as mutable as shown in the following example\r\n\r\n&nbsp;\r\n\r\nclass Time\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nint hours;\r\n\r\nint minutes;\r\n\r\nmutable int seconds;\r\n\r\npublic :\r\n\r\nvoid readTime()\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\ncin &gt;&gt; hours;\r\n\r\ncin &gt;&gt; minutes;\r\n\r\ncin &gt;&gt; seconds;\r\n\r\n}\r\n\r\nvoid displayTime() const\r\n\r\n{\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td>cout &lt;&lt;<\/td>\r\n<td>hours;<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>cout &lt;&lt; minutes;<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>cout &lt;&lt; seconds;<\/td>\r\n<td><\/td>\r\n<td><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>hours =<\/td>\r\n<td>12;<\/td>\r\n<td>\/\/<\/td>\r\n<td>will not work<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>minutes<\/td>\r\n<td>= 35;<\/td>\r\n<td>\/\/<\/td>\r\n<td>will not work<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>seconds<\/td>\r\n<td>= 40;<\/td>\r\n<td>\/\/<\/td>\r\n<td>will work<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\n}\r\n\r\n};\r\n\r\n&nbsp;\r\n\r\nAs we have declared data member second in the class as mutable, we can modify it in even constant function.\r\n\r\n&nbsp;\r\n\r\n<strong>Volatile Functions<\/strong>\r\n\r\n&nbsp;\r\n\r\nA member function can also be declared as volatile. Volatile member function can only be invoked on volatile object.\r\n<p style=\"text-align: justify\">A volatile object is a object whose values can be changed by external parameters such as hardware interrupt, network cards etc. which are not in control of the program.<\/p>\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++ I<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/rLamcaQNIlU\" 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>","rendered":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/rLamcaQNIlU\" 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<div>\n<p><strong>Functions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Functions in C++, offer similar solution for organizing large and complex program. While writing a program, the main program becomes too large and complex if programmer writes all the steps within it. The readability of program also turns to be complex. Testing or debugging becomes quite difficult. Instead, if he collects certain steps, for example finding square root or displaying record on the screen, bundles them in a routine and writes them in either a separate file or same file outside the main() function, the main programme would look much simpler. Functions facilitates partitioning a complex problem into set of sub-problems which are easily understandable, maintainable and manageable. Once the problem is divided into set of tasks, we can write function for each task. Each function implements a small task or particular aspect of a large program. A complete application can then be developed integrating all such functions.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Function can be defined as a group of statements that is given a unique and meaningful name within a program. It is created to perform a specific task.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Every C++ program has at least one function, which is main().One can define more functions as per the need.<\/p>\n<p>&nbsp;<\/p>\n<p>Functions in C++ are very similar to functions in C except the return type default. The default return type in C is int whereas default return type in C++ is void. There are three steps to create and use a<\/p>\n<p>&nbsp;<\/p>\n<p>function in C++ program.<\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0\u00a0\u00a0\u00a0\u00a0 Declaring a function<\/p>\n<p>2.\u00a0\u00a0\u00a0\u00a0\u00a0 Defining a function<\/p>\n<p>3.\u00a0\u00a0\u00a0\u00a0\u00a0 Calling a function<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us understand generalized structure of declaring, defining and calling a function along with an example. Assume that function to calculate <strong>x<\/strong><strong>y<\/strong> is intended, where x and y would be given by a user. Function would be given two values as input, x and y and it would return <strong>x<\/strong><strong>y<\/strong> as a result.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Declaring a function<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Declaration of a function includes stating to a compiler that the declared function is defined in the program, or its object module will be made available at the time of linking . Declaring a function<span style=\"text-align: initial;font-size: 1em\"> is also known as function prototyping. In C++, function prototyping is mandatory. Compiler error would be raised if a function is called without its prototype mentioned in the program before calling of the function. If a programmer writes a function to add two integers, It can be named as add and prototype of which can be written as int add(<\/span>int ,<span style=\"text-align: initial;font-size: 1em\"> int). This prototype specifies that add is a function which takes two integers as input and returns an integer value, which in this case will be <\/span>sum<span style=\"text-align: initial;font-size: 1em\"> of the given numbers. A function prototype is useful while checking whether the function when called is called with <\/span>correct<span style=\"text-align: initial;font-size: 1em\"> set of arguments. And the knowledge of return type will help <\/span>compliler<span style=\"text-align: initial;font-size: 1em\"> determine, that the call to the function is placed at correct place or not.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Defining a Function<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Defining function means implementing the function. The basic syntax for defining any function in C++ is: returnDataType functionName(dataType argument 1, dataType argument 2\u2026\u2026\u2026 dataType argument\u00a0 n)<\/p>\n<p>{<\/p>\n<p>function statements;<\/p>\n<p>}<\/p>\n<ul>\n<li style=\"text-align: justify\">\u00a0returnDataType is the datatype of the value function would return back to the point where it is called. If function does not return any value, only control is returned back to the calling function. Return datatype in this case will be defined as void.<\/li>\n<li style=\"text-align: justify\">\u00a0 \u00a0 functionName is the identifier by which the function would be referred\/called.<\/li>\n<li style=\"text-align: justify\">\u00a0 \u00a0Parameters in the comma-separated list bound by the parentheses specifies argument along with the data type, that would be passed to the function from the calling function. To compute xy, the syntax to declare a function would be as follows:<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>int power(int, int);<\/p>\n<p>To compute xy, the syntax to define a function and its steps would be as follows:<\/p>\n<p>int power(int base, int exponent)<\/p>\n<p>{<\/p>\n<p>int result=1;<\/p>\n<p>for (int i=1 ; i &lt;= exponent; i++)<\/p>\n<p>{<\/p>\n<p>result=result * base;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>return result;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">In the above function, two integers are accepted by the function as per its definition, which are stored in base and exponent. The variable result is local variable with its scope limited to the function power. It is initialized to 1. A for-loop is written to multiply the number stored in base, exponent times. The final answer is stored in variable result.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Note the return statement of the function. In the above mentioned function statements return, returns result to the point where it would be called. A function at a time can return only a single value. Return statement is also an exit statement of function. Once the return statement is executed control would return to the calling point of the function and no other statements following return statement would be executed. Programmer should carefully find out the place where he expects exit from the function. If return statement is not mentioned all the sequential steps of the function would be executed and control would be returned back to the calling point.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Calling a function<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We have seen how to declare a function and define a C++ function. However steps inside the function body would never execute until they are called. To use a function, we will have to call or invoke that function. When a program calls a function, program control is transferred to the called function. This is known as context switching. As mentioned earlier, a called function would performs defined steps mentioned in function body and when its return statement is executed or control reaches the end of the function, program control is returned back to the main program. To call a function, you simply need to pass the required parameters along with function name, and if function returns a value, then you can store returned value. For example:<\/p>\n<p>&nbsp;<\/p>\n<p>The basic syntax for calling any function in C++ is:<\/p>\n<p>&nbsp;<\/p>\n<p>result = functionName(actual arguments 1, actual argument 2, actual argument 3\u2026..)<\/p>\n<p>&nbsp;<\/p>\n<p>where,<\/p>\n<ul>\n<li style=\"text-align: justify\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0result is a variable of datatype same as that of function return type specified in function declaration.<\/li>\n<li>\u00a0 \u00a0 \u00a0 \u00a0 FunctionName should be same as mentioned at the time of declaration<\/li>\n<li style=\"text-align: justify\">\u00a0 \u00a0 \u00a0 \u00a0 List of actual arguments includes list of parameters required for execution of steps mentioned in the function body at the time of definition.<\/li>\n<\/ul>\n<p>To compute <strong>x<\/strong><strong>y<\/strong>, the syntax to call a function and its steps would be as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>answer = power (x, y);<\/p>\n<\/div>\n<div>\n<p><strong>Advantage of Functions<\/strong><\/p>\n<ul>\n<li>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0Intellectual Manageability<\/li>\n<li>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0Modular Design<\/li>\n<li>\u00a0 \u00a0 \u00a0 \u00a0 Abstraction<\/li>\n<li>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0Readability<\/li>\n<li>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0Supports Stepwise refinement approach to program development<\/li>\n<li>\u00a0 \u00a0 \u00a0 \u00a0 Reusability<\/li>\n<\/ul>\n<p><strong>Disadvantage of Using Functions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Function call generates executional overhead due to context switching<strong>.<\/strong> To understand this we need to understand what is context of the function and what is context switching.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Context of Function<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Values held in CPU registers, values of parameters and local variables at a given point of time forms the context for executing function\/program. When the execution of one function gets suspended for the execution of another function, it results into context switching.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Context Switching<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>On function call<\/p>\n<ul>\n<li>\u00a0 \u00a0Saving the context of calling program.<\/li>\n<li>\u00a0 Shifting control to called program<\/li>\n<\/ul>\n<p>On Completion of execution of called program<\/p>\n<ul>\n<li>\u00a0 Returning the control to calling program.<\/li>\n<li>\u00a0 \u00a0 Restoring the context of calling program.<\/li>\n<\/ul>\n<p>Context switching generates executional overhead but the advantages of function are significant and overpowering, hence this disadvantage of function can be ignored.<\/p>\n<p>&nbsp;<\/p>\n<p>Also for simple functions this advantage can be overcome in C++ by declaring the function inline.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Inline Functions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Inline functions are functions for which call to a function is replaced by the code of called function at compile time. Therefore no function call is made at runtime, hence no executional overhead. However there will be some increase in the size of executable file. A function can be defined as inline by prefixing keyword inline to the function header.<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">Module 8 &#8211; Functions in C++ I<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>inline swap(int &amp;num1, int &amp;num2)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>int temp;<\/p>\n<p>temp = num1;<\/p>\n<p>num1 = num2;<\/p>\n<p>num2 = temp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>void main()<\/p>\n<p>{<\/p>\n<p>int\u00a0 x=10, y=20;<\/p>\n<p>\u2026\u2026\u2026\u2026\u2026\u2026.<\/p>\n<p>\u2026\u2026\u2026\u2026\u2026\u2026..<\/p>\n<p>&nbsp;<\/p>\n<p>swap(x,y)<\/p>\n<p>\u2026\u2026\u2026\u2026\u2026.<\/p>\n<p>\u2026\u2026\u2026\u2026\u2026.<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Limitation of Inline Function<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">All functions can not be made inline. The request to make function inline will be ignored in the following cases:Too large or too Complex function. Recursive function.<\/p>\n<p>Function having Static Variable.<\/p>\n<p>&nbsp;<\/p>\n<p>\u2026\u2026\u2026\u2026.<\/p>\n<p>&nbsp;<\/p>\n<p>\u2026\u2026\u2026\u2026.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Functions which are defined inside the class will automatically be considered for making them inline. If they are eligible( i.e they are not recursive, do not have static variables etc) to become inline they will be made inline.<\/p>\n<p>&nbsp;<\/p>\n<p>class Time<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>int hours;<\/p>\n<p>int minutes;<\/p>\n<p>int seconds;<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\" style=\"width: 618px\">\n<tbody>\n<tr>\n<td style=\"width: 240px\">public :<\/td>\n<td style=\"width: 378px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 240px\">void readTime();<\/td>\n<td style=\"width: 378px\">\/\/ will be made inline<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 240px\">{<\/td>\n<td style=\"width: 378px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 240px\">cin &gt;&gt; hours;<\/td>\n<td style=\"width: 378px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 240px\">cin &gt;&gt; minutes;<\/td>\n<td style=\"width: 378px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 240px\">cin &gt;&gt; seconds;<\/td>\n<td style=\"width: 378px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 240px\">}<\/td>\n<td style=\"width: 378px\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td style=\"width: 260.063px\"><\/td>\n<td style=\"width: 400.063px\">Module 8 &#8211; Functions in C++ I<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 260.063px\"><\/td>\n<td style=\"width: 400.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 260.063px\">void displayTime();<\/td>\n<td style=\"width: 400.063px\">\/\/ will be made inline<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 260.063px\">{<\/td>\n<td style=\"width: 400.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 260.063px\">cout &lt;&lt; hours;<\/td>\n<td style=\"width: 400.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 260.063px\">cout &lt;&lt; minutes;<\/td>\n<td style=\"width: 400.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 260.063px\">cout &lt;&lt; seconds;<\/td>\n<td style=\"width: 400.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 260.063px\">}<\/td>\n<td style=\"width: 400.063px\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Member functions defined outside the class can also be made inline by prefixing keyword inline to the function header.<\/p>\n<p>&nbsp;<\/p>\n<p>class Time<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>int hours;<\/p>\n<p>int minutes;<\/p>\n<p>int seconds;<\/p>\n<p>&nbsp;<\/p>\n<p>public :<\/p>\n<p>&nbsp;<\/p>\n<p>void readTime();<\/p>\n<p>&nbsp;<\/p>\n<p>void displayTime();<\/p>\n<p>&nbsp;<\/p>\n<p>};<\/p>\n<\/div>\n<div>\n<p>Inline void Time :: readTime();<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>\/\/ will be made inline<\/p>\n<\/div>\n<div>\n<p>cin &gt;&gt; hours;<\/p>\n<p>cin &gt;&gt; minutes;<\/p>\n<p>cin &gt;&gt; seconds;<\/p>\n<p>}<\/p>\n<\/div>\n<div>\n<p>inline void Time :: displayTime();<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>\/\/\u00a0 will be made inline<\/p>\n<\/div>\n<div>\n<p>cout &lt;&lt; hours;<\/p>\n<p>cout &lt;&lt; minutes;<\/p>\n<p>&nbsp;<\/p>\n<p>cout &lt;&lt; seconds;<\/p>\n<p>}<\/p>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Non inline functions may be defined after its call but inline function must be fully defined before the call.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Parameter Passing in C++ Functions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The parameters passed in C++ functions can be of any data type including objects, pointers and references. Also Functions can return any data type except arrays including objects, pointers and references.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In C++ except array all the data types by default are passed as values. In C we used pointers to achieve the effect of call by reference parameter passing. In C++ we can use pointers and reference variables to achieve the effect of call by reference parameter passing. We discussed all this in detail in Module 4.<\/p>\n<\/div>\n<div>\n<p><strong>Returning reference from C++ variables<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>In C++ we can also return references from the functions<\/p>\n<p>int\u00a0 &amp;max( int &amp;x, int &amp;y)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>If ( x &gt; y )<\/p>\n<p>return x;<\/p>\n<p>else<\/p>\n<p>&nbsp;<\/p>\n<p>return y;<\/p>\n<p>}<\/p>\n<p>void main()<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>Int value1, value2;<\/p>\n<p>Int maxvalue;<\/p>\n<p>maxvalue = max(value1,value2);<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>More interestingly we can place the function call to a function returning reference variable at the left hand side of operation. For example<\/p>\n<p>We write following statement<\/p>\n<p>max( value1, value2) =\u00a0 20;<\/p>\n<p>This statement will assign 20 value1 or value2 depending on which one is larger.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Static Functions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Similar to static member variables, static member functions are not attached to any particular object, but remains common to a class. Following program demonstrates usage of static member function update_maxmarks(), which generates roll number which is next to previous generated roll no. Here it can be observed that static member function update_maxmarks(), access static integer max_marks() and updates it with the value provided as parameter. What is noticable is the ways it has been called in the main function. Although it seems that update_maxmarks(), is member function of Student class and hence can be called only if any object of Student class is created, it has been called without any object creation only by using class name as its prefix.<\/p>\n<p>&nbsp;<\/p>\n<p>class\u00a0 student<\/p>\n<p>{<\/p>\n<p>int rollno;<\/p>\n<p>&nbsp;<\/p>\n<p>char\u00a0 name[20];<\/p>\n<p>int marks_obtained;<\/p>\n<p>static int max_marks;<\/p>\n<p>public :<\/p>\n<p>void readdata();<\/p>\n<p>&nbsp;<\/p>\n<p>void displaydata();<\/p>\n<p>int\u00a0\u00a0 computePercentage();<\/p>\n<\/div>\n<div>\n<p>static int update_maxmarks(int\u00a0 mks) { total_marks = mks;}<\/p>\n<p>&nbsp;<\/p>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/\u00a0\u00a0 Initialize static data member int student :: max_marks = 0;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Restrictions on static member Functions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0They do not get this pointer implicitly passed<\/li>\n<li>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0They cannot access not static data member of the class<\/li>\n<li>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0They cannot be virtual<\/li>\n<li>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0They cannot be const or volatile.<\/li>\n<\/ul>\n<p><strong>Const Function<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Const member function is a function which cannot modify data members of the class.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>class Time<\/p>\n<p>{<\/p>\n<p>int hours;<\/p>\n<p>int minutes;<\/p>\n<p>int seconds;<\/p>\n<p>public :<\/p>\n<p>void readTime()<\/p>\n<p>{<\/p>\n<p>cin &gt;&gt; hours;<\/p>\n<p>cin &gt;&gt; minutes;<\/p>\n<p>cin &gt;&gt; seconds;<\/p>\n<p>}<\/p>\n<p>void displayTime() const<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td style=\"width: 261.063px\">cout &lt;&lt;<\/td>\n<td style=\"width: 110.063px\">hours;<\/td>\n<td style=\"width: 51.0625px\"><\/td>\n<td style=\"width: 210.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 261.063px\">cout &lt;&lt; minutes;<\/td>\n<td style=\"width: 110.063px\"><\/td>\n<td style=\"width: 51.0625px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 261.063px\">cout &lt;&lt; seconds;<\/td>\n<td style=\"width: 110.063px\"><\/td>\n<td style=\"width: 51.0625px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 261.063px\">hours =<\/td>\n<td style=\"width: 110.063px\">12;<\/td>\n<td style=\"width: 51.0625px\">\/\/<\/td>\n<td style=\"width: 210.063px\">will not work<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 261.063px\">minutes<\/td>\n<td style=\"width: 110.063px\">= 35;<\/td>\n<td style=\"width: 51.0625px\">\/\/<\/td>\n<td style=\"width: 210.063px\">will not work<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 261.063px\">seconds<\/td>\n<td style=\"width: 110.063px\">= 40;<\/td>\n<td style=\"width: 51.0625px\">\/\/<\/td>\n<td style=\"width: 210.063px\">will not work<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>}<\/p>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<p>In the above class function displayTime() has been declared as constant function , hence we will not be able to modify any of the data members of the class in function displyTime().<\/p>\n<\/div>\n<p><strong>Mutable Data Type<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>If we wish to modify any data member in constant function, we can declare that data member as mutable as shown in the following example<\/p>\n<p>&nbsp;<\/p>\n<p>class Time<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>int hours;<\/p>\n<p>int minutes;<\/p>\n<p>mutable int seconds;<\/p>\n<p>public :<\/p>\n<p>void readTime()<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>cin &gt;&gt; hours;<\/p>\n<p>cin &gt;&gt; minutes;<\/p>\n<p>cin &gt;&gt; seconds;<\/p>\n<p>}<\/p>\n<p>void displayTime() const<\/p>\n<p>{<\/p>\n<table>\n<tbody>\n<tr>\n<td>cout &lt;&lt;<\/td>\n<td>hours;<\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>cout &lt;&lt; minutes;<\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>cout &lt;&lt; seconds;<\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>hours =<\/td>\n<td>12;<\/td>\n<td>\/\/<\/td>\n<td>will not work<\/td>\n<\/tr>\n<tr>\n<td>minutes<\/td>\n<td>= 35;<\/td>\n<td>\/\/<\/td>\n<td>will not work<\/td>\n<\/tr>\n<tr>\n<td>seconds<\/td>\n<td>= 40;<\/td>\n<td>\/\/<\/td>\n<td>will work<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<p>As we have declared data member second in the class as mutable, we can modify it in even constant function.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Volatile Functions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>A member function can also be declared as volatile. Volatile member function can only be invoked on volatile object.<\/p>\n<p style=\"text-align: justify\">A volatile object is a object whose values can be changed by external parameters such as hardware interrupt, network cards etc. which are not in control of the program.<\/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++ I<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/rLamcaQNIlU\" 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","protected":false},"author":4,"menu_order":7,"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-68","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\/68","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":8,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/68\/revisions"}],"predecessor-version":[{"id":391,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/68\/revisions\/391"}],"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\/68\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/media?parent=68"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapter-type?post=68"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/contributor?post=68"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/license?post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}