From last two posts, we have been working on functions in swift. In the first post we learned how to define and call the functions and in the second we focused on the parameters.
In this post, we will look into the values a function can return.
If you recall the last two posts, we have used an example of a factorial function. For this post also, we will be using the same factorial function as an example.
func factorial (number: Int) -> Int{ var factorial = 1 var temp = number while(temp>1){ factorial = factorial * temp temp = temp - 1 } return factorial }
Functions with One Return Value
When we take a closer look at the function, we see that this function returns factorial variable. What is this factorial variable?
This factorial variable stores the factorial of the number which we pass as the parameter. And this factorial is of the type integer.
Plus the main point to note here is that we have used Int as the return type in the function definition which you can see in the first line.
When we call this function for a factorial of 7, we get the output as 5040 which is an integer.
So this function has a return value. It returns just one value ie factorial.
But returning a value is not mandatory for a function. We will also have functions which are having no return values.
Functions with No Return Value
For the example of a function with no return value, we will be removing the return type of this factorial function.
Let’s delete this int from the first line. But as soon as we remove this int, we get an error which says, “Unexpected non-void return value in void function”
This actually means that we are returning a value in a function which has no return value. Void means that the function is not going to have a return value.
And when we delete this return statement the error vanishes.
Now factorial function is complete and we are calling this factorial function here in the print statement. The output is just the empty parenthesis and not 5040. why?
Well, coming back to the function, we see it does not return a value and also have no print statement to print the value of factorial.
What we can do is copy this print statement inside and replace the function call with the factorial variable. And call this function separately.
Now we can again see the output 5070.
//Example //Factorial of a Number func factorial (number : Int) { var factorial = 1 var temp = number while(temp>1){ factorial = factorial * temp temp = temp - 1 } print("Factorial is \(factorial)") } //functionName(paramName: paramValue) factorial(number: 7)
Hmm, this was a function with no return value. And before this one, we saw the function with one return value.
Functions with Multiple Return Values
But what should we do if we want to return more than one value?
In swift, we can do that by using tuples as return values. And in these tuples, specify the multiple return types.
Not clear? I know. It will be through this example. We will first write a function to return the quotient and remainder of dividing number A by number B and then understand it.
//divide function with two return values quotient and remainder func divide(numA: Int, numB: Int) -> (quotient: Int, remainder: Int){ return (numA/numB, numA%numB) } let result = divide(numA: 9, numB: 2) print("Quotient is \(result.quotient) and Remainder is \(result.remainder)")
We get the output as 4 and 1.
Any guess what’s going on here. We have a function divide with two input parameters: numA and numB. And the return values quotient and remainder which are enclosed in parenthesis to form a tuple.
Next, we return numA/numB to get the quotient and numA%numB to get the remainder.
We can directly print the result by calling the function in a print statement. But if want to access both the results separately, we store them in a constant and then call them by their names as we did in this print statement using the result.quotient and result.remainder.
Now we know how to make functions with no parameter, one parameter and also multiple parameters.
You can find the source code for this post here.
In the next post, we will move on to argument labels.