Perl | abs() function

This function returns the absolute value of its argument. If a pure integer value is passed then it will return it as it is, but if a string is passed then it will return zero. If VALUE is omitted then it uses $_

Syntax: abs(VALUE)

Parameter:
VALUE: It is a required number which can be either positive or negative or a string.

Returns: Function returns the absolute value of the arguments passed.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Defining decimal value
$var1 = 15.8;
  
# Defining integer value
$var2 = 7;
  
# Defining negative value
$var3 = "-15.2";
  
# Calling abs() function
$res1 = abs($var1);
$res2 = abs($var2);
$res3 = abs($var3);
  
# Printing these values
print "Abs value of var1 is $res1\n";
print "Abs value of var2 is $res2\n";
print "Abs value of var3 is $res3";

chevron_right


Output:

Abs value of var1 is 15.8
Abs value of var2 is 7
Abs value of var3 is 15.2

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Defining string value
$var1 = "Geeks";
  
# Defining Fractional value
$var2 = 7/2;
  
# Calling abs() function
$res1 = abs($var1);
$res2 = abs($var2);
  
# Print these values
print "Abs value of var1 is $res1\n";
print "Abs value of var2 is $res2";

chevron_right


Output:

Abs value of var1 is 0
Abs value of var2 is 3.5


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.