Using Vararg Parameters in Kotlin
📖 Scenario: You are building a simple Kotlin program to handle a list of exam scores for students. Sometimes, you want to pass multiple scores at once to calculate their average.
🎯 Goal: Create a Kotlin function that uses vararg parameters to accept multiple exam scores and calculate their average.
📋 What You'll Learn
Create a function named
averageScore that accepts multiple Int scores using vararg.Inside the function, calculate the average of all scores passed.
Call the function with exactly these scores:
70, 85, 90, 75.Print the average score as a
Double.💡 Why This Matters
🌍 Real World
Vararg parameters let you write flexible functions that can take any number of inputs, like scores, names, or prices, without needing to create arrays manually.
💼 Career
Understanding vararg parameters is useful for Kotlin developers working on apps that process lists of data dynamically, such as user inputs, sensor readings, or batch operations.
Progress0 / 4 steps