Function in Every Programming Language
Tags: programming, codequality, computerscience
Published: 2021-03-18 16:01:35

#What is Function? 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5m02z2xp3wogwoo8z6cv.gif)

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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/24ozhpz2lzqw7au1hmpt.png)


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()
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5rn28m6kyyq1srm8awed.png)
**2. Come back to Home**
```
move()
move()
move()
moveDown()
moveDown()
moveRight()
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/35vnh4pq7drodts700rt.png)

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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vs5m2iylimhitiz36rw6.png)

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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/80t4n131mogx0btlp0p4.png)


#Time to use Superpower

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8f6cz2uvdy54r9t7niq.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x5obrzmnohh0m1zgfafx.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/axctjph4yxdkagqq6h06.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d0iuio0p5o2d8tyu72f1.png)


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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7wrojsrgc1q25s6z396.png)

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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0sbatwmmkih6sflsi9lj.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jkoujdc9ll06q8zb05cm.png)


###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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbb4fklnweiwo8io3xby.png)

###Output
```
move
moveUp
moveUp
moveRight
moveRight
Stop
Go and Buy 2 litre orange juice.
```


#Congratulations 🎉

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kmd0r4gehmudnqe1z5x5.png)

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 👨‍💻

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tysygm40iahopabh3pda.gif)
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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m2iygj9h6g2a2df37q4m.png)

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/