Saturday, October 30, 2010

Defining Functions In Infobasic

Functions can also be defined in Infobasic. These functions can take in any number of parameters but return only one value. Let us understand it with a simple example.

Example

we will create a function that will accept 2 integer values, multiply them and return the result in a variable.
FUNCTION DEMO.FUNCTION(ARG.1,ARG.2)
RET.VALUE = ARG.1 * ARG.2
RETURN(RET.VALUE)
END
The RETURN statement is used to return a value in a function.

Now the function defined above can be called from any program/subroutine. Now create a subroutine that would supply values and call the above-defined function. When calling a function, the function needs to be defined first using the DEFFUN command and only then the function should be called.
SUBROUTINE DEMO.SUB.CALLING.RTN
VAR.1 = 10
VAR.2 = 20
DEFFUN DEMO.FUNCTION(VAL.1,VAL.2)
VAR.3 = DEMO.FUNCTION(VAR.1,VAR.2)
CRT "Result : "VAR.3
RETURN
END
Incase of a function, only the incoming parameters need to be defined. There can and will be only one return value and that will directly get assigned to the variable on the right side of the equation as defined above.

Un-initialized variables VAL.1 and VAL.2 have been given to demonstrate that any variable can be given at the time of defining a function.

0 comments:

Post a Comment

 

Blog Info

A Pakistani Website by Originative Systems

Total Pageviews

Tutorial Jinni Copyright © 2015 WoodMag is Modified by Originative Systems