Difference between revisions of "W1205 Function Introduction"

From Coder Merlin
Line 17: Line 17:
In the above case, the function has no '''side-effects''', that is, it does not in anyway modify any data structures elsewhere, it simply returns a value.  In other cases, however, it may be desirable to perform some side effect.  For example, functions are available to open a file for writing or to retrieve a web page specified by a URL parameter.  
In the above case, the function has no '''side-effects''', that is, it does not in anyway modify any data structures elsewhere, it simply returns a value.  In other cases, however, it may be desirable to perform some side effect.  For example, functions are available to open a file for writing or to retrieve a web page specified by a URL parameter.  
== Anatomy of a Swift Function Declaration ==
== Anatomy of a Swift Function Declaration ==
<syntaxhighlight lang="swift">
func sayHello(toPersonName:String) {
    print("Hello, \(toPersonName)")
}
</syntaxhighlight>
In the above example:
* '''func''' is a keyword indicating that we're about to define a function
* ''sayHello'' is the '''name''' of the function
* ''toPersonName'' serves as both the '''argument label''' and '''parameter name'''.  By default, these share the same name.
** The ''argument label'' is used when invoking the function
** The ''parameter name'' is used to refer to the parameter ''within'' the function
* The '''body''' of the function contains the code executed when the function is invoked.  In the above case, this is the print statement.
A function may optionally return a value to the '''caller''' (the code which invoked the function).  In the below example, the function specifies the '''return type''' as ''String'' and used the keyword '''return''' at the end of the function to return the value.
<syntaxhighlight lang="swift">
func fullName(firstName:String, lastName:String) -> String {
    return "\(lastName), \(firstName)"
}
</syntaxhighlight>
If a function does indicate a return type then it must return a value at each exit point from the function.


== Key Concepts ==
== Key Concepts ==

Revision as of 22:50, 13 November 2019

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Tektronix FG 504 Function generator with DC 503 Universal Counter

Prerequisites[edit]

Introduction[edit]

Functions enable us to package a sequence of statements that we could easily reuse whenever desired. Functions can be used to break up a larger program into smaller, more easily understood units.

The code contained within a function is known as the body of the function. Some functions, similar to the way we use functions in algebra, expect one or more values to be provided by the calling program when it invokes the function. The function declares placeholders, called parameters for each value required. The type of value must also be specified. When the function is later invoked, the calling program specifies the actual arguments which must match the declared parameters.

Some functions may also return a value back to the caller (again, similar to the way we think of functions in algebra). In many cases functions will indeed behave as algebraic functions, where the returned value is dependent entirely on the argument(s).

Function Invocation[edit]

Activating (or calling) a function is formally termed function invocation. An example of invoking an algebraic-like function is:

let xCoordinate = cos(Double.pi * 2)

In this case, the name of the function is "cos" taking a single argument of type Double and returning a Double. In this case, the function has no side-effects, that is, it does not in anyway modify any data structures elsewhere, it simply returns a value. In other cases, however, it may be desirable to perform some side effect.

Side Effects[edit]

In the above case, the function has no side-effects, that is, it does not in anyway modify any data structures elsewhere, it simply returns a value. In other cases, however, it may be desirable to perform some side effect. For example, functions are available to open a file for writing or to retrieve a web page specified by a URL parameter.

Anatomy of a Swift Function Declaration[edit]

func sayHello(toPersonName:String) {
    print("Hello, \(toPersonName)")
}

In the above example:

  • func is a keyword indicating that we're about to define a function
  • sayHello is the name of the function
  • toPersonName serves as both the argument label and parameter name. By default, these share the same name.
    • The argument label is used when invoking the function
    • The parameter name is used to refer to the parameter within the function
  • The body of the function contains the code executed when the function is invoked. In the above case, this is the print statement.

A function may optionally return a value to the caller (the code which invoked the function). In the below example, the function specifies the return type as String and used the keyword return at the end of the function to return the value.

func fullName(firstName:String, lastName:String) -> String {
    return "\(lastName), \(firstName)"
}

If a function does indicate a return type then it must return a value at each exit point from the function.

Key Concepts[edit]

Exercises[edit]

References[edit]