Programming in Kotlin: Fundamentals

Aug 9 2022 · Kotlin 1.6, Android 12, IntelliJ IDEA CE 2022.1.3

Part 3: Functions & Nullability

24. Challenge: Work with Functions

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 23. Return Data From Functions Next episode: 25. Conclusion

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

To really master functions, I’ve prepared two challanges for you!

Challenge 1:

Create a function which takes in two parameters - a name and a last name. 
Because not everyone has a last name,
leave the lastName parameter to be an empty String if it is not passed in.

Then return the length of the person's full name is.


Challenge 2:

Overload the function from the first challenge, by adding a list of Strings parameter, for middle names,
in case someone has one or more middle names.

Use the function to return the full name length, for a name with and without middle names.
Remember to use named arguments if needed.
fun getFullNameLength(name: String, lastName: String = "") = 
	name.length + lastName.length
val nameLength = getFullNameLength("Ayo", "Balogun")
println(nameLength)
fun getFullNameLength(
    name: String,
    middleName: String = "",
    lastName: String = "",
  ): Int {
  
  return name.length + middleName.length + lastName.length
}
val length = getFullNameLength("Damini", "Ebunoluwa", "Ogulu")
println(length)