Pshyotic

Need explanation about some things in C++

Recommended Posts

Hello everbody, I first want to say that I couldn't find the corresponding section of the forum to post and ask things about programming, so I hope here will be okay and I think that this forum should have section for this purpose.

So, I really don't get mean of inline function in C++ ,  .. Here is one of the codes for this:

1:   // Listing 5.9 - demonstrates inline functions
2:
3:   #include 
4:
5:   inline int Double(int);
6:
7:   int main()
8:   {
9:     int target;
10:
11:    cout << "Enter a number to work with: ";
12:    cin >> target;
13:    cout << "\n";
14:
15:    target = Double(target);
16:    cout << "Target: " << target << endl;
17:
18:    target = Double(target);
19:    cout << "Target: " << target << endl;
20:
21:
22:    target = Double(target);
23:    cout << "Target: " << target << endl;
24:       return 0;
25:  }
26:
27:  int Double(int target)
28:  {
29:    return 2*target;
30: }

 

 

I do not understand the purpose of using the inline function, even if some function is not declared inline function, the compiler will perform the same as the code where the inline function is, respectively, both will do the same.

 

 

And after this question, I have question about recursion, I get it how storage works and purpose of recursion, but if anyone have some code and explanation of that code is not too complicated I would like to write to me here or in messages. 

 

Thanks :)

Share this post


Link to post
Share on other sites

To avoid function call overhead you use inline function and for recursion , it's good if you use it in dynamic programming using array or some que stack or linked list ro store data of previously performed steps else your programme will end up at being at some infinite recursion point and wont be able to figure out where it started and because of that you will get a stack over flow error , ooof

 

I'll hit you up with sample code when I reach home

Share this post


Link to post
Share on other sites

To avoid function call overhead you use inline function and for recursion , it's good if you use it in dynamic programming using array or some que stack or linked list ro store data of previously performed steps else your programme will end up at being at some infinite recursion point and wont be able to figure out where it started and because of that you will get a stack over flow error , ooof

 

I'll hit you up with sample code when I reach home

 

Okay, I think I'll wait for example code, but what I get is this that inline function and recursion is good for dynamic programming... And hell so I think that the rest of my life will be reduced to dynamic programming, since it comes down to what is known as "divide and rule", yet there are many functions, classes and objects ...

Share this post


Link to post
Share on other sites

The CPU requires some time to fetch the instruction then transfer the control for each instruction in the process. And for some function, if the execution time is smaller than the time required for the processor to switch controls over to the function then the efficiency of the code with respect to time decreases a bit so for those kinds of functions inline is used.

Share this post


Link to post
Share on other sites

The CPU requires some time to fetch the instruction then transfer the control for each instruction in the process. And for some function, if the execution time is smaller than the time required for the processor to switch controls over to the function then the efficiency of the code with respect to time decreases a bit so for those kinds of functions inline is used.

 

Heh... You are a Little bit late but , because I found explanation of it...  But thank you, anyways for your time...

Share this post


Link to post
Share on other sites

The best way to look at recursion is as an alternative to iteration.  Take the string:

 

char szHello="Hello World";

 

 

Let's say you're implementing a simple print function that calls putc(char c) to display each character. Normally, you'd do it like:

 

for(i=0;szHello[i]!=0;i++)
 putc(szHello[i]);

 

 

but let's say you wanted to do it recursively (you wouldn't, but this is an example):

 

 

void print(char *szString)
{
 if (*szString)
 {
   putc(*szString);
   print(szString + 1);
 }
}

 

 

The risk with recursion over iteration is that you could run out of stack space.  So, why would you use it?  Well, say you had a data structure:

 

 

struct tree {
 struct tree *pLeft;
 struct tree *pRight;
 char bLetter;
};

 

 

Now your print function would be:

 

void print (struct tree * pTree)
{
 if (pTree->pLeft)
   print(pTree->pLeft);
 putc(pTree->bLetter);
 if(pTree->pRight)
   print(pTree->pRight);
}

 

Note that I'm assuming you're familiar with pointers.

Share this post


Link to post
Share on other sites

Sadly I don't work with cpp.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.