educative.io

Called versus calling Methods

What is the difference between a called method and a calling method?

Hi @Ikenna_Okoro,
The terms “called method” and “calling method” refer to different roles that methods or functions can play in a program’s execution. These terms are often used in the context of function or method invocation:

1. Calling Method (Caller):

  • The calling method, also known as the “caller” or “parent method,” is the method or function that initiates or triggers the execution of another method.
  • It typically contains the code that makes a function or method call to achieve a specific task.
  • The calling method passes arguments or parameters to the called method, if necessary, and then invokes the called method.
  • After the called method completes its execution, control is usually returned to the calling method.

2. Called Method (Callee):

  • The called method, also known as the “callee” or “child method,” is the method or function that is invoked or executed in response to a call from another method.
  • It performs a specific subtask or operation and may return a value or produce a side effect.
  • The called method receives arguments or parameters from the calling method, if specified, and executes its logic based on those inputs.
  • Once the execution of the called method is complete, control is typically returned to the calling method.

Example in Python:

def main():
    result = add(2, 3)  # main is the calling method
    print(result)

def add(a, b):
    return a + b  # add is the called method

main()  # main calls add

In summary, the calling method initiates the execution of the called method by making a function or method call. The called method performs a specific task and may return a result to the calling method. These roles help organize and modularize code, making it more readable and maintainable.

Thank you Komal_Arif for the insight provided.


Course: Learn Java from Scratch - Free Interactive Course
Lesson: Constructors

1 Like