Function(1)

박영재·2024년 9월 24일
0

Function abstracts a sequence of behaviors as its signature

  • Function signature is exposed part

    • Name: represents the behavior sequence
    • Return type: result and/or confirmation of its behaviors
      • void: caller only requires callee’s behaviors without any return, not caring whether callee succeeds or fails
    • Parameters: data provided by caller to achieve callee’s behaviors
  • Function body is hidden part

    • Implementation of the sequence of behaviors is flexible



By treating a function as a protocol, we can achieve modular programming
  • Alice writes the main function (caller) and requires a module consisting of specific behaviors
  • Alice delegates Bob to implement a function (callee) that defines these behaviors (Function body)
  • To collaborate, To collaborate, Alice and Bob agree on the inputs (parameters) needed for the callee and the output (return value). These parameters and return value can be seen as an interface.



Call Stack (x86)

  • A function involves its local variables, parameters, and return value
  • These are stored and managed in the call stack
  • Each thread has its dedicated stack whose size is 1MB (by default)
  • A call stack consists of stack frames

When a function is called, its corresponding stack frame is created and pushed onto stack

  1. Local variables are pushed into stack in declaration order
  2. An array is pushed into stack from its last element
  3. parameters are pushed into stack from left to rights
  4. etc …



Calling Convention

profile
People live above layers of abstraction beneath which engineers reside

0개의 댓글