OneBite.Dev - Coding blog in a bite size

declare a simple function in C++

Code snippet on how to declare a simple function in C++

  int addTwoNumbers(int a, int b) {
      return a + b;
    }

This code declares a simple function in C++, which will take two integers (a and b) as parameters and return the sum of those two numbers. The function is given the name “addTwoNumbers”. To ensure the function returns a value, the keyword “return” is used to return the value of the sum of the two numbers. The function is enclosed in curly braces ”{}” which keep the code within the function and prevents it from being used outside the function. Inside the curly braces, the value of a plus b is calculated and returned.

c-plus-plus