What is Function?
In Simple Word, The Function is a block of code that we reuse at any time by calling them by their name.
Why we Need Function?
Imagine that you are a robot. And you work according to the code written by your boss.
Your boss need a milk in the morning, so they give you an introduction to buying milk from the shop then come back to home. For this, they write lots of lines of code.
Example
1. Go to Market
move()
moveUp()
moveUp()
moveRight()
moveRight()
move()
move()
move()
moveDown()
moveDown()
moveRight()
In above, you see that the same code is being repeated multiple times, and this is not a good way to writing a code. Every good programmer uses the DRY (Don't Repeat Yourself) rule to write code.
And the other problem is that your boss needs milk every day, so they will give you introduction daily to go and a buy milk, for this they have to write the same code every day. But it will take a lot of time and a lot of hard work.
Time to use Superpower
Now is the time to understand how the superpower Function works and how it helps your boss avoid writing the same lines of code again and again.
To use a function, we need to define the function first, after that we have to the call the function name to run the code written in function body.
Defining a Function
Before using a Function we should define the Function first. Here I am using JavaScript to define the function. If you don't know JavaScript, don't worry after knowing the concept you can use the function in any other programming language. The syntax is different in other programming languages but all concepts are the same.
Different programming languages name them differently, for example, functions, methods, sub-routines,etc.
The important thing is that you should know the concept of the Function.
Declaring Function Keyword and Function Name
Here you see that I have used the function keyword to define a Function which is a reserved keyword in JavaScript, in other programming languages the keyword may be different.
After writing the function keyword, I declare the function name. You can declare any name according to your code or project.
Parameter List and Function Body
After declaring the function keyword and function name you saw the bracket after the function name in that bracket we pass the value name as a Parameter.
Example
In your case your boss sometimes needs a different items from the shop, so they saved the parameter name as an item and itemQuantity so that they can change the value of the item and their quantity according to their need when the function is called.
Calling a Function
After defining the function and writing the code of instructions to be given to the robot, we now have to call it to use the function.
Here you see that we call the function by its name and pass the values of the parameters that we have defined earlier.
The value we write after the name of the function is called the argument. You can change the values according to your need.
Final Code
Output
move
moveUp
moveUp
moveRight
moveRight
Stop
Go and Buy 2 litre milk.
Now, you have completed the code using the function. From the above example your boss needs one milk every day, so now they write the code using the function and call the function to work according to the instructions given in the code.
Your boss can change the item according to his requirement by changing the values in arguments like orange juice instead of milk.
Example
Output
move
moveUp
moveUp
moveRight
moveRight
Stop
Go and Buy 2 litre orange juice.
Congratulations 🎉
You have understood the concept of the Function now. So you can use this concept in any programming language and like every good programmer in the world, you can also apply DRY rules in your code.
Keep Coding 👨💻
Let us see how a function is defined in another programming language.
In Python
def goToMarket(item,itemQuantity):
print("move")
print("moveUp")
print("moveUp")
print("moveRight")
print("moveRight")
print("Stop")
print(f"Go and Buy {item} {itemQuantity} litre .")
goToMarket("orange Juice", 2)
In Java
public class Main {
static void myMethod(String item, int itemQuantity) {
System.out.println("move");
System.out.println("moveUp");
System.out.println("moveUp");
System.out.println("moveRight");
System.out.println("moveRight");
System.out.println("Stop");
System.out.println("Go and Buy " + item + " " +
itemQuantity + " " + "Litre");
}
public static void main(String[] args) {
myMethod("orange Juice", 2);
}
}
Let's Connect With Me
Thank You for reading this blog 😀
I hope all of you have benefited after reading this blog 🎉
Do Share it with your friends and family and get them benefit from it too 🧡👍
Here You Connect with me https://connect.rishavraj.codes/
Top comments (14)
A procedure and a function are not the same things in every programming language.
A procedure is just a block of code you execute (without returning anything), a function always return something.
The language Pascal for example makes a difference between the two. I know, not many people use Pascal anymore, but still.
In Mathematics, a function has a stricter definition and some functional programming languages try to stick to this definition (like Haskell for example).
Thank you for explaining 😃
Thank you.
Your Welcome ☺️
Your function declaration is perfect.
Please write more for people.
Thanks again.
Thanks again and Don't Forget to Share 🤠
This is my first time Dev.to and your Blog is first that I read here.
Could you help me how to use Dev.to?
And here share mean?
If You want to write a blog then go to top of the home page amd click Write button and if you want to share the someone blog then open that blog you will get the share button.
Thanks
This is amazing ! Great job. Looking forward to more of your posts
Thank You So Much 😊❤
and Don't Forget to Share with your friends and family and get them benefit from it too😊👍
Some comments may only be visible to logged-in visitors. Sign in to view all comments.