Comparison of Programming Languages (basic Instructions) - Functions

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)» instructionsreturn 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:

    Adolescents, for all their self-involvement, are emerging from the self-centeredness of childhood. Their perception of other people has more depth. They are better equipped at appreciating others’ reasons for action, or the basis of others’ emotions. But this maturity functions in a piecemeal fashion. They show more understanding of their friends, but not of their teachers.
    Terri Apter (20th century)

    In today’s 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)

    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 (1844–1900)