This article covers Functions in C++.
What are Functions?
A function is a reusable block of code that runs only when called. Functions save time, increase code re-usability and makes your code simpler and easy to read.
Functions make use of arguments and parameters to receive data and output a result accordingly. This allows for a greater deal of flexibility on your code.
C++ Functions Syntax
return_type function_name (parameter1, parameter2,...) {
// Statements to be executed
}
return_type
determines the data type of the value returned by the function. Some of the possible values areint
,string
orvoid
.void
is used if the function will not return anything.function_name
is the name used to call the function.- Within the parenthesis the parameters of the function are defined. These parameters are variables which hold data to be used in the function body. These “values” or “data” are passed while calling the function. There is no limit to the number of parameters that may exist for a function.
- The curly brackets define the function body. The code inside this will run every time the function is called.
Arguments and Parameters – The difference
These two terms are very interchangeable with each other, so knowing the difference isn’t that big a of deal. The terms they refer to are almost identical.
Typically however, the Parameters are the variables present in the parenthesis during method definition. When a method is called, the Arguments are the data you pass into the method’s parameters.
There is no limit to the number of Arguments/Parameters that can be written. There may even be no parameters at all, depending on the purpose of the function you’re making.
Defining a Function in C++
Below is an example of simple function in C++. We’ve made our own addition function which takes two integers, x
and y
and returns the result.
#include <iostream>
using namespace std;
int add(int x, int y){
return x + y;
}
int main () {
cout << add(5,8);
return 0;
}
The output.
13
Some points to note here.
- Returning the value does not print it to the screen.
- Both parameters must be declared with an appropriate data type.
- The input arguments
5
and8
are separated by a comma. - The number of input arguments and parameters must be the same.
Functions with no arguments
Functions which have no return value, are sometimes referred to as procedures.
The Function below performs the simple task of displaying a “Hello World” prompt to screen. Also note the use of void
to declare it as a Function with no return value.
#include <iostream>
using namespace std;
void display(){
cout << "Hello World";
}
int main () {
display();
return 0;
}
Hello World
C++ Functions with Templates
Having to define the datatypes for the parameters of a function can get a little irritating when you want to reuse the function for different data-types. Luckily, there is an easy fix.
Using the “C++ Templates” we can overcome this problem by generically specifying the data-type. Here is an example with an “addition function” which can accept any datatype.
template<typename T>
T add(T n1, T n2) {
return n1 + n2;
}
int main() {
cout << add(4,2) << endl;
cout << add(5.5, 4.3) << endl;
}
To learn about Generic programming and templates, follow the link. On a similar note, you can also check out “variadic templates” which is a technique that allows you to pass any number of parameters to a function without defining them.
This marks the end of the C++ Functions article. Any suggestions or contributions for Coderslegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.