Recommended Books

C++ Functions



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.


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 are int, string or void. 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

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 and 8 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

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.

Leave a Reply

Your email address will not be published. Required fields are marked *