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:
“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)
“Nobody is so constituted as to be able to live everywhere and anywhere; and he who has great duties to perform, which lay claim to all his strength, has, in this respect, a very limited choice. The influence of climate upon the bodily functions ... extends so far, that a blunder in the choice of locality and climate is able not only to alienate a man from his actual duty, but also to withhold it from him altogether, so that he never even comes face to face with it.”
—Friedrich Nietzsche (18441900)
“The English masses are lovable: they are kind, decent, tolerant, practical and not stupid. The tragedy is that there are too many of them, and that they are aimless, having outgrown the servile functions for which they were encouraged to multiply. One day these huge crowds will have to seize power because there will be nothing else for them to do, and yet they neither demand power nor are ready to make use of it; they will learn only to be bored in a new way.”
—Cyril Connolly (19031974)