Most of the programs discussed so far are generally structured as functions call one another in a disciplined, hierarchical manner. In some cases, it is useful to have functions call itself. A function that call itself directly or indirectly (through another function) is a recursive function.

Sometimes it is simpler to implement a repeated operation using recursion instead of iteration. Consider the following requirements (print 1 till 10)

Just as we did for loop, we need to identify a situation (stopping case) that stops recursion otherwise the function will call itself for ever.

Arguments in successive calls should bring us closer and closer to the stopping case

Recursion Example

int printInteger (int i)
 if (i=10) {      //Stopping case
    return i;
 else {
   cout<< i;
 return printInteger(++i);   //Recursive call to itself

int main() {
  cout<< printInteger(i);

Any problem that can be solved recursively can be solved iteratively. Avoid using recursion as much as possible. Recursion is expensive in terms of Processor time and Memory space. Each recursive call causes another copy of function to be created.

Inline Functions

Implementing a program as a set of functions is good from a software engineering point. But function calls involve execution time overhead. C++ provides inline functions to help reduce function call overhead – especially for small functions.

Placing the qualifier inline before a function’s return type in the function definition advises the compiler to generate a copy of function’s code in place to avoid a function call. Multiple copies are inserted in the code, making the code size larger at the cost of overhead. Function prototype is not required for inline function. Function definition must be written before the main function – after the preprocessor directives.

inline void drawLine(char h)
{ . . . // what so ever 

int main(void)
int x = ‘*’;
return 0;

Tagged with: C/C++ language

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.