DEV Community

loading...
Cover image for Function in Every Programming Language

Function in Every Programming Language

iamrishavraj1 profile image Rishav Raj Updated on ・4 min read

What is Function?

Alt Text

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

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()
Enter fullscreen mode Exit fullscreen mode

Alt Text
2. Come back to Home

move()
move()
move()
moveDown()
moveDown()
moveRight()
Enter fullscreen mode Exit fullscreen mode

Alt Text

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

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

Time to use Superpower

Alt Text

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

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

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

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

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

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

Output

move
moveUp
moveUp
moveRight
moveRight
Stop
Go and Buy 2 litre milk.
Enter fullscreen mode Exit fullscreen mode

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

Output

move
moveUp
moveUp
moveRight
moveRight
Stop
Go and Buy 2 litre orange juice.
Enter fullscreen mode Exit fullscreen mode

Congratulations 🎉

Alt Text

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

Enter fullscreen mode Exit fullscreen mode

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);
  }
}
Enter fullscreen mode Exit fullscreen mode

Let's Connect With Me

Alt Text

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/

Discussion (14)

Collapse
phantas0s profile image
Matthieu Cneude

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).

Collapse
iamrishavraj1 profile image
Rishav Raj Author

Thank you for explaining 😃

Collapse
luckyarthas profile image
LuckyArthas

Thank you.

Collapse
iamrishavraj1 profile image
Rishav Raj Author

Your Welcome ☺️

Collapse
luckyarthas profile image
LuckyArthas

Your function declaration is perfect.
Please write more for people.
Thanks again.

Thread Thread
iamrishavraj1 profile image
Rishav Raj Author

Thanks again and Don't Forget to Share 🤠

Thread Thread
luckyarthas profile image
LuckyArthas

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?

Thread Thread
iamrishavraj1 profile image
Rishav Raj Author

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.

Thread Thread
luckyarthas profile image
LuckyArthas

Thanks

Collapse
darkphantom7750 profile image
Rushan S J

This is amazing ! Great job. Looking forward to more of your posts

Collapse
iamrishavraj1 profile image
Rishav Raj Author

Thank You So Much 😊❤
and Don't Forget to Share with your friends and family and get them benefit from it too😊👍

Collapse
micheal_claxton_2fbf4e67c profile image
Micheal Claxton • Edited

Thanks for writing this, but honestly, the English is very bad and can interfere with comprehension of the subject matter. if you need a proof reader, let's exchange skills.

Collapse
iamrishavraj1 profile image
Rishav Raj Author

Thanks Brother. next time i wll be improve it 😀❤️

Collapse
micheal_claxton_2fbf4e67c profile image
Micheal Claxton

if you need help I am happy to offer it. Let's exchange skills. I appreciate the article and am learning from it

Forem Open with the Forem app