CPP Unit 3
CPP Unit 3
Function Prototype
→ A function prototype tells the compiler
• Name of the function,
• Type of data returned by the function (Return type),
• Number of parameters,
1|Page 186_SHRADDHA KESHAO BONDE_N2
CM3104
Return by Reference
→ A function can also return a reference.
→ Example:
int & max(int & , int &);
void main(){
int a=10, b=20,m; m = max(a, b);
cout<<”Maximum nos:”<<m;
}
int & swap (int &x, int &y){
if( x > y ) return x;
else return y;
}
→ Here, function returns reference to x or y (not value) and return type is
int&.
Inline Function
→ The functions can be made inline by adding KEYWORD inline to the
function definition.
→ An inline function is a function that is expanded in line when it is
invoked.
→ The complier replaces the function call with the function code.
→ Inline function saves time of calling function, saving registers etc.
→ If function has very few lines of code and simple expressions then only it
should be used as inline otherwise behave as normal function.
→ For ex- Some Critical Situations
→ If a loop, a switch , goto exists in function body, If function is not
returning any value
→ If function contains static variables.
→ If it is recursive.
→ Then function does not work as inline.
→ Example:
inline int cube(int n){ return n*n*n; }
int main(){ int c; c = cube(10); cout<<c; }
→ Function call is replaced with expression So,
c = cube(10);
→ Becomes c =10*10*10; at compile time.
Default Arguments
→ Default values are specified when the function is declared
→ We must add default arguments from right to left.
→ We cannot provide a default value to a particular argument in the middle
of an argument list.
4|Page 186_SHRADDHA KESHAO BONDE_N2
CM3104
Function Overloading
→ Overloading means to use the same thing for different purpose C++
allows overloading of functions.
→ Definition: “When multiple functions with same name are used for
different task and purpose it is known as function overloading”.
→ The overloading function must be different in its argument list & with
different data type
→ Compiler Differentiate it by,
• Different no of arguments
• Different types of arguments
• Different return type
→ Example: Area of shape is defined as
float area(float r); //area of circle
float area(float l, float b); //area of rectangle
Friend Function
→ A friend function is a function that is not a member of a class but it can
access private and protected member of the class in which it is declared
as friend.
→ Since friend function is not a member of class it cannot be accessed using
object of the class.
→ Sometimes it is required that private
→ A function can be declared as a friend by preceding function declaration
with friend keyword as shown below:
friend Return_Type Function_Name (Argument List);
→ Eg: Friend void add();
→ Friend function can be called as follows
function_name(actual arguments);
→ The function that is declared with the keyword friend is known as friend
function
→ This function can be defined else where in the program.
→ The function definition does not use either keyword friend or the scope
operator::
→ A function can be declared as a friend in any number of classes.
→ A friend function although not a member function, has full access right to
the private members of the class
→ Friend function having following characteristics:
(1) A friend function can be declared inside class but it is not member of
the class.
(2) It can be declared either public or private without affecting its
meaning.
(3) A friend function is not a member of class so it is not called using
object of the class. It is called like normal external function.
(4) A friend function accepts object as an argument to access private or
public member of the class.
(5) A friend function can be declared as friend in any number of classes.
------- public:
void putdata(){
------- totalmarks();
}
void totalmarks(){ ------- }
};
String Functions
→ String literals such as “Hello, world!” are actually represented by C++ as
a sequence of characters in memory. In other words, a string is simply a
7|Page 186_SHRADDHA KESHAO BONDE_N2
CM3104
String Characteristics
→ C++ also provides it’s own string class
→ A string header <string> needs to be included in the program to use the
string class.