Functions with Parameters

Beh Kh.
A free video tutorial from Beh Kh.
Software Engineer | Instructor
4.3 instructor rating • 3 courses • 16,377 students

Learn more from the full course

Android app development for beginners (with Kotlin)

Kotlin Android Apps Development (Beginner to advanced)

13:00:37 of on-demand video • Updated July 2019

  • You will learn how to install tools
  • You will learn Kotlin for Android
  • You will learn how to develop Android apps
  • You will learn how to Implement 4 real apps
  • You will learn how to use database in your apps
  • You will learn how to publish your apps on Google Play
  • 1- Installing and Setting Up Tools
  • Introcucing tools (IDE + Android SDK + Emulator + JDK)
  • How to download and install required tools
  • Creating a new Android Studio project
  • How to change the font of code editor
  • What is an activity
  • What is androidmanifest file
  • Developing your first Android app
  • Attributes panel
  • Code completion list
  • Displaying a toast
  • Running your app on an emulator
  • Examining different parts of an emulator
  • Send sms and phone calls between two emulators
  • Enabling directional pad
  • Cloud-based testing
  • Installing Google USB driver
  • Enabling developr options
  • Connecting a real device to the Android Studio
  • 2- Kotlin _ Say Hello to Basics
  • Int data type
  • Declaring and initializing variables
  • Single line comment
  • var vs val
  • print() function
  • Floating point data type
  • Boolean data type
  • String data type
  • Type inference system
  • Naming conventions (Camel Case)
  • Data member and function member
  • dec() function
  • compareTo() function
  • Intellisence
  • Argument
  • replace(string) function
  • replace(char) function
  • char data type
  • toInt() function
  • toIntOrNull() function
  • NumberFormatException
  • toFloatOrNull() function
  • What is Null?
  • Kotlin Nullable types
  • Arithmetic operators
  • Operator precedence
  • Assignment operators
  • readLine() function
  • 3- App ( Tip Calculator ) + Android Basic Views
  • TextView
  • Seekbar
  • EditText
  • Button
  • Set constraints for views
  • Resolving the "Hardcoded strings" warning
  • Converting text to number
  • Working with seekbar's listeners (setOnSeekBarChangeListener)
  • Concatenating strings, variables and expressions with the + operator
  • Concatenating strings, variables and expressions with String Templates
  • 4- Kotlin _ Control Flow
  • If expression
  • If Else expression
  • Relational operators
  • Gutter area
  • Debugging the apps by using breakpoints
  • Transfer of control
  • How to check if a numbers is even or odd
  • Logcat tab
  • Toast messages
  • Statement vs Expression
  • lift out assignment
  • Any data type
  • When expression with arg
  • When vs If
  • When expression without arg
  • Combining concitions in a when expression
  • An app that determines if a number is prime number
  • How to hold a range of values (double dotted and in operators)
  • Using the when expression with ranges
  • Logical AND operator
  • Useful Android Studio shortcut keys
  • Defining range variables
  • 5- Kotlin _ Functions
  • Simple functions
  • Functions with Parameters
  • Functions with return type
  • Function signature
  • Parameter vs Argument
  • Userful Android Studio shortcut keys
  • Multi line comment
  • 6- Kotlin _ Immutable and Mutable Collections + Loops
  • Defining and initializing the Arrays
  • Using the arrays
  • Manipulating the array elements
  • Any data type
  • ArrayIndexOutOfBound exception
  • Data types and performance
  • How to define an array type explicitly
  • The second way of declaring and initializing an array
  • Iterating over arrays by using the for loop
  • Destructuring declaration
  • Iterate through a Range
  • How to get the numbers of an array elements
  • withIndex() function
  • Until keyword
  • listOf() function
  • mutableListOf() funciton
  • Array vs Collection
  • mutable vs immutable
  • 7- Kotlin _ Classes + Enums + Exception Handling
  • Declaring classes
  • How to create objects
  • Constructors
  • Iterating through an array of objects
  • Vertical selection
  • Property and method
  • Class header
  • Primary constructor
  • Declaring properties from the primary constructor
  • Class diagram
  • How Enums make things easier
  • Happy path
  • Try Catch block
  • Multiple catch blocks
  • Finally block
  • 8- App ( English Stories ) + Activities + List View + Singleton Pattern
  • Displaing data into a Listview
  • ListView click listener
  • How to creating a new Activity
  • Passing data between activities
  • Scrolling toolbar
  • How to set the start up Activity
  • FAB (Floating Action Button)
  • Snakbar
  • Singleton pattern
  • Private visibility modifier
  • Public visibility modifier
  • Overriding the functions
  • String controls characters
  • 9- App ( My Notes ) + SQLite Database + Activity Life Cycle + Action Bar + Alert Dialog
  • Set the Margin attribute for views
  • Designing SQLite databases for Android apps (Fields, Records, Tables, ...)
  • Extending classes
  • SQLiteOpenHelperClass
  • TODO() functions
  • NullPointerException
  • Check null values
  • Safe call operator
  • Not null assertion operator
  • Using readble connetions to read data from database
  • Using writable connections to write data to database
  • OnDestroy() funciton
  • Performance tune-up while working ith databases
  • Variables' scope
  • Up button
  • <Meta Data> tag
  • How to add menu to Action Bar
  • Working with Asset studio
  • Refactoring resource names
  • XML namespaces
  • match-constraint attribute
  • Hint attribute
  • Setting EditText text attribute
  • Changing the EditText focus programatically
  • How to check if an EditText is empty
  • Inserting new records into the database
  • Up button vs Back button
  • Activity life cycle
  • Navigating clipboard items
  • Refactoring functions
  • How to close an Activity programatically
  • Alert Dialog
  • Updating and Deleting records from database
  • 10- App ( Best Quotes ) + Recycler View +Card View + Intents + Guidelines + Unicode Characters
  • How to insert default values into the database
  • Designing layouts by using a Card View
  • Linear Layout
  • Image View
  • Layout Margin attribute
  • Writing a customized Adapter for Recycler View
  • Recycler View vs List View
  • Inner classes
  • findViewByID() function
  • Arranging and managing Recycler View items by using LayoutManager
  • Using the customized Adapter View
  • Passing functions in Kotlin as parameters
  • Guidelines in constraint layout
  • On item click listener for RecyclerView
  • Creating DB in memory
  • Explicit intent and Implicit intent
  • Ripple effect
  • Share data with other apps
  • 11- App ( Animating a Spaceship + Best Quotes ) + Animation in Android + Splash Screen
  • Alpha animation
  • Translate animation
  • Rotate animation
  • Scale animation
  • Companion objects
  • Creating Splash Screens by using Lottie library
  • Using third-party libraries
  • Assets folder
  • Application context vs the this keyword
  • noHistory attribute
  • 12- Publishing Your App
  • Parallex effect
  • Safe zone
  • Legacy icons
  • Adaptive icons
  • Debug key vs Release key
  • Signing the APK
