Home / Visual Basic Tutorial / Visual Basic Functions

Visual Basic Functions

A function is a preprogrammed calculation .It can be carried out on request from any point in a Visual Basic program.
A function takes one or more arguments and returns a single value and it can be included in an expression.

Argument:
It is a value on which a function or procedure operates.
For example, in the Visual Basic statement Str(10),
number 10 is the argument.

Visual Basic includes built-in functions like Sqr, Cos or Chr etc.We are discussing some of them here:

(1) Sqr()
Returns a Double specifying the square root of a number.

Syntax
Sqr(number)

(2) ABS()
Returns the absolute, positive value of the given numeric expression.

Syntax
ABS(numeric_expression)
Arguments
numeric_expression
Is an expression of the exact numeric or approximate numeric data type category.
Return Types
Returns same type as numeric_expression.

(3) POWER()
Returns value of given expression to the specified power.

Syntax
POWER(numeric_expression, y)
Arguments
numeric_expression
Is an expression of the exact numeric or approximate numeric data type category.
y Is the power to which to raise numeric_expression. y can be an expression of the exact numeric or approximate numeric data type category.

(4) FLOOR ()
Returns largest integer less than or equal to the given numeric expression.

Syntax
FLOOR(numeric_expression)
Arguments
numeric_expression
Is an expression of exact numeric or approximate numeric data type category. Return Types
Returns the same type as numeric_expression.

For Example:
FLOOR(122.45), Return value=122
FLOOR(-123.45) ,Return value=124

(5) Round()
Returns a numeric expression, rounded to the specified length or precision.

Syntax
ROUND (numeric_expression, length)
Arguments
numeric_expression
Is an expression of the exact numeric or approximate numeric data type category.
length Is the precision to which numeric_expression is to be rounded. length must be int.
When length is a positive number, numeric_expression is rounded to the number of decimal places specified by length.
When length is a negative number, numeric_expression is rounded on the left side of the decimal point, as specified by length.
Return Types
Returns the same type as numeric_expression.

For Example:
Round(123.4545,2) ,Return Value=123.4500

(6) SQUARE()
Returns the square of given expression.

Syntax
SQUARE(float_expression)
Arguments
float_expression
Is an expression of type float.
Return Types
float

(7) PI ()
Returns the constant value of PI.

Syntax
PI()
Return Types
float

(8) SIGN ()
Returns the positive (+1), zero (0), or negative (-1) sign of the given expression.

Syntax
SIGN(numeric_expression)
Arguments
numeric_expression
Is an expression of the exact numeric or approximate numeric data type category.
Return Types
Float

(9) LOG10 ()
Returns the base-10 logarithm of the given float expression.

Syntax
LOG10(float_expression)
Arguments
float_expression
Is an expression of the float data type.
Return Types
Float

(10) RAND ()
Returns a random float value between 0 and 1.

Syntax
RAND([seed])
Arguments
seed
Is an integer expression (int) giving the starting value.
Return Types
float