Skip to main content

How to use Functions in swift

As you know function is play an important part in programing so today i want to  show  you how to use function in swift
What is a function?




Functions are self-contained chunks of code that perform a specific task. You give a function a name that identifies what it does, and this name is used to “call” the function to perform its task when needed.
Swift’s unified function syntax is flexible enough to express anything from a simple C-style function with no parameter names to a complex Objective-C-style method with names and argument labels for each parameter. Parameters can provide default values to simplify function calls and can be passed as in-out parameters, which modify a passed variable once the function has completed its execution.

Every function in Swift has a type, consisting of the function’s parameter types and return type. You can use this type like any other type in Swift, which makes it easy to pass functions as parameters to other functions, and to return functions from functions. Functions can also be written within other functions to encapsulate useful functionality within a nested function scope.

if i don't use the function i have to write code like this:

print("Apple")
print("Sam Sum")
print("HTC")
instead of you have to write code like that you can use the function ,in swift a function is defined by the func keyword 

func cellPhone(){
    
    print("Apple")
    
    print("Sam Sum")
    
    print("HTC")
    
}

Functions Without Parameters 

Functions are not required to define input parameters. Here’s a function with no input parameters, but have return value 
func sayHello()->String{
    
    return "Hello Word"
    
}

The function definition still needs parentheses after the function’s name, even though it does not take any parameters. The function name is also followed by an empty pair of parentheses when the function is called.

Functions with one parameter with no return value


func  Sayhello(to  name:String)
{
    
print("my name \(name)")
}
Sayhello(to: "Kevin")

Functions With Multiple Parameters


func Minus(numberA:Int, numberB:Int)->Int{

    let result = numberA+numberB
    print("the result \(result)")
    return result;
}
Minus(numberA: 3, numberB: 4)

i'm kevin i hope that this blog help you understand about function in swift









Comments

Popular posts from this blog

When is viewWillAppear, viewDidAppear, viewDidLoad, viewWillDisappear actually called

A few months ago I decided to apply for a job at a software company, I interviewed for IOS developer position. After a week I got a result they  gave me an offer 400$ per months, salary not too high, not too low for me  I also  struggled with a lot of questions from my interviewer, so today in this article I will share with you my experiment to answer the question from the interviewer  if you are  IOS developer you are familiar with  viewWillAppear , viewDidAppear , viewDidLoad , viewWillDisappear , and viewDidDisappear  let me ask you a question what is the running order of viewDidAppear , viewDidLoad , viewWillDisappear , and viewDidDisappear. This question sounds easy it’s straightforward. Actually, it very hard to answer, because when you make app maybe you don't care about it , No one tells you before you get an interview ,  now we need to create a new project, inside of ViewController.swift  we need to implement some method...

Build Carousel Effect in iOS with Swift Kien Pham

This tutorial I’m going to show you how to build Carousel effect it is the most common, you can see it everywhere you can see it on Instagram or App Store which is beautiful UI you can use a lot of in your app many places  that you want to show beautiful text and beautiful image view First thing first we need to create a new project and select iOS->SingleView Application Product Name: Tableview – this the name of your project ·      Company Identifier: KienPhamDev It’s actually the domain name written the other way round. if  you have a domain, you can use your own domain name otherwise you may use mine or just fill in ·      Device family: you can choose universal it’s not important ·      Choose swift as Language And then we are going to the main storyboard , in this tutorial we want to use ViewController so we just need to drag a new Collection View from Object library ...

How to update two images using GCD in swift

If you are IOS developer you can run into a problem when your app download a lot of image from URL maybe your app is crashed or you can't interact with the rest of the application, when I interviewed for a job  a few months ago, I also got this question from my interviewer   He assumed that there are 3 images from URL when user click on the update button all of them will be updated at  the same time so let’s do it a demo to know what happens when your application run without multithreading    Inside of my project I need  some element into ViewController,  As you can see we have 2 images and a button to update our image  Inside of ViewController , we need to hook up all of them with  @IBOutlet and @IBAction it should look like this  As you can see when user click on the update button to download two images from URL, you can't interact with the rest of the application you can’t d...