Go Concepts - Functions: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 9: Line 9:
=Overview=
=Overview=


=Function=
=Function Definition=


A function is operation performed on a number of inputs resulting in a number of outputs. Same inputs will always result in the same outputs, ''function do not depend on state''.
A function is operation performed on a number of inputs resulting in a number of outputs. Same inputs will always result in the same outputs, ''function do not depend on state''.

Revision as of 02:08, 22 March 2016

External

Internal

Overview

Function Definition

A function is operation performed on a number of inputs resulting in a number of outputs. Same inputs will always result in the same outputs, function do not depend on state.

Syntax

func [value-receiver] <name>([parameter-identifier1] [type1], [parameter-identifier2] [type2], ...) ([return-type1], [return-type2], ...) {

   statement1
   statemen2
   ...

   return <return-value1>, <return-value2>

}

Examples:

func add(a int, b int) (int) {
   return a + b;
}

Built-in Functions

make() append() copy() delete() .
new() len() cap() . .
. . . . .

Closures

Anonymous function that capture local variables.

More about closures is available here.


Methods

A method defines the behavior of a type, and it relies on the state of an instance of the type. The method will - and it is supposed to - change the state.

Difference between Functions and Methods

Receivers

Value Receivers

Pointer Receivers