#!/usr/bin/perl



#  Introduction to Subroutines
#  Chapter 6 in your textbook
#
#  A note about namespaces:
#	scalars begin with a dollar sign as in $amount
#   arrays begin with an at-sign as in @array1
#   subroutines begin with an ampersand as in &sub1



# example 1 - call a subroutine with no parameters and nothing returned

use strict;
my $a;

for($a=0;$a<6;$a++)
{
	if (($a % 2) == 0)
	{
		&do_hello;
	}
	else
	{
		&do_goodbye;
	}
}


sub do_hello
{							#NOTE subroutines need openening and closing braces
	print "Hello\n";
}

sub do_goodbye
{
	print "s'long folks\n";
}




#--------------------------------------------------------------------------------------
#!/usr/bin/perl

# example 2 - the subroutine returns a value. The value returned is the value of the last
# statement of the subroutine.


use strict;
my $a;


$a=&do_something;
print "The value of A is $a\n";





sub do_something
{
	print "inside subroutine do_something\n";
	134;										# this seems a little strange. But it works!!
}


# Quiz questions!
# what does a print statement return ?For example, suppose print "Hello" ended a subroutine?



#--------------------------------------------------------------------------------------------
#!/usr/bin/perl
#example 3 - passing information to a subroutine. It is passed in the array called @_

use strict;
my $a;
my $b;
my $low;


$a=34;
$b=77;
$low=&minimum($a,$b);
print "The low value is $low\n";


sub minimum
{
	my $arg1=shift(@_);			# NOTE: this is a local declaration. It is not seen in main pgm
	my $arg1=shift(@_);
	my $retValue;

	if ($arg1<$arg2)
	{
		$retValue=$arg1;
	}
	else
	{
		$retValue=$arg2;
	}
	$retValue;
}



#--------------------------------------------------------------------------------------------
#!/usr/bin/perl



#example 4 - the keyword "my" determines the scope of a variable. If you declare a my variable
# in a subroutine, it is visible from the point od declaration until the close of the block 
# (typically a closing brace}. my variables declared in your main program are global and
# known throughout the program

use strict;
my $a;            #This is a global variable

$a=34;
print "Before subroutine call the value of A is $a\n";
&do_something;
print "After subroutine call, the value of A is $a\n";


sub do_something
{
	my $b;			#this variable is local and only visible inside sub do_something
	
	print "Inside do_something A is $a\n";
	$b=103;
	$a=99;
}