3

I'm trying to understand a little bit more about inline functions. The problem is that i don't think i understand how does 'normal' functions works. Could someone explain:

  • What happens when you call a function?

enter image description here


The picture above illustrates how i understand a function call. When you call it, the 'thread will go' to the function (going to) , will execute (process), will return (returning). But i'm not sure is correct, and i would like to know what is happening between main passing the thread to the function and the function doing it's stuff. Analog for the other way (returning).

Here I've found something related. But I did not understand quite well. Thanks!

P.s a draw would help!

Cătălina Sîrbu
  • 1,253
  • 9
  • 30
  • 5
    How low do you want to do? Assembly level? How the assembly instructions work? The CPU microcode being executed? – Some programmer dude Dec 27 '19 at 15:52
  • `Calling a function generally causes a certain overhead (stacking arguments, jumps, etc...), and thus for very short functions, it may be more efficient to simply insert the code of the function where it is called, instead of performing the process of formally calling a function.` The "stacking arguments jumps, etc" is all i can get from cplusplus.com. I would like something in that direction, but more explicit! – Cătălina Sîrbu Dec 27 '19 at 15:53
  • You might have better luck if you try a)compiling some sample code to assembly, e.g. using godbolt.org and b) reviewing some calling conventions, which stipulate how functions are to be called (and thus provide some of the info you want). – nanofarad Dec 27 '19 at 15:55
  • Depending on the calling convention, you have to push values onto the stack, or move into registers (which might require pushing to save values needed). Have a look [here](https://en.wikipedia.org/wiki/Calling_convention) and [here](https://en.wikipedia.org/wiki/X86_calling_conventions) – ChrisMM Dec 27 '19 at 16:03
  • 2
    there's a similar question [here](https://stackoverflow.com/q/144993), info about [stack vs heap](https://stackoverflow.com/q/79923) (though only the stack part is relevant here), and a more complicated example with a large struct [here](https://stackoverflow.com/q/39068492). I hope they help! :D – kmdreko Dec 27 '19 at 16:08
  • 1
    `inline` is just a *hint* to the compiler (like `register`). Read about [inline expansion](https://en.wikipedia.org/wiki/Inline_expansion). See [here](https://en.cppreference.com/w/cpp/language/inline) for more. See also [n3337](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf) – Basile Starynkevitch Dec 27 '19 at 16:10
  • That is brilliant @kmdreko thanks! – Cătălina Sîrbu Dec 27 '19 at 16:12
  • The *term* inline can be consider as substitution. The call to the function is replaced by the contents of the function, substituting the call with the contents of the function. The keyword `inline` in C++ is a hint or a suggestion to the compiler. – Thomas Matthews Dec 27 '19 at 19:07
  • To be honest, technically it doesn't matter. How functions are called is an implementation detail and could differ quite a lot between target platforms. A stack could be used for arguments, or CPU registers, or something else completely. Same with returned values. And how the function is actually called by the compiler. The normal description you will find (with stacks and jumps etc.) are common for the "normal" CPU used in PC-like systems (x86, ARM, or similar), but it's not the only way to do it. – Some programmer dude Dec 28 '19 at 10:50

1 Answers1

4

My guess is that you're looking for a simpler explanation than the one you just read on Quora. Here it is for you.

In mathematics, a function f(X) is a sequential set of transformations on X which gives you a completely different output (usually called Y) A function is often mathematically described. For example, let there be a function f(X) = (8*X + 5) So for every input, there's gonna be a output. Like, f(5) will be 45 on computation. In terms of programming, This 45 is the returned value (Y)

Whenever you are trying to use a function, you're calling that function for a specific input.

I could get into the details of why a program slows down when you call multiple functions but that was explained in the link you provided. So to keep things simple, imagine if I ask you to calculate something simple once. You'll do it right away. But if I ask you to do a thousand simple calculations, though you'll do it, it'll still take you quite a bit of time. It's not exactly the same with computers, but it's somewhere along the lines.

There are some functions which don't actually return anything at all. In Java they're referred to as void functions. To understand this, let's take another example. Let f() be your function, and let it print out 5 lines to your console.

Now whenever you try to call a void function, you can think of it like you've replaced those 5 print statements by one function call.

Say it takes an input now f(X) and it prints out X+18 Since it doesn't return anything, you can't assign f(X) to a variable. But it does print things, so whenever you call it in your code, it basically just executes the statement that prints out (X+18) to your console. In case this isn't clear enough, say you define a void function g(X) to a block of code. Now whenever you call it, you can think about replacing g(X) with that block of code.

Hopefully this helps

EDIT: It seems like you've edited your question, so here's an edited answer for you.

Now, a fair warning - the explanation I'm about to give you is not a technical one. My teacher and one of my colleagues used this explanation for me when I was questioning them about the stack and functions for the first time so it's gonna be in layman terms.

Imagine a huge container (or a box), and let's call it A The container/box is open from the top. Any item you put inside the box, goes down to the bottom due to gravity.

Suppose you put an item B inside of the container A. Then you add Cand then D

Since the box is only open from the top, the last item that you put in is the only one you can access first. And only when you take out this item, can you go on to access the item below it. So you'll first have access to D, when you take out D, you'll have access to C, and then finally when you take out C you'll have access to the lowest item B

The container A is basically a stack. The you can think of the container as a box of memory (not physically but figuratively) it's basically just free memory space.

Whenever you invoke the main function, it gets pushed in the box or as we call it now, stack. The main( ) function is at the bottom of the stack now. You can relate this to the item B from our above example. Now suppose you have a function f(X) And within your main( ) function you're calling f(5)

Calling f(5) creates a new scope for the function where the value of the passed in variable (X) is now set to 5

This entire thing - the function definition, any other variables you've created within the function, and the value of the passed in variable - is called a "stack frame". The stack frame will take up some space in our stack/container. This stack frame also corresponds to the next item in the container, and gets "stacked" over the main method. You can think of it as C from the previous example.

Now comes in the part I said about removing items from the container/stack. Removing corresponds to computing and returning the value. Considering the presence of void functions, a better term would be that removing refers to execution of a function. The first item that needs to be removed is C or f(5) and then B or main( )

Now coming to a simple answer to why things slow down when you're calling a lot of functions. There's one simple answer. Your container doesn't have an infinite capacity.

If you keep filling up container A with items B, C, the items will obviously occupy space. Eventually if you go on adding items D, E, F, G, H... there'll be a point when your container is full, after that if you try to put in more items, your container will "overflow" Basically when you call a lot of functions, your container/stack keeps filling up, eventually leading to a stack overflow.

So calling a function creates a scope of its own, takes all the information related to the function (definition, passed variables, other variables etc), puts it in a stack frame and then stacks the stack frame.

Hopefully it's all clear now... And since this question has been put on hold, if you still have doubts feel free to leave another comment and tag me in it or contact me somehow :)

PS: I've used a lot of layman language here to explain this, so use this as a general way to visualise what's going on, and refer to technical definitions if you're studying this for an exam

EDIT I've edited it to be an example with just one single function f(5) so that it's easier to understand what I'm saying. The earlier one was slightly incorrect as well

Akshath Mahajan
  • 248
  • 2
  • 11
  • 1
    i'm sorry but this does not help me. Have you seen my modifier question ? – Cătălina Sîrbu Dec 27 '19 at 16:09
  • I don't think this is at all what OP is asking. I think OP understands the purpose on functions, s/he is asking what the happens during a function call. – ChrisMM Dec 27 '19 at 16:09
  • 1
    No.. it actually took me a bit to type and as soon as I hit post, I saw that you've edited your question. I'm on my mobile right now but I've favorited your question and will get back to it as soon as I'm on my laptop again. Thanks a ton for the upvotes... – Akshath Mahajan Dec 27 '19 at 16:11
  • @CătălinaSîrbu please check out https://www.tenouk.com/Bufferoverflowc/Bufferoverflow2.html If that's not what you're looking for lemme know once again – Akshath Mahajan Dec 27 '19 at 16:40
  • @AkshathMahajan it looks like what i'm searching for, but believe me i don't understand . I asked on stack because here are some 'user friendly explainations'. That was what i was looking for ( e.g. your answer ) – Cătălina Sîrbu Dec 27 '19 at 20:24
  • @CătălinaSîrbu Hey again! I can actually explain it to you in a very user friendly manner, but unfortunately your question has been put on hold. Gimme a few minutes, I'll just try to edit my previous answer or write it in the comments :) I just got caught up with some work, sorry – Akshath Mahajan Dec 27 '19 at 20:27
  • Take your time. It would be great! – Cătălina Sîrbu Dec 27 '19 at 20:31
  • 1
    I wanted to thank you but i think you get it from the upvote and accepted answer :) Thanks! – Cătălina Sîrbu Dec 28 '19 at 12:23