HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to SwiftVariablesData TypesOperatorsStringsArraysConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 Array & Dictionary
🔀 Condition & Loop
🧩 Function
func (function)Default parameter
Swift›REFERENCE›Function
🧩

Function

func, return, and default parameters.

CommandDescription
func (function)Defines your own command.
Default parameterIf not provided, the default value is used.

func (function)

Try it yourself →

Defines your own command.

Syntax

func name(_ p: Type) -> ReturnType { return ... }

Description

Defined with func; -> specifies the return type. The _ before a parameter omits its label. return gives back the result.

Example

func square(_ x: Int) -> Int {
  return x * x
}
print(square(5))

Expected output

25

When to use?

Preventing code repetition, breaking the program into parts.

Default parameter

Try it yourself →

If not provided, the default value is used.

Syntax

func name(p: Type = default)

Description

Parameters are given a default value using =. If omitted during a call, the default is used; this enables writing flexible functions.

Example

func greet(_ name: String, greet: String = "Hello") -> String {
  return "\(greet) \(name)"
}
print(greet("Alex"))
print(greet("Alex", greet: "Good morning"))

Expected output

Hello Alex
Good morning Alex

When to use?

Arguments that are usually the same, but occasionally change.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code