Skip to main content

Command Palette

Search for a command to run...

CallBack Function

Published
2 min read
R

🎓 B.Tech CSE | 👨‍💻 Learning DSA & C++ | 🚀 Building projects & writing what I learn | 📚 Currently solving LeetCode & exploring Git/GitHub

Function Terms: Parameter vs Argument

1. Parameter

  • Definition: A variable written inside the function definition.

  • Role: Acts as a placeholder for input.

  • Location: In the function header.

2. Argument

  • Definition: The actual value/data you pass when calling the function.

  • Role: Fills the placeholder (parameter) during execution.

  • Location: In the function call.

[!Example]

# Function definition
def greet(name):        # 'name' -> parameter ( placeholder variable )
    return "Hello, " + name

# Function call
print(greet("David")) # "David" -> argument ( actual value )
  • Parameter = placeholder in function definition.

  • Argument = real value passed during function call.


CallBack function

A callback is simply:

a function you give to another function, so that the other function can decide when to call it.

That’s it

1. function(callback)
  • Here, you are passing the function itself (the reference) as an argument.

  • The outer function can then call it later whenever it wants.

  • This is the essence of a callback: you give a function to another function, and it decides when/how to execute it.

[!Example]

def executor(cb):
    print("Before callback")
    cb()   # calling the callback inside
    print("After callback")

def say_hello():
    print("Hello!")

executor(say_hello)   # pass the function reference

Output:

Before callback
Hello!
After callback

2. function(callback())

  • Here, you are executing callback() immediately and passing its return value to the outer function.

  • The outer function does not receive the function itself, only the result of calling it.

  • This means the outer function cannot decide when to call the callback — it only gets the result.

[!Example]

def executor(value):
    print("Got:", value)

def say_hello():
    print("Hello!")
    return "Done"

executor(say_hello())   # pass the result of calling say_hello

Output:

Hello!
Got: Done