English Hi again in the previous video you saw how to write a function in its simplest form. Now you know a function is a series of statements inside a body that this body has a name. Note that this part of a function is called the function signature. In this video I'm gonna teach you how to extend the function and also teach you the advantages of using functions. Let's start. OK we have a function here that calculates the area of a rectangle with a width of two and a length three. Suppose that we also need to calculate another rectangle's area with the width of 10 and the length of 20. What's your solution. Do we need another function to do this. Something like this let's use println and run the code again. OK Do we need to define another function with a different name but the same body? Note that we can't have functions with the same function signature. Maybe the solution works, but it's not the best solution. In such case that you need to perform the same task more than once on various inputs that in this case inputs are we width and length, we should change the function as follow. Let's remove the extra function we don't need this anymore and let's change the function so that it can works on various widths and lengths. Note that at the moment this function only works on numbers 2 and 3. We need to change the function so it can work on various inputs. To do so, we need some changes here in the function definition and also we need some changes here where we're calling the function. Ok, here we need to define 2 integer parameters in the function inside the parentheses. These parameters are supposed to get a width and a length. So we should declare a variable here write its name for example width colon its data type. Note that the name is arbitrary and we also need another parameter. So type comma and declare another variable so write its name length colon its data type that is int. Note that the data type of a parameter can be of any type. For example string or boolean. But in this case we need the int data type. OK. No our function needs two parameters passed to it to work correctly. This is why here, where we have called the function is an error that says no value passed for parameter L and no value no value passed for parameter W. So we'll have to pass an integer number as a width and an integer number as a length to the function. So Type 2 as the width and 3 as length. Parameters allow us to share values with the function so we don't need these variables anymore. Instead we have to use the name of the parameters here w as width and L as length. Note that these names are arbitrary so we can change it to width and length. As you see changes are reflected here. Okay so we need to change it to width and length. okay. Now let's run the app again. And back to explain Ok, as you say the result will be displayed here. What's happening is that when we run the app it starts to execute the main function. Inside the main function we're calling the calculate rectangle area function with two values. So the code execution moves inside the function's body with these values 2 and 3. So calculation will be based on two for width and three for length. In order to have a better understanding of how it works, I debug the app once. So let me put a breakpoint here in the gutter area and start debugging so I select debug instead of run. okay. As you see after the debugging starts, the code execution stops here. Note that this line of code is not executed yet. After pressing the F7 key or this button the code the selected line of code will be executed. So if I press the F7 key the code execution moves into the function's body with these values two and three. Note that here it says that width has a value of two and length has a value of three. Because we are passing these values from here where we are calling the function. So the area will be calculated based on numbers that are passed with. OK. Let me press the F7 key and F7 key again. If I back to the console tab you will see that this message is displayed here and after pressing the F7 key again the code execution back to where the function is called okay. Now that our function accepts parameters we can reuse these block of code for various values. So for example if I need to calculate the area for numbers 10 and 20 I can easily type the function's name again and pass values 10 and 20 to its parameters list so if I run this code the calculate rectangle area will be executed twice once for values 2 and 3 and once for values 10 and 20. So the output will be like this. We can also change the println and like this the area for width width and length length is area. OK let me run it again. Now the output messages will be meaningful OK the area for width 2 and length 3 is 6 and the area for width 10 and length 20 is 200 So when I run the code the compiler first executes the calculate rectangle area function for values 2 and 3. After that it executes the function again for values 10 and 20. What's happening is that we used the same set of statements every time we need to perform the task let me debug it to see the code execution, line by line. So I put a breakpoint here in the gutter area and debug the code and it start to debugging the code. Ok. As you see the code execution stops here. So I press the F7 key to execute the current line that is highlighted by this blue line. So I press the F7 key once, as you see the code execution moves to the calculate rectangle area's body. F7 key F7 key the area for width two and three is six after pressing the F7 key the code execution backs here so by pressing this button again the code execution executes the body of this function of the calculate rectangle area function again with values 10 and 20 so this message is printed here. So now you can write functions that accepts parameters so they can work with various values. Note that these values are called arguments and these variables are called parameters. So a parameter is a variable in a function definition and argument is the data you pass into the function's parameters list. Parameters are arbitrary both in number and type. You can have any number of parameters with any date type. The final note is that this part of a function is called the function signature.