In continuation with our programming series, we are going to talk about Function – much of all that it entails. If you want to learn how to code, then understanding functions is really important. The same applies to current programmers who tend to copy their code quite often to use in a different section of their work.
Learning how to use functions means the coder will know how to work more efficiently. Not only that, but the code will be easier to read, and that is a boon if you’re working in a team.
What is a Function in Programming?
In basic terms, a function is a block of code that performs various tasks. Should you need to, a function can be called and reused numerous times. To make things even more interesting, coders can pass information to a function with ease, but not only that, but it is also possible to send information right back.
At the moment, many of the popular programming languages have this feature built-in, which is expected at this point.
Now, whenever a function is called, the program will usually pause the currently running program and implement the function. From there, the function will be read from top to bottom, and once the function has completed its task, the program will continue from where it had paused.
Should the function send back a value, that particular value will then be used where the function was originally called.
Read: What is Java Programming language?
How to write a Void function
OK, so writing a void function is super easy and can be done in a short amount of time. Bear in mind that this function does not return a value. Let’s look at a few examples that might give you an idea of what to do.
JavaScript Example
function helloFunction(){ alert("Hello World!"); } helloFunction();
Python Example
def helloFunction(): print("Hello World") helloFunction()
C++ Example
#include <iostream> using namespace std; void helloFunction(){ cout << "Hello World!"; } int main(){ helloFunction(); return 0; }
Read: What is the R programming language?
How to write Functions that require a value
If you are writing the same piece of code several times throughout your work, then void functions are perfect for that. However, these types of functions do not change, which doesn’t make them super useful. The best way to make void functions more beneficial is to increase what they can do by sending different values to the function.
Python Example
def helloFunction(newPhrase): print(newPhrase) helloFunction("Our new phrase")
JavaScript Example
function helloFunction(newPhrase){ alert(newPhrase); } helloFunction("Our new phrase");
C++ Example
#include <iostream> using namespace std; void helloFunction(string newPhrase){ cout << newPhrase; } int main(){ helloFunction("Our new Phrase"); return 0; }
Read: Best Programming Principles & Guidelines all Programmers should follow.
How to write a Function that returns a value
The final aspect of this article, then, is how to write a function that will return a value. Whenever you want to alter data before using it, then this is the way to go in most situations.
Python Example
def addingFunction(a, b): return a + b print(addingFunction(2, 4))
JavaScript Example
function addingFunction(a, b){ return a + b; } alert(addingFunction(2, 4));
C++ Example
#include <iostream> using namespace std; int addingFunction(int a, int b){ return a + b; } int main(){ cout << addingFunction(2, 4) ; return 0; }
Read: The best projects for beginner Programmers.
Have fun testing the codes we’ve listed here. We hope they will prove useful in your work.