Subroutines #2

#0 | #1 | #2 | #3 | #4

Using the return value

As mentioned, subroutines are used whenever you have a particular task that is quite common, and rather than writing or copying the same batch of code over and over, we can put that code into a special block of code called a subroutine.

If you have spent some time programming, you will realize that a lot of programming is made up of similar tasks which you will likely need to do over and over.

These are good candidates for making a subroutine. One of the things I tend to need is the current date.

One of the things you can do with a subroutine is assign a variable to it.

Every subroutine has a return value.

The return value is the value of the last expression evaluated.

So, knowing that a subroutine returns a value, how can we use that? Here is one way I use it.

As mentioned, I often have a need for the current date. I use this several ways: inserting it into a database; using it in a filename; putting it into a note or email; etc.

Here is how mine looks:

sub GetToday
{
    my ($sec, $min, $hour, $day, $month, $year)=(localtime)[0,1,2,3,4,5];
    my $thisyear = $year+1900;
    my $thismonth = $month+1;
    $thismonth = "0$thismonth" if ($thismonth < 10);
    $day = "0$day" if ($day < 10);
    $min = "0$min" if ($min < 10);
    $sec = "0$sec" if ($sec < 10);
    $hour = "0$hour" if ($hour < 10);
    my $Now="$thisyear-$thismonth-$day $hour:$min:$sec";
    my $Today="$thisyear-$thismonth-$day";
    return $Now;
}

Again, we first set up some variables used in the subroutine: ($sec, $min, $hour, $day, $month, $year)

However, we assign them to the result of running the Perl function localtime.

The variable $year returns the number of years since 1900, so we need to correct for that by adding '1900'.

Also, $month returns a value in the range '0 .. 11' (since it's an array). That has to be corrected as well.

In my version of getting the current date and time, I like to have 2-digit values for months, days, hours, minutes, and seconds. In other words, leading zeroes when the value is less than 10.

This is one way to do it:

    $thismonth = "0$thismonth" if ($thismonth < 10);
    $day = "0$day" if ($day < 10);
    $min = "0$min" if ($min < 10);
    $sec = "0$sec" if ($sec < 10);
    $hour = "0$hour" if ($hour < 10);

Using sprintf would be another way.

Then we set 2 more variables to make up different formats of the date and time ($Now and $Today).

The last step is to return a value - since this version has 2 values, you have to choose 1 to use in the return, depending on which format you want.

You might notice that the strings to make up our different formats contain a "-", which normally might be considered as a mathematical minus symbol. But in this case (the use of double-quotes in the subroutine), it is simply a hyphen.

Now that we have it finished, does it work?

#!/usr/bin/perl
use strict;
use warnings;

my $today = GetToday();

print "\n\n$today\n\n";

sub GetToday
{
    my ($sec, $min, $hour, $day, $month, $year)=(localtime)[0,1,2,3,4,5];
    my $thisyear = $year+1900;
    my $thismonth = $month+1;
    $thismonth = "0$thismonth" if ($thismonth < 10);
    $day = "0$day" if ($day < 10);
    $min = "0$min" if ($min < 10);
    $sec = "0$sec" if ($sec < 10);
    $hour = "0$hour" if ($hour < 10);
    my $Now="$thisyear-$thismonth-$day $hour:$min:$sec";
    my $Today="$thisyear-$thismonth-$day";
    return $Today;
#    return $Now;
}

Yes, it does:

#!/usr/bin/perl
use strict;
use warnings;

my $today = GetToday();

print "\n\n$today\n\n";

sub GetToday
{
    my ($sec, $min, $hour, $day, $month, $year)=(localtime)[0,1,2,3,4,5];
    my $thisyear = $year+1900;
    my $thismonth = $month+1;
    $thismonth = "0$thismonth" if ($thismonth < 10);
    $day = "0$day" if ($day < 10);
    $min = "0$min" if ($min < 10);
    $sec = "0$sec" if ($sec < 10);
    $hour = "0$hour" if ($hour < 10);
    my $Now="$thisyear-$thismonth-$day $hour:$min:$sec";
    my $Today="$thisyear-$thismonth-$day";
#    return $Today;
    return $Now;
}

To see the other format, change the code in the subroutine to read 'return $Now' instead of 'return $Today':

As you can see, all values are 2 digits.

Now that you have a working subroutine (and a useful one at that), you can use it in any of your scripts by including the subroutine code in your script, then calling it wherever you need it.

More ways to use subroutines