• Skip to main content

Connections.Digital

Media Production, Mobile Web and iOS Development

  • Facebook
  • X
  • YouTube
  • GitHub
  • Home
  • Media Production
  • Blog
  • Galleries
  • Contact
  • About

November 4, 2018

Intermediate Swift – Chapter 2.2 Functions

import UIKit

func displayPi() {
    print("3.1415926535")
}

displayPi()

//Parameters

func triple(value: Int) {
    let result = value * 3
    print("If you multiply \(value) by 3, you'll get \(result).")
}

triple(value: 10)

func multiply(firstNumber: Int, secondNumber: Int) {
    let result = firstNumber * secondNumber
    print("The result is \(result).")
}

multiply(firstNumber: 7, secondNumber: 8)

//Argument Labels

func sayHello(to firstPerson: String, and SecondPerson: String) {
    print("Hello, \(firstPerson) and \(SecondPerson).")
}

sayHello(to: "Jancy", and: "Yvonne")

//Default Parameters

func display(teamName: String, score: Int = 0) {
    print("\(teamName): \(score)")
}

display(teamName: "Wombats", score: 100)
display(teamName: "Warriors")

//Return Values

func multiply1(firstNumber: Int, secondNumber: Int) -> Int {
    let result = firstNumber * secondNumber
    return result
}

print(multiply1(firstNumber: 7, secondNumber: 8))

let myResult = multiply1(firstNumber: 5, secondNumber: 5)

print("5 * 5 is \(myResult)")

print("5 * 2 is \(multiply1(firstNumber: 5, secondNumber: 2))")

Filed Under: Swift Tagged With: Amarillo, Swift 4.2, Texas

Reader Interactions

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Copyright © Connections Information Systems

Designed by Kenneth Jackson at Connections.Digital in Amarillo, Texas

Powered by WPplaces