Functions
See reflection for calling and declaring functions by strings.
calling a function | basic/void function | value-returning function | required main function | |
---|---|---|---|---|
ALGOL 68 | foo«(parameters)»; | proc foo = «(parameters)» void: ( instructions ); | proc foo = «(parameters)» rettype: ( instructions ...; retvalue ); | |
C (C99) | foo(«parameters») | void foo(«parameters») { instructions } | type foo(«parameters») { instructions ... return value; } | «global declarations» int main(«int argc, char *argv») { instructions } |
Objective-C | ||||
C++ (STL) | ||||
C# | static void Main(«string args») { instructions } or static int Main(«string args») { instructions } |
|||
Java | public static void main(String args) { instructions } or public static void main(String... args) { instructions } |
|||
D | int main(«char args») { instructions} or int main(«string args») { instructions} or void main(«char args») { instructions} or void main(«string args») { instructions} |
|||
JavaScript | function foo(«parameters») { instructions } or var foo = function («parameters») {instructions } or var foo = new Function («"parameter", ... ,"last parameter"» "instructions"); |
function foo(«parameters») { instructions ... return value; } | ||
Go | func foo(«parameters») { instructions } | func foo(«parameters») type { instructions ... return value } | func main { instructions } | |
Common Lisp | (foo «parameters») | (defun foo («parameters»)
instructions)
or (setf (symbol-function 'symbol) lambda) |
(defun foo («parameters») ... value) | |
Scheme | (define (foo parameters) instructions) or (define foo (lambda (parameters) instructions)) |
(define (foo parameters) instructions... return_value) or (define foo (lambda (parameters) instructions... return_value)) |
||
ISLISP | (defun foo («parameters») instructions) | (defun foo («parameters») ... value) | ||
Pascal | foo«(parameters)» | procedure foo«(parameters)»; «forward;» «label label declarations» «const constant declarations» «type type declarations» «var variable declarations» «local function declarations» begin instructions end; |
function foo«(parameters)»: type; «forward;» «label label declarations» «const constant declarations» «type type declarations» «var variable declarations» «local function declarations» begin instructions; foo := value end; |
program name; «label label declarations» «const constant declarations» «type type declarations» «var variable declarations» «function declarations» begin instructions end. |
Visual Basic | Foo(«parameters») | Sub Foo(«parameters») instructions End Sub | Function Foo(«parameters») As type
instructions Foo = value End Function |
Sub Main instructions End Sub |
Visual Basic .NET | Function Foo(«parameters») As type
instructions Return value End Function |
Sub Main(«ByVal CmdArgs As String»)
instructions
End Sub or Function Main(«ByVal CmdArgs As String») As Integer instructions End Function |
||
Python | foo(«parameters») | def foo(«parameters»): Tab ↹ instructions |
def foo(«parameters»): Tab ↹ instructions Tab ↹ return value |
|
S-Lang | foo(«parameters» «;qualifiers») | define foo («parameters») { instructions } | define foo («parameters») { instructions ... return value; } | public define slsh_main { instructions } |
Fortran | foo («arguments») CALL sub_foo («arguments») |
SUBROUTINE sub_foo («arguments») instructions END SUBROUTINE | type FUNCTION foo («arguments»)
instructions ... foo = value END FUNCTION |
PROGRAM main instructions END PROGRAM |
Forth | «parameters» FOO | : FOO « stack effect comment: ( before -- ) » instructions ; | : FOO « stack effect comment: ( before -- after ) » instructions ; | |
PHP | foo(«parameters») | function foo(«parameters») { instructions } | function foo(«parameters») { instructions ... return value; } | |
Perl | foo(«parameters») or &foo«(parameters)» |
sub foo { «my (parameters) = @_;» instructions } | sub foo { «my (parameters) = @_;» instructions... «return» value; } | |
Perl 6 | foo(«parameters») or &foo«(parameters)» |
«multi »sub foo(parameters) { instructions } | «our «type» »«multi »sub foo(parameters) { instructions... «return» value; } | |
Ruby | foo«(parameters)» | def foo«(parameters)» instructions end | def foo«(parameters)»
instructions «return» value end |
|
Windows PowerShell | foo« parameters» | function foo «(parameters)» { instructions }; or function foo { «param(parameters)» instructions } |
function foo «(parameters)» { instructions … return value }; or function foo { «param(parameters)» instructions … return value } |
|
OCaml | foo parameters | let «rec» foo parameters = instructions | let «rec» foo parameters = instructions... return_value | |
F# | let main args = instructions | |||
Standard ML | fun foo parameters = ( instructions ) | fun foo parameters = ( instructions... return_value ) | ||
Haskell | foo parameters = do Tab ↹ instructions |
foo parameters = return_value or foo parameters = do Tab ↹ instructions Tab ↹ return value |
«main :: IO » main = do instructions |
|
Eiffel | foo («parameters») | foo («parameters») require preconditions do instructions ensure postconditions end |
foo («parameters»): type require preconditions do instructions Result := value ensure postconditions end |
^a Pascal requires "forward;" for forward declarations.
^b Eiffel allows the specification of an application's root class and feature.
^c In Fortran, function/subroutine parameters are called arguments (since PARAMETER is a language keyword); the CALL keyword is required for subroutines.
Read more about this topic: Comparison Of Programming Languages (basic Instructions)
Famous quotes containing the word functions:
“Empirical science is apt to cloud the sight, and, by the very knowledge of functions and processes, to bereave the student of the manly contemplation of the whole.”
—Ralph Waldo Emerson (18031882)
“Those things which now most engage the attention of men, as politics and the daily routine, are, it is true, vital functions of human society, but should be unconsciously performed, like the corresponding functions of the physical body.”
—Henry David Thoreau (18171862)
“Let us stop being afraid. Of our own thoughts, our own minds. Of madness, our own or others. Stop being afraid of the mind itself, its astonishing functions and fandangos, its complications and simplifications, the wonderful operation of its machinerymore wonderful because it is not machinery at all or predictable.”
—Kate Millett (b. 1934)