#!/usr/bin/perl use strict; use warnings; my $variable=total(1,2,3); print"the variable is :$variable\n"; sub total { my $element=0; foreach (@_) { $element=$element+$_; } element; } Output is: The variable is : 6 Now i changed the code to #!/usr/bin/perl use strict; use warnings; my $variable=total(1,2,3); print"the variable is :$variable\n"; sub total { my $element=0; foreach (@_) { $element=$element+$_; } element; print"the value of element is:$element\n"; } The output is the value of element is:6 the variable is :1 why the variable in this case is not showing as 6 is my question.