0
0
Kotlinprogramming~15 mins

Function declaration syntax in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Function declaration syntax
📖 Scenario: You are learning how to write simple functions in Kotlin. Functions help you organize your code into reusable blocks that do specific tasks.
🎯 Goal: Create a Kotlin function that adds two numbers and returns the result.
📋 What You'll Learn
Declare a function named addNumbers that takes two Int parameters named a and b.
The function should return an Int which is the sum of a and b.
Call the function with the numbers 5 and 7 and print the result.
💡 Why This Matters
🌍 Real World
Functions help programmers organize code into small reusable pieces, making programs easier to read and maintain.
💼 Career
Knowing how to write functions is essential for any software developer to build clean and efficient code.
Progress0 / 4 steps
1
Create the function header
Write a function declaration named addNumbers that takes two Int parameters called a and b. Do not write the function body yet.
Kotlin
Need a hint?

Start with the keyword fun, then the function name, parameters in parentheses, and the return type after a colon.

2
Add the function body
Inside the addNumbers function, write code to return the sum of a and b.
Kotlin
Need a hint?

Use the return keyword followed by a + b.

3
Call the function
Call the addNumbers function with arguments 5 and 7 and store the result in a variable named result.
Kotlin
Need a hint?

Use val result = addNumbers(5, 7) to call the function and save the answer.

4
Print the result
Write a println statement to display the value of the variable result.
Kotlin
Need a hint?

Use println(result) to show the sum on the screen.