w3resource

Kotlin Function - Exercises, Practice, Solutions

Kotlin Function Exercises [ 22 exercises with solution ]

[An editor is available at the bottom of the page to write and execute the scripts.  Go to the editor]

1. Write a Kotlin function that takes a 'name' as an argument and prints a personalized greeting message to the user.

Click me to see the solution

2. Write a Kotlin function that takes an array of integers and prints only even numbers.

Click me to see the solution

3. Write a Kotlin function `countVowels` that counts the number of vowels in a given string.

Click me to see the solution

4. Write a Kotlin function that reverses a given string.

Click me to see the solution

5. Write a Kotlin function that checks if a string is a palindrome or not.

Click me to see the solution

6. Write a Kotlin function that prints a message and does not return anything.

Click me to see the solution

7. Write a Kotlin function that calculates and returns the area of a rectangle. It should take 'length' and 'width' as arguments, with default values of 0.0.

Click me to see the solution

8. Write a Kotlin function that calculates the Body Mass Index (BMI) of a person. The function should take the height (in meters) and weight (in kilograms) as arguments. Use default arguments for height and weight.

Click me to see the solution

9. Write a Kotlin function that calculates the area of a circle. Use a default value of 3.14 for pi.

Click me to see the solution

10. Write a Kotlin function that takes named arguments 'name', 'age', and 'city' and prints the details of a person.

Click me to see the solution

11. Write a Kotlin function that takes a person's name as an argument and prints a greeting message with the name. The function should return Unit.

Click me to see the solution

12. Write a Kotlin function that takes an integer n as an argument and prints the numbers from 1 to n on separate lines. The function should return Unit.

Click me to see the solution

13. Write a Kotlin function that takes a list of integers as an argument and prints only the even numbers in the list. The function should return Unit.

Click me to see the solution

14. Write a Kotlin single-expression function that takes a number as an argument and returns true if it is negative, false otherwise.

Click me to see the solution

15. Write a Kotlin function that prints a pattern of asterisks based on a given size. The size represents the number of rows and columns in the pattern. Use a single-expression function.

Click me to see the solution

16 Write a Kotlin function that takes two numbers as arguments and returns their sum. Explicitly specify the return type.

Click me to see the solution

17 Write a Kotlin function that takes an integer as an argument and returns a Boolean indicating whether the number is prime or not. Use explicit return type.

Click me to see the solution

18 Write a Kotlin function that takes a variable number of arguments (varargs) and calculates the average of those numbers.

Click me to see the solution

19 Write an Kotlin infix function that checks if a number is divisible by another number.

Click me to see the solution

20 Write a Kotlin function inside another function that calculates the square of a number.

Click me to see the solution

21 Write a Kotlin generic function that swaps the values of two variables.

Click me to see the solution

22 Write a Kotlin function that calculates the sum of a given arithmetic or geometric series. The function should take the first term, common difference/ratio, and the number of terms as arguments. Use generic types to handle both arithmetic and geometric series.

Click me to see the solution

More to Come !

* To run the code mouse over on Result panel and click on 'RERUN' button.*

Kotlin Editor:


Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/kotlin-exercises/function/index.php