HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to PerlOperatorsStringsArrays (@)Hashes (%)Conditions (if)LoopsFunctions (sub)
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
✍️ Text & Number
📋 Array & Hash
🔁 Condition & Loop
🧩 Function (sub)
sub
Perl›REFERENCE›Function (sub)›sub

sub

Defines a function (subprogram).

Syntax

sub name { ... return ...; }

Description

Defined with sub. Arguments come in the @_ array: $_[0] is the first, $_[1] is the second. Values are returned with return.

Example

Loading editor…

Expected output

17

When to use?

Preventing code repetition.

Common mistakes

  • ⚠️ Arguments are retrieved from the @_ array: my ($a, $b) = @_;

Related commands

foreach / for
HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code