How to resolve the algorithm Function definition step by step in the BASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Function definition step by step in the BASIC programming language
Table of Contents
Problem Statement
A function is a body of code that returns a value. The value returned may depend on arguments provided to the function.
Write a definition of a function called "multiply" that takes two arguments and returns their product. (Argument types should be chosen so as not to distract from showing how functions are created and values returned).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Function definition step by step in the BASIC programming language
Source code in the basic programming language
DECLARE FUNCTION multiply% (a AS INTEGER, b AS INTEGER)
FUNCTION multiply% (a AS INTEGER, b AS INTEGER)
multiply = a * b
END FUNCTION
10 DEF FN MULTIPLY(P) = P(P) * P(P+1)
20 P(1) = 611 : P(2) = 78 : PRINT FN MULTIPLY(1)
47658
function multiply(a, b)
return a * b
end function
PRINT FNmultiply(6,7)
END
DEF FNmultiply(a,b) = a * b
DEF FNmultiply(a,b)
LOCAL c
c = a * b
= c
10 rem Function definition
20 rem ** 1. Function defined as formula. An obsolete way - does not work properly with integer formal parameters (e.g. x%).
30 def fnmultiply(a, b) = a * b
40 rem ** Call the functions
50 print multiply(3,1.23456)
60 print fn multiply(3,1.23456)
70 end
200 rem ** 2. Function defined as subroutine returning a value
210 sub multiply(a,b)
220 multiply = a*b
230 end sub
10 DEF FN MULT(X) = X*Y
20 Y = 4 : REM VALUE OF SECOND ARGUMENT MUST BE ASSIGNED SEPARATELY
30 PRINT FN MULT(3)
DECLARE Multiply(N1:INT,N2:INT)
DEF A,B:INT
A=2:B=2
OPENCONSOLE
PRINT Multiply(A,B)
PRINT:PRINT"Press any key to close."
DO:UNTIL INKEY$<>""
CLOSECONSOLE
END
SUB Multiply(N1:INT,N2:INT)
DEF Product:INT
Product=N1*N2
RETURN Product
'Can also be written with no code in the subroutine and just RETURN N1*N2.
' FB 1.05.0 Win64
Function multiply(d1 As Double, d2 As Double) As Double
Return d1 * d2
End Function
#Define multiply(d1, d2) (d1) * (d2)
Public Sub Main()
Print Multiply(56, 4.66)
End
Public Sub Multiply(f1 As Float, f2 As Float) As Float
Return f1 * f2
End
10 DEF FNMULT(X,Y)=X*Y
20 PRINT FNMULT(5,6)
39 END
100 DEF MULTIPLY(A,B)=A*B
'1. Not Object Oriented Program
DECLARE Multiply(N1:INT,N2:INT),INT
DEF A,B:INT
A=2:B=2
OPENCONSOLE
PRINT Multiply(A,B)
PRINT
'When compiled as a console only program, a press any key to continue is automatic.
CLOSECONSOLE
END
SUB Multiply(N1:INT,N2:INT),INT
DEF Product:INT
Product=N1*N2
RETURN Product
ENDSUB
'Can also be written with no code in the subroutine and just RETURN N1*N2.
----
'2. Not Object Oriented Program Using A Macro
$MACRO Multiply (N1,N2) (N1*N2)
DEF A,B:INT
A=5:B=5
OPENCONSOLE
PRINT Multiply (A,B)
PRINT
'When compiled as a console only program, a press any key to continue is automatic.
CLOSECONSOLE
END
----
'3. In An Object Oriented Program
CLASS Associate
'functions/methods
DECLARE Associate:'object constructor
DECLARE _Associate:'object destructor
'***Multiply declared***
DECLARE Multiply(UnitsSold:UINT),UINT
'members
DEF m_Price:UINT
DEF m_UnitsSold:UINT
DEF m_SalesTotal:UINT
ENDCLASS
DEF Emp:Associate
m_UnitsSold=10
Ass.Multiply(m_UnitsSold)
OPENCONSOLE
PRINT"Sales total: ",:PRINT"$"+LTRIM$(STR$(Emp.m_SalesTotal))
PRINT
CLOSECONSOLE
END
'm_price is set in constructor
SUB Associate::Multiply(UnitsSold:UINT),UINT
m_SalesTotal=m_Price*UnitsSold
RETURN m_SalesTotal
ENDSUB
SUB Associate::Associate()
m_Price=10
ENDSUB
SUB Associate::_Associate()
'Nothing to cleanup
ENDSUB
' define & call a function
print multiply( 3, 1.23456)
wait
function multiply( m1, m2)
multiply =m1 *m2
end function
end
10 DEF FNmultiply(x,y)=x*y
20 PRINT FNmultiply(2,PI)
'SHORT FORMS:
float multiply(float a,b) = a * b
float multiply(float a,b) { return a * b}
'BASIC FORM:
function multiply(float a, float b) as float
return a * b
end function
'BASIC LEGACY FORM:
function multiply(byval a as float, byval b as float) as float
function = a * b
end function
'TEST:
print multiply(pi,2) '6.28...
Procedure multiply(a,b)
ProcedureReturn a*b
EndProcedure
'This function could either be used for all numeric types
'(as they are implicitly convertible to Double)
FUNCTION multiply# (a AS DOUBLE, b AS DOUBLE)
multiply = a * b
END FUNCTION
'
' Alternatively, it can be expressed in abbreviated form :
'
DEF FNmultiply# (a AS DOUBLE, b AS DOUBLE) = a * b
PRINT multiply(3, 1.23456)
PRINT FNmultiply#(3, 1.23456)
Function Multiply(a As Integer, b As Integer) As Integer
Return a * b
End Function
function multiply(a, b = integer) = integer
end = a * b
rem - exercise the function
print "The product of 9 times 3 is"; multiply(9, 3)
end
FUNCTION multiply(a, b)
LET multiply = a * b
END FUNCTION
!
! Alternatively, it can be expressed in abbreviated form :
!
DEF multiply (a, b) = a * b
END
multiply(a, b)
Func
Return a * b
EndFunc
Print FUNC(_multiply (23, 65))
End
_multiply Param (2) : Return (a@ * b@)
Function Multiply(lngMcand As Long, lngMplier As Long) As Long
Multiply = lngMcand * lngMplier
End Function
Sub Main()
Dim Result As Long
Result = Multiply(564231, 897)
End Sub
function multiply( multiplicand, multiplier )
multiply = multiplicand * multiplier
end function
dim twosquared
twosquared = multiply(2, 2)
Function multiply(a As Integer, b As Integer) As Integer
multiply = a * b
End Function
Multiply(6, 111)
Function Multiply(ByVal a As Integer, ByVal b As Integer) As Integer
Return a * b
End Function
Multiply(1, 1)
sub multiply(a, b)
return a * b
end sub
10 PRINT FN m(3,4): REM call our function to produce a value of 12
20 STOP
9950 DEF FN m(a,b)=a*b
You may also check:How to resolve the algorithm Arithmetic evaluation step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm HTTP step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Walk a directory/Non-recursively step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Peano curve step by step in the Java programming language
You may also check:How to resolve the algorithm Matrix transposition step by step in the HicEst programming language