Mastering in Swift Functions

Introduction to Swift Functions

Sridharan T
IVYMobility TechBytes
3 min readApr 1, 2020

--

Swift Functions

Functions

A function is a group of statements that defines an action to be performed. The main use of a function is to make the code reusable.

Defining a Function

Let’s describe each component briefly :

  • func is the keyword you must write to create a function
  • function_name is the name of a function. You can give it any name that defines what a function does.
  • args.. defines the input a function accepts.
  • -> This operator is used to indicate the return type of a function.
  • ReturnType defines the type of value you can return from a function. E.g. Int, String, etc.
  • The return keyword is used to transfer the control of a program to the function call and also return value from a function.
    Even if you don’t specify the return keyword the function returns automatically after execution of the last statement.
  • The value represents the actual data being returned from the function. The value type must match the ReturnType.

Function workflow

In the above diagram, the statement function_name(args) invokes/calls the function with argument values args, which then leaves the current section of code (i.e. stops executing statements below it) and begins to execute the first line inside the function.

  1. The program comes to a line of code func function_name(args..) and accepts the values args passed during the function call function_name(args).
  2. The program then executes the statements statementsInsideFunction defined inside the function.
  3. The statements inside the function are executed one after the other.
  4. After the execution of the last statement, the program leaves the function and goes back to where it started from i.e function_name(args).
  5. let val = stores the value returned from the function in a constant val. Similarly, you can store in a variable as var val = value.
  6. After that, statements statementsOutsideFunction are executed.

Calling a function

Once you have created a function, you can call it in your program to execute the statements declared inside the function. To call a function you simply write the function name followed by ( ) and pass the input parameters inside it as:

function_name(args)

Example :

In the above code, greet(user: “Isac”) calls the function and passes value Isac of type String. After that, the print statement inside the function executes.

When you run the above program, the output will be :

Good Morning! Isac

Keys to remember :

  • Functions help us to reuse our code without repeating it.
  • The function call will invoke the function and execute the code inside the function.

Next part: Types of Swift Functions

A good programmer is someone who always looks both ways before crossing a one-way street. — Doug Linder

Find it a good read?

Recommend this post (by clicking 👏 button) so other people can see it too…
reach me on Twitter Sridharan T

--

--