Open in App
Not now

# What are the Helper functions ?

• Difficulty Level : Medium
• Last Updated : 08 Dec, 2022

The functions that perform some part of the computation of other functions are termed helper functions

They are named as per their work, i.e. they help the main function to look more clean, precise, and readable by giving descriptive names to some of the computations involved. Also, helper functions once declared, can be used anywhere in the program, thus, enhancing the reusability of code.
The following examples would make the use of helper functions clear.

Example 1: Let us suppose we have to write a function that takes an array of strings str as an input and returns another array pal, where pal[i] is “Yes” if str[i] is a palindrome and it returns “No” otherwise. There can be two ways to solve this problem, one without using any helper function and the other using it.

## Javascript

 ` `

Output:

```gfg Yes
article No
geeksforgeeks No
racecar Yes```

## Javascript

 ``

Output:

```gfg Yes
article No
geeksforgeeks No
racecar Yes```

Example 2: As a second example for the usage of helper functions, let us assume we need to print the average of various parameters such as height, weight, etc. of players in a team. One way would be writing the same code again and again to compute the average for each parameter which would clutter the code.
The other way would be to use a helper function that calculates the average of the array passed to it as an argument. The helper function can be invoked in the main function used to calculate and print averages. This enhances the code’s reusability.

## Javascript

 ``

Output:

```Average Height - 172
Average Weight - 70
Average Age - 21```

## Javascript

 ``

Output:

```Average Height - 172
Average Weight - 70
Average Age - 21```

Now, it becomes clear from the examples above that how helper functions help us to enhance code reusability, write clean code and improve code readability. Also, a helper function once defined, can also be used in various other functions which require similar computation.
Thus, whenever we find the same computation multiple times in any function or program, we should create a separate (helper) function for that computation and call that function instead to make the code more readable and clean.

My Personal Notes arrow_drop_up
Related Articles