Parameters with Default Values in Kotlin
📖 Scenario: You are creating a simple greeting function for a friendly chatbot. Sometimes the chatbot knows the user's name, and sometimes it doesn't. You want the chatbot to greet the user by name if given, or use a general greeting if no name is provided.
🎯 Goal: Build a Kotlin function called greet that uses a parameter with a default value. This function will print a greeting message using the name if provided, or a default greeting if no name is given.
📋 What You'll Learn
Create a function named
greet with a parameter name of type String that has a default value.Set the default value of
name to "Guest".Inside the function, print a greeting message using the
name parameter.Call the
greet function twice: once without any argument, and once with the argument "Alice".💡 Why This Matters
🌍 Real World
Functions with default parameters are common in apps and programs to provide flexible behavior without forcing users to always give all details.
💼 Career
Understanding default parameters is important for writing clean, reusable code in Kotlin, which is widely used for Android app development and backend services.
Progress0 / 4 steps