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)
Perl›Functions (sub)

Functions (sub)

Subroutines with sub; arguments are in the @_ array, return with return.

Simple enough for a 5-year-old to understand

sub and @_

Arguments arrive in the @_ array: $_[0] is the first, $_[1] is the second.

Example code
1
sub square {
Function definition.
FunctionFunction definition.
2
my ($x) = @_;
Get the first argument.
VariableGet the first argument.
3
return $x * $x;
Return the result.
FunctionReturn the result.
4
}
5
print square(5), "\n";
25
Output / Input25
FunctionVariableOutput / Input
💡 Hint: Arguments are retrieved from the @_ array: my ($a, $b) = @_;

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Write a sub named addUp that adds two arguments and returns the result; print addUp(8, 9).

🎯 Expected output: 17

Loading editor…

About this topic

In Perl, functions are defined with the sub keyword. Unlike many other languages, a parameter list is not written; all arguments given in the call are placed in a special array and you retrieve them from there.

This design is flexible: accepting a variable number of arguments requires no extra effort. The cost is that the signature is undocumented — you have to read the body to understand how many arguments the function expects.

There are two common ways to get arguments: unpacking the list into named variables all at once or pulling them one by one from the start. The former is preferred for readability and is the standard in modern Perl code.

What you will learn

  • Defining and calling a function
  • Accessing arguments
  • Returning values
  • The behavior of array arguments
  • Using local variables

Who is this for?

  • Those who want to organize their scripts
  • Those experiencing issues with passing arguments
  • Those preparing to write a Perl module

When to use?

If a piece of code is written a second time, it should be extracted into a function; this discipline is important because Perl scripts grow rapidly.

Functions to be used in multiple scripts should be moved to a module; copied code drifts apart over time.

Common mistakes

Wrong

sub topla($a, $b) {
  return $a + $b;
}
print topla(2, 3);

In classic Perl, a parameter list is not written this way; the expression in parentheses is interpreted as a prototype and does not work as you expect. Arguments must be retrieved from the special array. In newer Perl versions, signatures have been added as an experimental feature but they need to be explicitly enabled.

Correct

sub topla {
  my ($a, $b) = @_;
  return $a + $b;
}
print topla(2, 3);

Wrong

sub bilgi {
  my ($liste, $ad) = @_;
  print scalar(@$liste);
}
my @sayilar = (1, 2, 3);
bilgi(@sayilar, "test");

When an array is passed as an argument, it is flattened into its elements; the function takes the number 1 as the first argument and 2 as the second, breaking the expected structure. To pass the array as a whole, you must pass its reference.

Correct

sub bilgi {
  my ($liste, $ad) = @_;
  print scalar(@$liste);
}
my @sayilar = (1, 2, 3);
bilgi(\@sayilar, "test");

Wrong

sub hesapla {
  $sonuc = 42;
}
hesapla();
print $sonuc;

Since it was not declared with my, the result became a global variable; when strict is enabled this code does not fail to compile, and when disabled it silently pollutes the global namespace. Every variable inside a function should be declared locally with my and the result should be returned.

Correct

sub hesapla {
  my $sonuc = 42;
  return $sonuc;
}
print hesapla();

Frequently asked questions

Can a function return multiple values?

Yes, by returning a list. The caller can unpack the result into multiple variables. However, using the returned list in a scalar context yields a different result; the concept of context always requires care in Perl.

What happens if I don't write return?

The value of the last evaluated expression is returned. Relying on this behavior is common in short functions, but for readability writing it explicitly is preferred; especially if there is conditional branching, which value is returned becomes unclear.

Do I have to define the function before calling it?

It is not mandatory; Perl compiles the entire script, so you can call a function defined below from above. Still, keeping definitions together is a good habit for readability.

Summary

Functions are defined with sub.

Arguments are retrieved from a special array.

Array argument is flattened into elements, a reference is needed.

Variables must be declared locally with my.

If return is omitted, the last expression is returned.

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