Open In App

Calling Functions in Swift

Last Updated : 03 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Functions are independent lumps of code that play out a particular undertaking. You can give a name to the function that recognizes what it does, and also the name is utilized to “call” the function to play out its errand when required. Or we can say that a function is a bunch of orders/explanations. To make a straightforward function, you initially need to characterize and name it; for the most part, naming is as indicated by the errand you believe it should perform.

At the point when you characterize a Function, you can alternatively characterize at least one named, a composed value that a function takes as info, known as boundaries. You can likewise alternatively characterize a sort of significant worth that a function will pass back as a result when it’s finished, known as its bring type back.

Declaring and Calling Functions

Pronouncing a function and composing the code inside the enclosure will work unequivocally. The thrilling thing is that you don’t need to compose many lines of code again sometimes from here on out. Call the function as displayed in the model underneath, and it will perform it.

Example:

Swift




// Swift program to declare and call a function
 
// Declare a function
func greet()
{
    print("Hello GeeksForGeeks")
}
 
// Calling the function
greet()
 
print("Outside function")


Output:

Hello GeeksForGeeks
Outside function

Assuming you see, it currently is more appealing and organized. Very much like “var” and “let” keywords, we have a “func” catchphrase for pronouncing a capability. Then name the capability like how you call your factors and constants yet add open and close supports and end up with open and close brackets.

func car() {
/* lines of code */
}

The function won’t ever run consequently. To run a function, you really want to call it at whatever point you wish to run it. Calling a function is straightforward, express “car()” and afterward anything code is inside this function, the control center will have a go at running that.

car()

Parameters

You figured out how to compose a function and call it. Be that as it may, imagine a scenario where you need the other name rather than “Joey” or “Chandler” or “Xyz” like clockwork. The response is to involve boundaries in a function. The boundaries assist with getting new qualities each time you take a stab at calling a function.

You can see a boundary “item” with a kind String in the model underneath. Very much like you dole out another worth to a variable, you give worth to your function, which ultimately replaces that esteem wherever you utilize that boundary in the code composed inside the enclosure of a function.

Example:

Swift




// Swift program to declare and call a function with two parameters
 
// Function that adds two numbers
func addNumbers(num1: Int, num2: Int)
{
    let sum = num1 + num2
    print("Sum: ", sum)
}
 
// Calling function with two values
addNumbers(num1: 6, num2: 4)


Output:

Sum: 10

Return Values

“Return” in programming means returning a value. The function receives the value from what you entered, but it can also return the value by giving it back to you.

Example:

Swift




// Swift program to declare and call a function with return value
 
// Function which returns the area of circle
func circleArea(radius: Double) -> Double
{
   let pi = 3.14
   let formula = pi*radius*radius
    
   return formula
}
 
// Calling the function and passing parameter to the function
let answer = circleArea(radius: 3.5) 
print("Area:", answer)


Output:

38.465

Note: Return is similar to the print statement, but they are not the same. When you return a value and then call a function, it won’t print any value. You must declare a variable or a constant and then print it with its help!

So to return a value, add “->” after the parameters list, and when returning a value, write the keyword “return” just before your closing bracket of a function.

Argument Labels

Up to this point, you just grasped the outline idea of boundaries, however when you name a boundary, it serves/relegates a similar mark to the contention, and the boundary name itself is known as “contention marks” and “boundary names”. Of course, the boundaries utilize their boundary names as their contention marks.

In basic terms, think like the x variable when you were in secondary school and tackle a few logarithmic issues. At the point when the coefficient of x is 1, you don’t compose 1x yet x. During the time, you disregarded composing 1 preceding your variable since it wasn’t required; very much like that, with basic boundaries being used in your function, you don’t need to irritate a lot of contention marks.
However, you compose a coefficient before the variable when it is more prominent than the worth 1 since you can’t simply make it a suspicion since it would make it mind-boggling/complex to comprehend the issue, about the equivalent, contention names make your program intelligible and ease for yourself and other colleagues.

Swift




// Swift program to declare and call a function with argument label
 
// Function with argument label
func marvel(year: Int, spiderman movie: String)
{
   print("\(movie) was released in \(year)")
    
}
 
// Calling the function
marvel(year: 2008, spiderman: "Iron man")


Output:

Iron man was released in 2008

In the above example, you probably saw that when you call the function marvel, you will compose a contention mark inside the round that prepares for the subsequent boundary and not a boundary name, yet that is the contrary while involving it in a print proclamation. Another significant thing you probably speculated is that the primary boundary of this function will be utilized as a boundary name and argument label.

Excluding Argument Labels

You can drop giving the name to an argument label by replacing it with a “_” (underscore). For example,

Swift




// Swift program to declare and call a function without argument label
 
// Declare function with arguments
func marvel(_ year: Int, IronMan: String)
{
 
   print("\(IronMan) was released in \(year)")
    
}
 
// Calling function with argument
marvel(2008, IronMan: "3")


Output:

3 was released in 2008

If you use an argument label in your parameter, be careful to label it.

Default Parameter Values

Try remembering how you assigned a value to a variable at the time of declaring,

var x = 3

Declaring/assigning a value to a parameter is precisely the same. It would help if you kept in mind placing a default value parameter after the parameter without the default value. This is helpful, excluding the use of parameters with default values while calling the function.

Example:

Swift




// Swift program to declare and call a function with default parameter value
 
// Declaring function with parameter
func demoFunction(x : Int, y : Int = 10)
{
    print("Parameter without Default Value = \(x)")
    print("Parameter with Default Value = \(y)")
}
 
// Calling function with default value
demoFunction(x: 4)
 
// Calling function and change the default value
// of a parameter to a new one
demoFunction(x: 4, y: 7) 


Output:

Parameter without Default Value = 4
Parameter with Default Value = 10
Parameter without Default Value = 4
Parameter with Default Value = 7


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads