DEV Community

Saurabh Chavan
Saurabh Chavan

Posted on

3 1

Day 5 : 100DaysOfSwift🚀

Day 5

Functions

Functions let us re-use code, which means we can write a function to do something interesting then run that function from lots of places.
Repeating code is generally a bad idea, and functions help us avoid doing that.

func printHelp() {
    let message = "Namste Swift"

    print(message)
} 


Enter fullscreen mode Exit fullscreen mode

Run using

printHelp()
//output: Namste Swift

Enter fullscreen mode Exit fullscreen mode

1.Accepting parameter

Values sent into functions this way are called parameters.

func sauChavan(Parameter: DataType){
//code
}

sauChavan(Parameter: argument)


Enter fullscreen mode Exit fullscreen mode

func sauChavan(Surname:String){
    let name="saurabh \(Surname)"
    print(name)
}

sauChavan(Surname:"Chavan")
//Output: saurabh chavan
Enter fullscreen mode Exit fullscreen mode

2.Returning values

func square(number: Int) -> Int {
    return number * number
}

let result = square(number: 8)
print(result)
Enter fullscreen mode Exit fullscreen mode

3.Parameter labels

func sayHello(to name: String) {
    print("Hello, \(name)!")
}

sayHello(to: "Saurabh")
//output: Hello Saurabh
Enter fullscreen mode Exit fullscreen mode

4.Omitting parameter labels

func Hello(_ person: String) {
    print("Hello, \(person)!")
}
Hello("Saurabh")
//output: Hello Saurabh
Enter fullscreen mode Exit fullscreen mode

You can refer here also best Way and easy Function

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Sentry mobile image

Improving mobile performance, from slow screens to app start time

Based on our experience working with thousands of mobile developer teams, we developed a mobile monitoring maturity curve.

Read more

đź‘‹ Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay