σμ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 AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμ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 AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
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—
Lecture0% read

2 min readCode examples: 5

  1. 01Giving a block of code a name
  2. 02Parameters and arguments
  3. 03return is not print
  4. 04Small functions that build on each other
  5. 05Returning several values
01

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.

Python
1
2
3
4def gross_value(price, quantity):
    return price * quantity

print(gross_value(184.25, 200))  # 36850.0
02

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.

Python
1
2
3
4
5
6# 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
03

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.

Python
1
2
3
4
5
6# A function with no return gives back None
def show(price):
    print("price", price)

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

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.

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18# 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
05

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.

Python
1
2
3
4
5
6# 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 function

Python 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

4 tasks · 100 pts
  • 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