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

Lesson Complete

Play Next Lesson
Save for later
About this episode
See versions
See forum comments
Cinema mode Mark as Complete Download course materials
Previous episode: 23. Return Data From Functions Next episode: 25. Conclusion

This video was last updated on Aug 9 2022

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a Professional subscription.

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")
fun getFullNameLength(
    name: String,
    middleName: String = "",
    lastName: String = "",
  ): Int {
  return name.length + middleName.length + lastName.length
val length = getFullNameLength("Damini", "Ebunoluwa", "Ogulu")