Functions

PropellerAds

Member functions:

All the the declared functions and varibles are part of the class and hence members of the corresponding class. Those are declared and defined outside the class are known as not the members of the class. So all the functions declared inside the class are known as member functions.
Ex:

#include <iostream>
Using namespace std;
Class Abc
{
Void show()
{
Cout&lt;&lt;”This is inside a member function”; 
}
};
Void display()
{
Cout&lt;&lt;”We are no more inside a member function”;
}
void main()
{
Abc obj;
Obj.show();
}

Member function definition and declaration
When member function is declared it is not necessary to complete its defination inside the class, for that purpose the definition is left undefined for full definition outside the class.

The main purpose is to main the C++ program more redable and understandable. When the definition itself is some thousands line of code. Then the code itself becomes clumsy for understanding and for that purpose we write the function outside the class. Referring to the previous example The Function declaration ends with semicolon(;) where definition don’t have semicolon

function declaration
Syntax:
Returntype functionname(parameter list);
Function Defination
Syntax:
Returntype classname::functionname(parameter list)
{
}
Function Calling
Main function is a separate function other than the member function, so it does not have any permission to call the member function directly. Thus calling a member function need the help of object and function is called with the help of the corresponding object for which the function is a member
Syntax function calling:
Objectname.functionname(actual parameters);
Ex:

#include
Using namespace std;
Class Abc
{
Public:
Void show(); // Declaration part
};
Void Abc::show() // Defination part
{
Cout<<”This is inside a member function”;
}
void main()
{
Abc obj;
Obj.show();
}

Need of defining function outside the Class
For better readable purpose generally functions are defined outside the class.

Inline Function
Inline functions are generally works like macro, where the function call replaces the function with its definations. This work is done during compilation not during run time execuation. Inline function reduces the accessing overhead of member function.
Syntax:
Inline return type classname::function name(arguments)

Need of Inline function
Faster in program execution
Rules for Inline Function
1. Use when function contains very few lines of code.
2. Inline function should not be used where loop exist.

How to define a member function inside or outside a class
Member function can only be accessed from outside class using class name or object name. When object is used for accessing purpose then dot(.) operaror is used. Similarly when class name is used then scope resolution operator(::) is used for accessing any function.

he number and type of parameter remains the same as during declaration of the function. But the parameter used in declaration of the function becomes dummy or formal parameters. The parameters that are passed to a function during its declaration is known as actual parameter.

Try Now – PROGRAMMING IN C++ MCQs