σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademySkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
Skills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
Python skill›Module 1 · Python foundations for quants›Topic 1.8·~40 minBeginner

Defining and calling functions

Skill positionModule 1 of 11 · Topic 8 of 33
1.8 Defining and calling functions
—Lab score— / 100Total XP—Streak—

Giving a block of code a name

A function is a named block of code you can run again and again. You define it once with def, a name, parentheses and a colon; the indented block under it is the body. return sends a value back to whoever called the function. Calling it means writing its name with parentheses and the values to work on.

def gross_value(price, quantity):
    return price * quantity

print(gross_value(184.25, 200))  # 36850.0

Parameters and arguments

The names in the parentheses of def are parameters: placeholders for the inputs. The values you pass when you call are arguments, and they fill the parameters in order. Swap them and the result changes. Passing the wrong number of arguments is an error. Each call gets its own fresh copy of the parameters.

# Arguments fill the parameters in order
def spread(bid, ask):
    return ask - bid

print(spread(184.25, 184.5))  # 0.25
print(spread(184.5, 184.25))  # -0.25

return is not print

print shows something on the screen for a human. return hands a value to the calling code so it can keep working with it. A function with no return gives back None, which means "nothing". Functions that calculate should return; the code that calls them decides whether to print.

# A function with no return gives back None
def show(price):
    print("price", price)

result = show(184.25)   # price 184.25
print(result)           # None

Small functions that build on each other

A function can call other functions, so you build big behaviour from small, testable pieces. Give each function one job and a name that says it. The docstring, a string right under def, explains it to the next reader. Define a function before the line that calls it runs.

# Small functions build bigger ones
def gross_value(price, quantity):
    """Value of a position before costs."""
    return price * quantity


def commission(gross):
    """0.1% of the value, but never less than 1.0."""
    return max(1.0, gross * 0.001)


def net_cost(price, quantity):
    """What the position really costs: value plus commission."""
    gross = gross_value(price, quantity)
    return gross + commission(gross)


print(net_cost(184.25, 200))  # 36886.85

Returning several values

return a, b hands back both values at once, as a tuple: an ordered pair written with commas (tuples get their own topic soon). The caller can name the pieces straight away. Splitting an order of 1,000 shares into 3 slices with // and % returns the size of each slice and what is left over.

# return several values as a tuple
def split_fill(quantity, slices):
    return quantity // slices, quantity % slices

size, leftover = split_fill(1000, 3)
print(size, leftover)  # 333 1
Return ends the functionPython leaves the function at the first return it reaches; any lines after it in that path never run. Parameters and any variable you create inside the function exist only during the call: outside code cannot see them (more on that in the scope topic).
  • One job per function, and a name that says what it returns.
  • Calculate and return; let the caller print.
  • Add a docstring that says what goes in and what comes out.

Next up: function arguments and defaults, to call functions with names, optional values and any number of arguments.

Lab · what's graded

  • Define a function that values a position with gross_value25 pts
  • Charge a commission with a minimum using commission25 pts
  • Build the real cost from two functions with net_cost25 pts
  • Return two values at once with split_fill25 pts
Explorer · lab-8-1
▾ lab-8-1
…
bash · in-browser
learner@quantcraft:~/lab-8-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-8-1 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes

Log in to run your code.

Every lecture is free to read. Create an account or log in to run code, run tests and submit this lab.

  • In-browser IDE with a bash terminal — nothing to install
  • Instant grading, XP and your place on the skill
Create an accountLog in
OR
Continue with GitHub