Introduction
This is part Seven of the Swift article series "Swift Programming - Zero to Hero". You can read my previous article from the below links,
- http://www.c-sharpcorner.com/article/swift-programming-zero-to-hero-part-six/
In this article we will be learning about Functions in Swift.
Functions
Functions are blocks of code, that can be executed whenever it's needed. In Swift programming, the function begins with the keyword as func, followed by function name.
Syntax
- func functionName(){
- // Some Operation
- // Block of Codes
- }
- func firstFunction(){
- print("Hello, My First Function in Swift")
- }
The block of codes are wrapped in a pair of curly braces. From the above example, firstFunction() function contains a print statement. This is how the Swift Program's Functions looks like.
Now we can invoke the function by typing the name of functon , followed by parenttheses as,
- firstFunction()
We can provide Inputs to a function. Those inputs are known as Parameters of function or simply parameter. Those input values are used in the function's body.
Syntax
- func functionName(paramenterName: Datatype){
- }
- func firstFunction(values: string){
- }
Invoking the function is very simple as we saw before. Here you have to pass the argument.
- firstFunction(values: "My First Function")
Multiple Parameters
A Function can have more than one parameter. For this you have to pass multiple arguments.
Syntax
- func functionName(argumentName1: type , argumentName2: type){
- // Code Blocks
- }
Example
- func firstFunction(values: string, names:string){
- }
Conclusion
In this article, we have learned briefly about functions in Swift. In my next article we will learn about return types and tuples in detail and also some good examples in functions. Hope this article was very useful. Kindly give your feedback in the comments section.

Munish APosted Oct 26, 2017, 7:19 AM
Nice sharing bro.......
Gayathri AnbazhaganPosted Oct 21, 2017, 9:14 AM
Nice one bro.... thanks for sharing....