Download presentation
Presentation is loading. Please wait.
1
Programming with ANSI C ++
A Step-by-Step Approach Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
2
Chapter 4 Functions
3
Similarity and difference with C 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
4
Inline functions 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
5
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); // following is wrong! CheckPass(int Sub1Marks, int PassingMarks1 = 50, int Sub2Marks, int PassingMark2 = 35);
6
Objects as parameters class Time { } Time Difference(Time , Time);
Temporary object creation and NRV optimization
7
Call by and return reference
Time AddTimes(Time & , Time & ); StudList.GetStudent(OriginalName) = NewNode; Returning a temporary object as an alias of existing variable
8
Prototype and overloading
A 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
9
Other issues Function overloading and polymorphism
Use and differences between default argument and function overloading solutions Program Readability and Default arguments
10
Member and non member 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.
11
Non friend non member function
The 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
12
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
13
Other issues Const and Volatile functions
void PrintDetails() const { ….} void CheckValuesForNIC() volatile {…} Mutable data members
14
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,
15
Static and normal functions
They 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.
16
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]; } }
17
Public and Private functions
class employee { … bool IsPermanent() /* a private function */ return (EmpNo > 2000); }
18
Non member function pointers
int (*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);
19
Member function pointers
int 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;
20
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.
21
Adding C function to a C program
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(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.