Introduction to Empty Parameter list and Inline Functions

Functions with Empty Parameter Lists:We have already used the concept of empty parameters in some of our examples. Empty parameter list are used to indicate to the compiler that the function will receive and use any arguments in executing its statements. This empty parameter list function is normally written as a void function to indicate to compiler that the function will not return a value to the calling function. To write an empty parameter list, you indicate the return type (which normally is void), followed by the identifier/name and empty braces, e.g void myName();

#include<iostream>
#include<string>
using namespace std;
void myName() //empty parameter function declared
{ string name="Stallen"; cout<<"My name is "<<name <<endl; //function myName() will only print name when called }//end myName()
}
int main() //start main()
{
cout<<"Please note that "; myName(); //myName() simply prints to complete the sentence
cout<<endl;
return 0; }
Please note that My name is Stallen

Inline functions

An inline function is used to force the compiler to write and insert the function definition at the point where it is called rather waiting for function to return with a value. This is done so as to enable the program execute faster than it would if it was to keep calling and waiting for functions to return. An inline function is declared normally but with an “inline” key word at the beginning. We modify our example above as follows

#include<iostream>
#include<string>
using namespace std;
inline string myName(const string name) //use of the key word “inline” makes the compiler to write this function at any point is called
{ string Fname=name; return Fname; }//end myName()
int main() //start main()
{ cout<< "Please note that my name is "<<myName("Stallen")<<endl; //The compiler writes here the definition of myName() instead of calling for it to return a value
return 0; } //end main()
Please note that my name is Stallen
note that in our program we have introduced the key word “const”. It is used to inform the compiler that the value entered should not be modified

Inline function that are meant to be re used should be placed in the header. If observe clearly you will note that we defined the function before main(). The purpose is to let the compiler know in advance how to work with the inline function myName().

While the inline functions can reduce the execution time, if the many points where they are called, it can result in a very large program size. For this reason, it is advisable to use inline functions in small programs.
PREV:Introduction to rules of names or identifiers


NEXT:Introduction to passing by value and by reference

1 comment:

  1. Yar give some space in words or change your them and swift to wordpress.com
    you will get more comfort there i am writting C there
    https://vipin711.wordpress.com/

    and
    Thanks your content was help full for me.

    ReplyDelete