What are functions to kids?
How do kids learn new things? Or What is the effective way to teach a kid? I am sure as parents you can tell many ideas but, Isn't it teaching step by step is one of the best ways? But what if we are working with computers or we are teaching them? Do we need to repeat the same steps every time? No, Not in programming. We are here to make everything convenient for us. In programming, we often use the same code more than once to repeat actions or add onto in order to execute more complex tasks. Functions allow us to teach the computer how to do something one time and easily reuse the code later.
Let us understand the concept of functions with an example:
Today due to a pandemic we have understood the importance of sanitization. In a hospital, every person who wants to enter has to follow the set of instructions.
- Wear your mask
- Go through the sanitizing booth for full-body sanitization.
- Give your Full name and mobile no.
Every time when a person enters the guard has to repeat the instructions to them. Which was a hectic job isn't it? Is there any way we can help them? Yes, we can. Can you notice the guard has to repeat a set of instructions? let's put all the instructions in a function and give them a meaningful name. Now every time a new patient visits the hospital, the guard could show them the board and tell them to follow the Entrance_Instructions function. Thus minimizing all the hard work of repeating each step again and again.
What will we learn?
- What is a function?
- How to name a function?
- How to create a function?
- What are Functions with Input?
- Why functions are important?
What is a function?
A function is a block of organized, reusable code that is used to perform some particular tasks. In the function, we combine some simple tasks to help us in more complex tasks. In sports, you can think about dribbling a soccer ball - putting together running and kicking, and naming it as one new action: Dribbling.
Let us understand this one more example taken from our CuriousJr App for function practice problem:-
There is a path we have to follow to reach the destination and also we have to collect 2 stars in the path. First of all, take a pen and paper and try to give instructions to reach the destination and don't forget about collecting stars.
Now Check your steps and see if there is a repetition of a set of steps. If yes, you are going right. Now let's give a name to that set of instructions so that whenever we have to use it, we will call its name.
Our lines of code also become less when used functions:
- Move Up and down
- Collect Star
- Turn Left
- Move Up and down
- Turn left
- Move Up and down
- Collect Star
- Turn Left
- Move Up and down
How to name a function?
According to a survey, an average coder spends 75% of the time for understanding the code, 20% of the time modifying the existing code, and only 5% for writing new code. Almost every programming language has a pre-specified naming convention(rules and regulations) and it is important to follow it for making it easy and meaningful to read our code. The names should be clear and it should define what the function does or its functionality specifically.
Let us understand this we made a function to instruct how to pack bags and named it to A() and another function to instruct how to skate and named it to lB(). Now while writing the program we have to use these functions but we don't know which function teaches skating and which teaches packing bag. Now what?
For better understanding, try to give these instructions a name that should be understood by any new programmer that is reading your code. I am sure you could come up with perfect names and as a reference, I will give them the name pack bag() and learn skate() respectively.
Are Loops and Functions Same?
A function is a piece of code you can run over and over again simply by calling it. Whereas a loop is a piece of code that runs over and over again until the condition is false. No, they are not the same they have some similar functionality. They both keep our code DRY(Don't Repeat Yourself) but has a different use. We can understand this with an example: Suppose you are watching TV(Television) till Electricity is there so using a loop we will say:
But just imagine Electricity is gone you come out of the loop or you stop watching TV but after one hour light comes again and you want to watch TV again then what you have to use that loop again? But remember our task as a programmer is to reduce the lines of code and making it more convenient. So if we put both instructions in a function and give it a meaningful name then we can call it any no. of times we needed.
What is ()? Why we are using it with functions?
These are called parentheses (opening and closing). In most programming languages, we use parentheses to represent the function. It acts as the identity of functions just like: Every Student in a class has a unique Identity No. or we can Roll No.
How to create a function?
In different programming languages, we have different syntax for creating a function. But for introducing the concept amongst the kids let's consider general steps that are common in almost every language.
- Deciding the purpose of the function - The main and first thing is identifying the use of the function in the program. Where we can use the function for better flexibility.
- Defining the function - By defining we mean, Writing the steps which we want to reuse with the help of functions.
- Calling the function - This is the part when we actually use a function. we can call functions any number of times. Without calling the function nothing will happen. Imagine it like you have written every instruction a patient has to follow every time they visit the hospital but Guard doesn't tell them to see the instructions on the board and they being unaware of the instructions will do nothing. Thus calling the functions is as important as writing the function.
What are functions with Input?
In most of the programming languages, Functions allow the passing of some parameters(understand it as an input) within the parentheses which we have discussed above. But What is the use of function with parameter? Don't get confused with the term argument or parameter or input. These are just synonyms our main objective is to understand the concept.
Let us understand this with a real-life example: Let us make a function for driving a car. Try yourself: Give the function a name and write the instructions for it.
Now, what if you want that driver drives at a particular speed. How you will convey this message through functions to the driver? The answer is by providing a parameter of speed in the function.
See this function to understand:
Now whatever speed we give as input in the function will be the speed we will be instructed to drive the car. As we can see in the image above in the right image we have shown how to pass input in function and in the left image we have passed a particular speed which included in our function. Can you understand the use of parameters in the functions now? Try to put parameters in other examples also.
Why functions are important?
When we have to repeat the same task in programming we have mainly two options:-
- Use the same set of statements every time you want to perform the task.
- Create a function to perform that task, and just call it every time you need to perform that task.
How the second option is better than the first tells us the importance of functions. We can also call them the feature of functions let us discuss them:
- It Improves the Readability of code - While writing code we should consider the readability one of the most important points as It becomes easy for beginners in programming like you to understand the concept more easily.
- Debugging is easy - Debugging means finding the errors and resolving them just like a superhero does in stories. But how debugging becomes easy? Let's say we don't have any features like function so what will we do? We will repeatedly write all the steps of that task whenever needed which means if we need to do that task ten times then our code becomes 10 times longer. When we have more lines of code finding error becomes a tough task.
- Fewer lines of code - As mentioned above if we don't have functions our programs become longer but one of our main aim of programming is better readability and using less line for better understanding and functions provide both.
- Reusability - This is the main feature of function which is why we are using a function because it helps us in DRY(don't repeat yourself) which is important for us when writing codes.
Above are some features of functions which show us the importance of function when you will use them you can find some more with your perspective.
We can conclude that Functions are small programs within a large program. We can reuse a piece of code by converting them into a function. Calling the function plays a key role in using the functionality of the function we declared. We can Call function in a program any no. of times. We can declare them with inputs also which helps us in providing a particular input to our function. They are useful for better readability, Fewer lines of code, Reusability, and easy debugging. We have given different examples for better understanding, try to find some yourself. let your brain have some exercise.
Keep following CuriousJr for more updates on Coding for Kids.