Functions:
So far we have seen the main function. So if you want to split your code into separate functions. So main function is actually launched your application as it the function that gets executed when you run it.So how to create a function.
For example;
fun test()
{
println("Test")
}
So once you have written the function you can call it from elsewhere in your application:
fun main(args: Arrays<String>)
{
test()
}
you can also send things to a function.you can tell the compiler what values a function can accept by your specification one or more parameters you can pass:
fun test(param: Int)
{
println("Paramater is $param")
}
// you can call it like
test(6)
You can also pass variables to a function so long as the variable type matches the parameter type:
fun main(args: Array<Strings>)
{
val x: Int =7
val y:Int =8
printSum(x,y)
}
fun printSum(int1: Int, int2:Int)
{
val result = int1 + int2
println(result)
}
You can also get things back from a function:
So if you want to get something back from a function.you need to declare it.for example:
fun max(a: Int, b:Int) : Int // this tells the compiler that the function returns an Int value
{
val maxValue = if(a > b) a else b
return maxValue; // you can not return string here you have to return Int only
}
Functions with no return value:
if you do not want to return a value you can either omit the return type from the function declaration or specify a return type Unit. Declaring ta return type Unit means that the function returns no value as in the example:
fun printSum(int1 : Int , int : Int2)
{
val result = int1 + int2
println(result)
}
or
fun printSum(int1 : Int, int2 : Int): Unit
{
val result = int 1 + int 2
println(result)
}
But if you try to return a value in function with no declared return type or a return type of Unit, your code won't compile.
Functions with single-expression bodies:
if you have a function whose body consists of a single expression. you can simplify the code by removing the curly braces and return statement from the function declaration. for example:
fun max(a: Int,b:Int) : Int
{
val maxValue = if(a > b) a else b
return maxValue
}
The function returns the result of a single expression which means that we can rewrite the function like below:
fun max(a:Int , b:Int):Int = if (a > b ) a else b
// here we have used = to say what the function returns and remove the {}'s
because the compiler can infer the function return type from the if expression we can make the code even shorter by omitting the : Int
fun max(a:Int,b:Int) = if (a > b) a else b
here the compiler knows that a and b are Ints So it can work out of the function return type from the expression
So you can ask more questions here like :
Bullet Points to remember:
So far we have seen the main function. So if you want to split your code into separate functions. So main function is actually launched your application as it the function that gets executed when you run it.So how to create a function.
For example;
fun test()
{
println("Test")
}
So once you have written the function you can call it from elsewhere in your application:
fun main(args: Arrays<String>)
{
test()
}
you can also send things to a function.you can tell the compiler what values a function can accept by your specification one or more parameters you can pass:
fun test(param: Int)
{
println("Paramater is $param")
}
// you can call it like
test(6)
You can also pass variables to a function so long as the variable type matches the parameter type:
fun main(args: Array<Strings>)
{
val x: Int =7
val y:Int =8
printSum(x,y)
}
fun printSum(int1: Int, int2:Int)
{
val result = int1 + int2
println(result)
}
You can also get things back from a function:
So if you want to get something back from a function.you need to declare it.for example:
fun max(a: Int, b:Int) : Int // this tells the compiler that the function returns an Int value
{
val maxValue = if(a > b) a else b
return maxValue; // you can not return string here you have to return Int only
}
Functions with no return value:
if you do not want to return a value you can either omit the return type from the function declaration or specify a return type Unit. Declaring ta return type Unit means that the function returns no value as in the example:
fun printSum(int1 : Int , int : Int2)
{
val result = int1 + int2
println(result)
}
or
fun printSum(int1 : Int, int2 : Int): Unit
{
val result = int 1 + int 2
println(result)
}
But if you try to return a value in function with no declared return type or a return type of Unit, your code won't compile.
Functions with single-expression bodies:
if you have a function whose body consists of a single expression. you can simplify the code by removing the curly braces and return statement from the function declaration. for example:
fun max(a: Int,b:Int) : Int
{
val maxValue = if(a > b) a else b
return maxValue
}
The function returns the result of a single expression which means that we can rewrite the function like below:
fun max(a:Int , b:Int):Int = if (a > b ) a else b
// here we have used = to say what the function returns and remove the {}'s
because the compiler can infer the function return type from the if expression we can make the code even shorter by omitting the : Int
fun max(a:Int,b:Int) = if (a > b) a else b
here the compiler knows that a and b are Ints So it can work out of the function return type from the expression
So you can ask more questions here like :
- Can I return more than one value from a function?. A function can declare only one return value but if you want to say return three Int values then the declared type can be an array of Ints(Array<Int>). Put those Ints into the array and pass it back.
- Do I have to do something with the return value of a function? Can I just ignore it? Kotlin does not require you to acknowledge a return value. you might want to call a function with a return type even though you do not care about the return value.in this case, you are calling the function for the work it does inside the function rather than for what it returns. you do not have to assign or use the return value.
Example:
fun main(args: Array<Strings>)
{
val options = arrayOf("One","Two","three")
val gameChoice = getGameChoice(options)
val userChoice = getUserChoice(options)
pritnResult(userChoice,ganeChoice)
}
fun getGameChoice(optionsParam:Array<String>) = optionsParam[Math.random() * optionsParam.size).toInt()]
fun getUserChoice(optionParams:Array<String>) : String
{
var isValidChoice = false
var userChoice = ""
while(!isValidChoice)
{
print("Please enter one of the following:")
for(item in optionParams) print(" $item")
val userInput = readLine()
if(userInput!=null && userInput in optionParam)
{
isValidChoice = true
userChoice = userInput
}
if(!isvalidChoice) println("you must enter a valid choice.")
}
return userChoice
}
fun printresult()
{
val result : String
if(userChoice == ganeChoice) result = "Anything you would like to print"
else if((userChoice == "One" && userChoice == "Two" && userChoice == "Three")
{
print("print something here")
}
else result = "You wnated"
println("You chose $ userChoice . I chose $gameChoice . $result ")
}
Bullet Points to remember:
- use function to organize your code and make it more reusable.
- A function can have parameters so that you can pass more than one value to it
- The number and type of values you pass to function must match the order and type of the parameters declared by the function.
- A function can return a value. you must define the type of value it returns
- A Unit return type means that the function does not return anything.
- Choose for loops over while loops when you know how many times you want to repeat the loop code.
- The ReadLine() function reads a line of input from the standard input system.it returns a String value, the text entered by the user.
- If the input system has been redirected to a file and the end of the line has been reached, the readLine() function returns null, null means it has no value or its missing.
- && means "and". || means "or" . !means "not".
No comments:
Post a Comment