Lesson 19 of 55
8 mins readPython Functions: Definition, Parameters & Calls
In Plain English
In Lua, functions are defined with `function name(args) ... end` or `local name = function(args) ... end`. Lua passes arguments positionally, assigning missing arguments to `nil`.
Deep Dive: How It Works
Function Syntax: `function add(a, b) return a + b end`.
Flexible Parameter Counts: Passing fewer arguments binds missing parameters to `nil`; extra arguments are quietly ignored.
Local Functions: `local function helper() ... end` creates a function scoped to the local file/chunk.
Core Rules to Remember

Local Functions Preferred: Use `local function` to avoid polluting the global environment.

Nil for Missing Args: Unsupplied arguments default to `nil`.
Live Interactive Example
Hit Run Code to see it liveDefining and Calling Functions
Python 3.12
1
2
3
4
5
6
7
Output Console
Click "Run Code" to view the rendered output.
How it works: greet used default value idiom for missing title argument.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Create Rectangle Area Function
Define `local function area(w, h) return w * h end`.
Print `"Area: " .. area(5, 8)`.
1
2
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.