Functions with Empty Parameter Lists and Inline function in CPP

Function With Empty Parameter Lists in C++
Inline Functions


Function With Empty Parameter lists in C++

In C++, an empty parameter list is specified by writing either void or nothing at all in parentheses. The prototype

void print();

specifies that function print does not take arguments and does not return a value. Demonstrates both ways to declare and use functions that do not take arguments.

Always provide function prototypes, even though it is possible to omit them when functions are defined before they are used(in which case the first line of the function definition acts as the function prototype as well). Providing the prototypes avoids tying the code to the order in which functions are defined(which can easily change as a program evolves).

#include<iostream>
using std::cout;
using std::endl;
void function1();
void function2( void );
int main()
{
function1();
function2();
return 0;
}
void function1()
{
cout<<"function1 takes no argumemnts"<<endl;
}
void function( void )
{
cout<<"function2 also takes no arguments"<<endl;
}

OUTPUT:

function1 takes no arguments
function2 also takes no arguments

The meaning of an empty function parameter list in C++ is dramatically different than in C. In C, it means all argument checking is disabled(i.e. the function call can pass any arguments it wants).In C++, it means that the function takes no arguments. Thus, C programs using this feature might report syntax errors when compiled in C++

C++ programs do not compile unless function prototypes are provided for every function or each function is defined before it is called

Inline Functions in C++

Implementing a program as a set of functions is good from a software engineering standpoint, but function calls involve execution-time overhead.C++ provides inline functions to help reduce function-call overhead-especially for small functions. The qualifier inline before a function’s return type in the function definition “advises” the compiler to generate a copy of the function’s code in place(when appropriate) to avoid a function call. The trade-off is that multiple copies of the function code are inserted in the program(often making the program larger) rather than having a single copy of the function to which control is passed each time the function is called. The compiler can ignore the inline qualifier and typically does so for all but the smallest functions.

Any change to an inline function could require all clients of the function to be recompiled.This can be significant in some program-development and maintenance situations

The inline qualifier should be used only with small, frequently used functions

Example of Inline function in C++

#include<iostream>
using std::cout;
using std::cin;
using std::endl;

inline double cube( const double side )
{
return side*side*side;
}
int main()
{
cout<<"Enter the side length of your code:";
double sideValue;
cin>>sideValue;
cout<<"Volume of cube with side"
    <<sideValue<<"is"<<cube(sideValue)<<endl;
return 0;
}

OUTPUT:

Enter the side length of your cube:3.5 Volume of cube with side 3.5 is 42.875
Translate »