What happens when a function is called

I was learning about inline functions, and couldn't grasp the concept. Then I realised I couldn't understand it because I do not actually know what happens when a normal function is run.

I have tried to read one of the articles on here which states "When a normal function call instruction is encountered, the program stores the memory address of the instructions immediately following the function call statement, loads the function being called into the memory, copies argument values, jumps to the memory location of the called function, executes the function codes, stores the return value of the function, and then jumps back to the address of the instruction that was saved just before executing the called function. Too much run time overhead."

But I can not follow that, it seems to be very jumbled to me to try and follow. Would anyone be kind enough to give information on what happens when functions are run in a clear step by step manner. Thank you.
I won't go into that. Lets try analog:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int foo(int);

int main() {
  int bar = 7;
  int gaz = foo( bar );
  std::cout << gaz;
}

int foo( int x )
{
  return x * 2;
}

When you read that program, tracking what happens, you start from line 5.
On line 7 you have to jump to read lines 11-14 (and return) before you know what happens on line 7.
(On line 8 you would jump to line 1 and read the file "iostream" to guess the std::cout and <<.)

You have to memorize where you are and what you have before jumping to read the implementation of foo (that could be on separate file) so that you know how to continue once you know the result of foo(bar).


Lets assume that the compiler inlines foo() and shows us pseudocode:
1
2
3
4
5
6
7
8
#include <iostream>

int main() {
  int bar = 7;
  int x = bar;
  int gaz = x * 2;
  std::cout << gaz;
}

No jumping around, no keeping track of values (except for the std::cout).
Topic archived. No new replies allowed.