0

I've been working with functions in C++ and I dont realy know what really is going on behind them( how the wheels spin :) ). What exactly i wanna know is:

  1. how the parameters of a function get the values? ex:
#include<iostream>

int function(int x)
{
    x += 10;
    return x;
}


int main()
{
    int y;
    std::cin >> y;
    std::cout<< function(y);
}

In reality the function parameter x will get the value y like this: x=y ?

  1. Based on the first question, will pass by referenece be done like this &x=y? y will be converted from int to int& ???
#include<iostream>

void function(int& x)
{
    x += 10;

}


int main()
{
    int y;
    std::cin >> y;
    function(y);
    cout<<y;
}
  1. When we call a function how much memory will be allocated for it?how much will it consume? based on what?

I need to know everything that happens when a function is called , and how the function really work

Sorry for my bad english , I'm Romanian. Thank you!

Robert
  • 71
  • 8
  • You seem to be asking multiple questions here. Are you asking what the language rules are about function calls? Or the assembly that gets generated for such a program? – cigien Nov 05 '20 at 12:38
  • I am asking for the basic stuff, just to understand pass by ref + function parameters – Robert Nov 05 '20 at 12:42
  • 1
    None of what's asked here is really something that you need to know in order to understand C++. It is ***your compiler*** that needs to all these things. The only thing you need to know is what these things ***mean***. – Sam Varshavchik Nov 05 '20 at 12:48
  • Ok, I've marked the question as a duplicate of the question I think you're asking. – cigien Nov 05 '20 at 12:50
  • Consider: You can build your code (both by-value, and by-reference), launch it in the debugger (mine is gdb) and inspect the assembly (in gdb, use "disassemble /m"). – 2785528 Nov 05 '20 at 12:59

0 Answers0