Open In App

Perl | abs() function

Improve
Improve
Like Article
Like
Save
Share
Report

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:




#!/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";


Output:

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

Example 2:




#!/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";


Output:

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


Last Updated : 25 Jun, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads