+ 5
In Java, C and C++, programs are compiled into machine code which doesn't have any concept of functions, callers or anything we call programming. everything is just a procedural, unreadable mess. so nothing really "calls" main. main is just the first thing to run when the program is compiled and executed
25th Aug 2018, 8:54 AM
Aidan Haddon-Wright
Aidan Haddon-Wright - avatar
+ 2
I know what you mean but that's not the right way to think about it. when a program is compiled, everything is procedural and functions don't exist in the way we think of them. instead, when you want to call a function, you just "jump" back to the first instruction that makes up that function and the computer will carry on executing each instruction that comes after no matter what. When you pass parameters, what really happens is the parameters get pushed onto the stack. Then when the program jumps to the function, it now has access to those values and it can just pop them off one at a time and use them in the following instructions. returning values works in a similar way except the value is pushed into a register on the CPU So there is no 'call', it's all sequential. the code in main just ends up at the top of the program and is run first
2nd Sep 2018, 6:54 AM
Aidan Haddon-Wright
Aidan Haddon-Wright - avatar