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?
![]() |
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
Post a Comment