437 33 Powerpoint-Slides Chapter-4-Functions Ch4
437 33 Powerpoint-Slides Chapter-4-Functions Ch4
Chapter 4 Functions
The function calling, execution and return of a C++ function is technically similar to that of a C function. C++ has inline functions when programmer would like to eliminate the overhead of context switching Also, the object code of the C++ function contains some additional information then a C function. Main is different here
Inline fucntions
provide
the flexibility of normal functions and provide efficiency of macros precede the function name with keyword inline at the time of definition must be defined before usage Eliminate the context switching overhead Increase the program size It is a request
Default arguments
bool
CheckPass(int TotalMarks, int PassingMarks = 50); CheckPass(int Sub1Marks, int Sub2Marks, int PassingMarks = 50) CheckPass(int Sub1Marks, int Sub2Marks, int PassingMarks1 = 50, int PassingMark2 = 35);
Default arguments
//
following is wrong! CheckPass(int Sub1Marks, int PassingMarks1 = 50, int Sub2Marks, int PassingMark2 = 35);
Objects as parameters
class
Time { } Time Difference(Time , Time); Temporary object creation and NRV optimization
AddTimes(Time & , Time & ); StudList.GetStudent(OriginalName) = NewNode; Returning a temporary object as an alias of existing variable
function prototype MyFunction(int First, char Second) Overloading a funtion int Add(int, int); double Add(double, double); string Add(string, string); Overloading does not involve return type
Other issues
Function
overloading and polymorphism Use and differences between default argument and function overloading solutions Program Readability and Default arguments
We need to pass only one argument in place of two arguments while using a member function. In the non member case, Difference (Time1, Time2) or Difference (Time2, Time1) can be possible. Non member need either public members or be friends We do not need an explicit return statement while using a member function.
class is not cluttered with unnecessary member functions and remains manageable. The function code is not affected if private variables (the implementation of the class) changes, as the function are not using any private variables
A friend function
friend
SomeFunction(list of arguments). friend void ListDeptWise(employee[]); A function is not truly a member by nature but needs access to private members should not be made member but a friend Friends violate information hiding principle
Other issues
Const
and Volatile functions void PrintDetails() const { .} void CheckValuesForNIC() volatile {} Mutable data members
Static functions
static int TotalStudents; static void DispTotal();// a declaration void student :: DispTotal() // definition { cout << "Total Students at the moment "<< student::TotalStudents<< "\n"; } a static function can only have static variables of the class to act upon,
do not possess this pointer. They can not access other data members of the class. They can not be virtual. They can not be declared either const or volatile.
Returning an object
employee GetManager(employee EmpArray[]) { return EmpArray[i]; }} employee GetEmp(string EmpName, employee EmpArray[]) { for (int i=0;i<10;++i) { if (EmpArray[i].Name==EmpName) return EmpArray[i]; } }
(*PointFun)(int,int); // defining pointer to a function which has two arguments, both of them are integers Two ways to call the function cout << (*PointFun) (PointArg1,PointArg2); cout << PointFun(PointArg1,PointArg2);
PointFunEx :: PointAccess() int (PointFunEx::*PA)(); //a function pointer PA = PointFunEx :: PointAccess; cout << " " <<(PFE.*PA) (); int (PointFunEx::*PB)(); /*a function pointer PB is defined here */ PB = &PointFunEx :: PointAccess;
Operators
<class
name > :: for defining pointer to function and making a call. ::* operator to define pointer to a member function .* operator for accessing a member pointer using an object ->* operator for accessing a member pointer using pointer to an object.
C++ decorates the compiled function to represent its arguments and types of them. at run time, there is no overhead extern "C" void TestCLinkage(); extern "C" { void TestCLinkage(); void TestCLinkage2(); }