How function calls and return statements work at machine level?

I am trying to understand how a function call and returning from function works at machine level. From Microsoft’s documentation and many other resources define returning from a function like this:

A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function.

But I could find none that gave a detailed explanation on the low level working of return statements and function calling.I watched a Youtube video (it was the only one which i could find that discussed this at machine level). It was for c but I think the execution should be similar in c++.

Let say I have two test codes as follows (Assuming no optimization is being done by the compiler):

  1. This one should have common execution in both c and c++
int square(int i){
   return i*i;
}
int main(){
    int num = square(7);
    return 0; 
}
  1. C does not have classes. So this code is for c++ only. Assuming a class called MyObj is already defined and is big enough that it can neither be stored in a single register nor in multiple registers.
MyObj func(MyObj obj_recieved){
   //...
   //Does some operations on obj_recieved
   //...
   return obj_recieved;
}
int main(){
    MyObj obj1;
    MyObj obj2 = func(obj1);
    return 0;
}

How will the execution look at machine level of the above two codes?

From what I could learn through the video and searching over the internet. I think for the first code it should go like this:

  1. An integer variable num is created on stack.

  2. The function square() is executed with 7 as its argument and for that following steps will occur.

    • A pointer is saved on the stack for the return address. In this case the return address will be the address of num variable.
    • A stack frame is created on the stack for the square() functions and the argument 7 gets copied and stored in this stack frame.
    • Stack pointer now moves to the function square().
    • An instruction for multiply command is executed and 7 gets multiplied with 7
    • The result of multiply instruction which will be 49 is to be returned and this gets stored in a register ( but this can vary with CPUs).
    • The stack frame created for square() gets cleaned up.
    • The stack pointer now moves back to the main() function and to the return address which is address of num
  3. The returned value is stored in num variable.

  4. A return statement is encountered which returns 0, so 0 gets stored in a register.

  5. The stack for main() gets cleaned up and program execution finishes. The OS should handle the rest including the returned value 0 inside the register.

Is this the correct way the CPU does the execution for code 1 for both c and c++?

For 2nd code I got stuck on sub steps of point 2 that I have written in the execution for code 1.
When stack pointer reaches obj2, CPU will do the same thing as it did for code 1. obj1 will be copied and stored in obj_recieved inside the stack frame of func(). At the end of execution a copy of this obj_recieved should be returned but where will it store the copy of obj_recieved?

New contributor

Divyansh Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật