UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Almost have it

Forums > Swift

I almost have for the function descnedant. Could some one please help? Here is my code.

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    func descendant(_ first: Int, _ rest: Int) -> Any? {
        print("\(first) \(rest)")
        return descendant(_:_:)
    }
    descendant(4, 9)

}

}

3      

Not really sure what it is you're trying to do here. Can you clarify?

3      

What I'm trying to do is print out the numbers of the descendants.

I think I found the soultion.

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    func descendant(_ first: Int, _ rest: Int) -> Any? {
        print("\(first) \(rest)")
        return descendant(_:_:)
    }
    print(descendant(4, 9)!)

}

}

The output that I see is: 4 9 (Function) could anyone tell me if this would be correct?

3      

If all you want to do is print out the numbers, then this will work:

override func viewDidLoad() {
    super.viewDidLoad()

    func descendant(_ first: Int, _ rest: Int) {
        print("\(first) \(rest)")
    }
    descendant(4, 9)

}

You don't need to return anything from descendant since you handle the printing inside that function.

3      

Thank you for your help. It worked like a charm.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.