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:
“In todays world parents find themselves at the mercy of a society which imposes pressures and priorities that allow neither time nor place for meaningful activities and relations between children and adults, which downgrade the role of parents and the functions of parenthood, and which prevent the parent from doing things he wants to do as a guide, friend, and companion to his children.”
—Urie Bronfenbrenner (b. 1917)
“When Western people train the mind, the focus is generally on the left hemisphere of the cortex, which is the portion of the brain that is concerned with words and numbers. We enhance the logical, bounded, linear functions of the mind. In the East, exercises of this sort are for the purpose of getting in tune with the unconsciousto get rid of boundaries, not to create them.”
—Edward T. Hall (b. 1914)
“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)