Lesson 13 of 55
9 mins readPython Function Definition & One-Liner Compact Syntax
In Plain English
Functions are fundamental building blocks in Julia. Define them with standard function ... end blocks or elegant mathematical one-liners like f(x) = x^2 + 1.
Deep Dive: How It Works
Block Syntax: Standard functions use function name(args) ... end. The value of the last evaluated expression is automatically returned (return keyword is optional).
Compact One-Liner: In Julia, f(x) = 2x + 1 is full, first-class function definition syntax identical to block functions.
Return Keyword: Use return x for early returns from conditional branches.
Core Rules to Remember

Implicit Return: The value of the last expression in the function body is returned automatically.

Compact Mathematical Syntax: Define functions cleanly in one line: f(x, y) = sqrt(x^2 + y^2).
Live Interactive Example
Hit Run Code to see it liveStandard and Compact Function Definitions
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Output Console
Click "Run Code" to view the rendered output.
How it works: Both block functions and one-liners compile to identical high-performance LLVM machine code.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Define Quadratic Formula One-Liner
Define a one-liner function `quadratic(x) = 2x^2 + 5x - 3`.
Call `quadratic(3)` and print `"Result: "` followed by the output.
1
2
3
4
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.