‫Welcome back.
‫In this section we're going to talk about functions in Kotlin.
‫In Kotlin, functions are blocks of code that encapsulate a specific task or functionality.
‫Functions are a fundamental building block of any Kotlin program, and they allow you to organize your
‫code into reusable and modular components.
‫You can create functions to perform various tasks, and you can define functions with parameters and
‫return values.
‫To declare a function in Kotlin, you use the fun keyword followed by the function name, a list of
‫parameters enclosed in parentheses, a colon, and the return type.
‫So this is the general syntax of the function.
‫Again, to create a function, we declare a fun keyword followed by the function name a list of parameters.
‫We have two parameters and you specify the type of these parameters.
‫A colon and the return type if the function returns a value.
‫Of course, if a function doesn't return anything, you can specify the return type as unit, which
‫is similar to void in Java and other programming languages.
‫Then you specify the function body, perform some operations, and optionally return a value.
‫So this is the general syntax of creating a function.
‫Let me create a real function following this syntax starting by fun.
‫Say hello and here I'll use one parameter of type string.
‫You can see parameter one is name.
‫Type is a string and this function will not return anything.
‫So it's void or unit.
‫And you can remove this unit.
‫And you don't specify any return type.
‫So Kotlin will not return anything when calling this function.
‫Then I need to print ln.
‫And here I am adding function body and performing some operation.
‫And this operation is printing.
‫Hello.
‫And I'm going to use this parameter.
‫You can specify zero or more parameters within the parentheses.
‫Each parameter consists of name and type.
‫Parameters allow you to pass values into the function for processing.
‫The function body contains the code that defines the functionality of the function.
‫You can perform operations, calculations and other tasks within the function body.
‫To use a function, you simply call it by its name and the provide required arguments, if any.
‫If the function returns a value, you can assign it to a variable or use it in an expression.
‫For example, I need to cut this and outside the main function and you can see this is a function called
‫main and we talked about it.
‫I need to call this function inside the main function.
‫So for that I need to specify the name of the function.
‫Say hello.
‫And here I need to pass an argument.
‫Here it's called parameter.
‫When defining a variable here.
‫When you pass the value of this parameter it's called argument.
‫Say hello I need to pass Jack.
‫Jack is a string and you can see I'm matching the type of the parameter with the passed argument.
‫If I specify 1 to 4, it's an integer and the required type is a string.
‫So there is an error.
‫I need to specify a string.
‫Here I need to pass jack.
‫Let me disable hints.
‫Run the program.
‫And here we go.
‫Hello, Jack.
‫I'm passing Jack to here and replacing this name by Jack.
‫And then get use of this parameter Jack and printing it to the console.
‫Okay.
‫This is how we create functions.
‫How to call functions, how to pass arguments to the parameters in the function.
‫Also, you can provide default values for function parameters, allowing you to call function without
‫specifying all arguments if their values are not required.
‫For example, here I need to pass age, which is of type string equals to not specified.
‫If the user didn't specify any argument here, like for example, 30 enclosed in double quotation because
‫it's of type string, the value would be not specified, but if the user specified 30 when calling this
‫function, it will be assigned to this parameter.
‫Let me show you if I run the program.
‫Hello, Jack, your age is 30.
‫If I remove 30.
‫And by the way, you don't have any error because here I specified the default argument for this function.
‫Let me run again.
‫Hello, Jack, your age is not specified, so if I don't specify any value here for the second parameter,
‫the not specified which is the default argument will get its value as not specified.