+10 XP

Return vs Print: Critical for NMA

🚨 This is THE most important concept about functions. Getting this wrong will break your neural simulations!

πŸ“Œ Two Types of Functions: Return vs Do Something

Not every function needs return. It depends on what you want to do with the result.

🎯 Type 1: Functions that RETURN a value

python
def firing_rate(n_spikes, duration):
    """Calculate firing rate in Hz."""
    rate = n_spikes / duration
    return rate  # ← Gives the value back to use it

# Use the returned value
my_rate = firing_rate(42, 1)  # my_rate = 42.0
print(f'Rate is {my_rate} Hz')  # Now I can USE it

Use RETURN when: You need the result to use in your code (like computing voltage, spike rate, etc.)

🎯 Type 2: Functions that just DO something

python
def print_greeting(name):
    """Print a greeting message."""
    print(f"Hello {name}!")
    # No return - just performs an action

# Call it
print_greeting('Alice')  # Prints: Hello Alice!
# But you get nothing back to use

Use NO RETURN when: The function just needs to print, save, or modify something

🧠 Mental Model: Restaurant Analogy

β€’ print(answer) = Waiter SHOUTS the answer to the whole restaurant (everyone hears it, but you get nothing)
β€’ return answer = Waiter HANDS YOU the plate (you can keep it and use it)

⚑ Why This MATTERS for NMA

In neural simulations, you MUST use return because you're building a chain of computations:

1. Compute firing rate β†’ need to RETURN it β†’ use it in next calculation
2. Calculate voltage β†’ need to RETURN it β†’ use it to check for spikes
3. Simulate one time step β†’ need to RETURN state β†’ use it in next iteration

python
# NMA example: LIF Neuron simulation
def lif_step(V, I, dt, tau=20, V_rest=-70, R=10):
    """Simulate one time step of a neuron.
    
    Must RETURN the new voltage because the next iteration needs it!
    """
    dV = (-(V - V_rest) + R * I) / tau  # change in voltage
    V_new = V + dV * dt                  # new voltage
    return V_new  # ← CRITICAL! Next iteration uses this

# Use it in a loop
V = -70  # start at rest
for t in range(1000):
    V = lif_step(V, I=2, dt=0.1)  # ← MUST return V for next iteration
    if V > -55:  # threshold
        print(f'Spike at t={t}')

If lif_step didn't return V, the loop would break! Each step needs the previous step's result.

πŸ“‹ Quick Rule of Thumb

❓ Computing something? β†’ Use return
❓ Just doing something? β†’ Don't need return

If you're building a chain of calculations (like neural simulation), always return.