Lesson 31 of 55
9 mins readPython Subroutines: Definition & Arguments (@_)
In Plain English
Perl functions are called subroutines, defined with `sub name { ... }`. All parameters passed to a subroutine are automatically stored in the special array `@_`.
Deep Dive: How It Works
sub Keyword: Declares a named subroutine.
Special Argument Array (@_): Holds all incoming positional parameters.
Unpacking Idioms: `my ($a, $b) = @_;` or unpacking one by one via `my $arg = shift;`.
Core Rules to Remember

@_ Argument Array: Arguments are passed as a flat list into `@_`.

Unpacking with shift or List: `my ($x, $y) = @_;` is the most common parameter unpacking idiom.
Live Interactive Example
Hit Run Code to see it liveDefining and Calling Subroutines
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Output Console
Click "Run Code" to view the rendered output.
How it works: add_numbers unpacked ($a, $b) and greet_user shifted the first parameter.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Create Multiply Subroutine
Define a subroutine `sub multiply { my ($x, $y) = @_; return $x * $y; }`.
Print `"Product: " . multiply(6, 7) . "\n"`.
1
2
3
4
5
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.