Function prototype: Difference between revisions

→‎{{header|PL/I}}: Added PureBasic
(→‎{{header|PL/I}}: Added PureBasic)
Line 1,025:
declare f3 entry (character(*), character(*)) returns (character (20));
</lang>
=={{header|PureBasic}}==
PureBasic defines both functions and procedures by using the keyword '''Procedure'''. For the purposes of this task I will describe both 'procedures' and 'functions' using only the term 'procedure'. PureBasic uses a one-pass compiler. All procedures need to be defined before use. The prototypes referred to in the task description are performed with forward declarations in PureBasic.
 
PureBasic allows two types of prototyping.
The first uses the keyword '''Declare''' and describes the name, return value, and parameters of a procedure.
It is identical in form with the first line of a procedure definition with the exception that the keyword
'''Declare''' is used instead of the keyword 'Procedure'''.
It must be placed before the first use of the procedure and must occur before the procedure's definition.
The procedure declaration's parameteres need to match the order, type, and number of those in the procedure's
definition, though their names may be different.
 
The keyword '''ProtoType''' may be used for pointers to procedures so that a definition of the parameters and return type for the function being pointed to are defined and that the pointer may be used to execute the function with type checking. The parameter names do not have to match in the 'ProtoType' definition but the order, type and optional parameters do. 'ProtoTypes' must be defined before their first use.
 
PureBasic does not allow either variable arguments or named parameters.
<lang purebasic>;Forward procedure declare defined with no arguments and that returns a string
Declare.s booTwo()
;Forward procedure declare defined with two arguments and that returns a float
Declare.f moo(x.f, y.f)
;Forward procedure declare with two arguments and an optional argument and that returns a float
Declare.f cmoo(x.f, y.f, m.f = 0)
 
;*** The following three procedures are defined before their first use.
;Procedure defined with no arguments and that returns a string
Procedure.s boo(): ProcedureReturn "boo": EndProcedure
;Procedure defined with two arguments and that returns an float
Procedure.f aoo(x.f, y.f): ProcedureReturn x + y: EndProcedure
;Procedure defined with two arguments and an optional argument and that returns a float
Procedure.f caoo(x.f, y.f, m.f = 1): ProcedureReturn (x + y) * m: EndProcedure
 
;ProtoType defined for any function with no arguments and that returns a string
Prototype.s showString()
;Prototype defined for any function with two float arguments and that returns a float
Prototype.f doMath(x.f, y.f)
;ProtoType defined for any function with two float arguments and an optional float argument and that returns a float
Prototype.f doMathWithOpt(x.f, y.f, m.f = 0)
 
Define a.f = 12, b.f = 5, c.f = 9
Define proc_1.showString, proc_2.doMath, proc_3.doMathWithOpt ;using defined ProtoTypes
If OpenConsole("ProtoTypes and Forward Declarations")
PrintN("Forward Declared procedures:")
PrintN(boo())
PrintN(StrF(a, 2) + " * " + StrF(b, 2) + " = " + StrF(moo(a, b), 2))
PrintN(StrF(a, 2) + " * " + StrF(b, 2) + " + " + StrF(c, 2) + " = " + StrF(cmoo(a, b, c), 2))
PrintN(StrF(a, 2) + " * " + StrF(b, 2) + " = " + StrF(cmoo(a, b), 2))
;set pointers to second set of functions
proc_1 = @boo()
proc_2 = @aoo()
proc_3 = @caoo()
PrintN("ProtoTyped procedures (set 1):")
PrintN(proc_1())
PrintN(StrF(a, 2) + " ? " + StrF(b, 2) + " = " + StrF(proc_2(a, b), 2))
PrintN(StrF(a, 2) + " ? " + StrF(b, 2) + " ? " + StrF(c, 2) + " = " + StrF(proc_3(a, b, c), 2))
PrintN(StrF(a, 2) + " ? " + StrF(b, 2) + " = " + StrF(proc_3(a, b), 2))
;set pointers to second set of functions
proc_1 = @booTwo()
proc_2 = @moo()
proc_3 = @cmoo()
PrintN("ProtoTyped procedures (set 2):")
PrintN(proc_1())
PrintN(StrF(a, 2) + " ? " + StrF(b, 2) + " = " + StrF(proc_2(a, b), 2))
PrintN(StrF(a, 2) + " ? " + StrF(b, 2) + " ? " + StrF(c, 2) + " = " + StrF(proc_3(a, b, c), 2))
PrintN(StrF(a, 2) + " ? " + StrF(b, 2) + " = " + StrF(proc_3(a, b), 2))
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf
 
 
;*** If the forward Declaration above are not used then the following Procedure
;definitions each have to be placed before the call to the respective procedure.
 
;Procedure defined with no arguments and that returns a string
Procedure.s booTwo()
ProcedureReturn "booTwo"
EndProcedure
 
;Procedure defined with two arguments and that returns an float
Procedure.f moo(x.f, y.f)
ProcedureReturn x * y
EndProcedure
 
;Procedure defined with two arguments and an optional argument and that returns an float
Procedure.f cmoo(x.f, y.f, m.f = 0)
ProcedureReturn (x * y) + m
EndProcedure
</lang>
Sample output:
<pre>Forward Declared procedures:
boo
12.00 * 5.00 = 60.00
12.00 * 5.00 + 9.00 = 69.00
12.00 * 5.00 = 60.00
ProtoTyped procedures (set 1):
boo
12.00 ? 5.00 = 17.00
12.00 ? 5.00 ? 9.00 = 153.00
12.00 ? 5.00 = 0.00
ProtoTyped procedures (set 2):
booTwo
12.00 ? 5.00 = 60.00
12.00 ? 5.00 ? 9.00 = 69.00
12.00 ? 5.00 = 60.00</pre>
 
=={{header|Racket}}==
Anonymous user