From the course: Perl 5 Essential Training
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Defining and calling functions - Perl Tutorial
From the course: Perl 5 Essential Training
Defining and calling functions
- [Voiceover] In Perl, a function is defined using the sub keyword. Here's a working copy of hello.pl from Chapter 10 of the Exercise Files. Let's start by creating a simple function. I'm gonna delete that line, and instead, I'm going to create a function called hello. In the body of the function, I'm going to say this is the hello function. So, this is a simple function. It has one statement. The name of the function is hello. The body is a block in curly braces, and it's called using the function call operator. So, I say hello with two parenthesis. That is the function call operator. If I had parameters, I would put parameters inside the parenthesis, but this is very simple. So, when I save this and run it, the function is called, and it displays the message. So, this is a very simple example of a function. We'll add to this with parameters and return values and other features as we continue in this chapter.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.