DEV Community

eli-shi
eli-shi

Posted on

Functions (Javascript)

Functions are another thing you are going to be using constantly as a programmer. Functions are small parts of code that take arguments (values) and work with those values to come up with a new value, this means we can reuse the same set of operations and actions on different values without having to repeat the code. To use them, you have to first declare a function, mention the name of the function, the values(arguments) it has to take in, and the actions that the function should perform when called. You can call a function, by writing the name of the function name and mention values if the function requires values. Parameters, are arbitrary letters that you mention in the arguments for a function, when you wish to be able to call the function and manualy input argument values.
Any global values can be used inside a function, however, make note that any value with the same name that is declared within the function will take priority in the functions operations. Also, if you haven't read up on varaibles, you should know that any value that is declared within a function has local scope, which means it can only be visible within the function and not outside of it, this is due to the fact that when you create a function, you create a new local scope.
If you would like to recive a value from your function then you must use the return statement, which will return whatever value you mention after typing 'return'. A function can only return one variable. If you choose to not return any value from a function, then the return value is considered undefined.
Functions that go into a loop until certain conditions(usually while or if-then conditions) by calling themselves, are considered recursive functions.
You can also duplicate any function by simply referencing the original function to a new variable.
Good luck with working with functions. Practice! Practice! Practice!

Hakuna Matata!

Top comments (0)