devim1
devim1

Reputation: 532

Swift: Why to functions have parameters and return value types?

I'm filling a few gaps in my existing Swift programming language skills before moving onto the more advanced features.

I have checked Apples... "Swift Programming Language" guide and Google search shows lots of information about the rules around using the parameters, but I am looking for the overall Why , not the How...

Question:

Why is there a need to have parameters and return value types for functions in Swift?

(I am not asking about the parameter names etc., but a higher level (general) question on 'Why')

I have made a number of programs using simple C-Style functions in Swift, without a need for parameters or return values which works fine as I know what the functions should be doing for me.

Simple e.g.

func printName() {
print("John")
}

However, there are 'exceptions' like some in-built Swift functions. E.g....

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return colors.count }

etc. for table view or collection view usage .

So the only reason I see for using parameters + return values (type) in functions is to ensure only specific type of data is inputted and outputted.

In other words, its a way to avoid unexpected outcomes. So acting almost like a Switch statement, where if I have a specific condition is met -- a specific output will occur... (in functions... "an action").

Am I correct in this understanding, in trying to understand why / where one needs to use parameters & / or return values types with Functions? Or are there other practical reasons? If so can you provide an example?

Many thanks in advance!!!!

Upvotes: 2

Views: 649

Answers (2)

Luca Angeletti
Luca Angeletti

Reputation: 59496

Let's imagine a sum function

func sum(a: Int, b:Int) -> Int {
    return a + b
}

What happen if we remove the parameter types

func sum(a, b) -> Int {
    return a + b // what?
}

Of course this is not allowed by the compiler but let's try to imagine.

Now a and b could be anything (like a String and a UIImage). How could we write the code to sum 2 things and get an Int?

Another test, let's remove the return type

func sum(a:Int, b:Int) -> ??? {
    return a + b
}

Now let's try to invoke our crazy function

let tot: Int = sum(1, b: 2)

but since sum could return any kind of value how can we put the result into an Int constant?

Upvotes: 0

Diego Freniche
Diego Freniche

Reputation: 5414

If you don't use parameters, you're tied to use what's visible in your scope.

  • if your function is inside a ViewController, all properties inside that VC, also global scope variables (horrible)
  • if your function is in global scope, you can only use global scope symbols (horrible, again)

You really do want to pass in things using parameters. This way you can:

  • auto check types (compiles is doing for you)
  • check for optionality
  • check value ranges
  • test your functions using Unit Tests. if you use global symbols, you're tied to that symbols, can't change them
  • less maintenance: changes outside your function doesn't affect you

Also, having a clear return type helps other developers and your future-self to understand what's returning from that function: a tuple? An optional array? A number? Also, having defined output types helps you with testing your functions.

Using only global variables & procedures (chunks of code to process those global vars) could work on a small scale. That's the old approach use with languages like FORTRAN, BASIC, COBOL... The moment your project grows, you need to isolate one part of your code from the other. For that:

  • use functions & a functional approach
  • use OOP

Upvotes: 2

Related Questions