C++ Functions

In this lesson, we will learn about C++ functions and why we need a function, types, and overloading, with examples to better understand the lesson.


C++ Functions

A function is a unit of code that carries out a particular task, a section of code that only executes when called.


Why is a function important?

Reusable: Functions are used to carry out certain activities and are crucial for code reuse. Programmers can use code definition several times and run an identical piece of code more than once, thanks to functions.

Easy to understand: A program becomes simple to learn and reuse since it breaks a difficult problem into smaller components.

Easy to share and use by other programmers: Large programs are divided into smaller parts by functions. Multiple programmers can share and use functions.


C++ function Types

The function can be of two different types:

  • C++ Common Library functions
  • C++ User-defined functions
  • C++ User-defined functions

In C++ and programming in general, programmers can define their functions. A piece of code for carrying out a certain task is gathered into a group by a user-defined function, which is given a name (identifier). The code defined in the function’s body is executed when the function is called. Before a function can be utilized (called) anywhere in the program, it should have the following parts in C++:

  • C++ Function Declaration and Definition
  • C++ Function Call
  • C++ Function Parameters
  • C++ Return Statement
  • C++ Function Prototype

C++ Function Declaration

The basic syntax to declare a function in C++ is:

returnType function_Name (argument1, argument2,...) {
// C++ function body
}

Or

returnType function_Identifer(argument1, argument2,...) {
// C++ function body
}

Example

// C++ function declaration
void myFirstProgram () {
cout << "My First Program ";
}

In the above example,

  • The function identifier is known as myFirstProgram()
  • The function’s return type is void.
  • It has no arguments because the parenthesis is empty.
  • The function’s body is written inside the function parenthesis.

C++ Function Call

A function called myFirstProgram() has been declared in the program. We must call the myFirstProgram() function to use it. Here is how to call the myFirstProgram() function from the main.

int main() {
// C++ function call
myFirstProgram ();
}

C++ Function Call Example

#include <iostream>

using namespace std;
void myFirstProgram() {
  cout << "My First Program."; // C++ function declaration
}
int main() {
  myFirstProgram(); // C++ function call
  return 0;
}

Output

My First Program.

C++ Function Parameters

A function can be declared with parameters. A parameter is a piece of data passed when a function is declared.

Example

The basic syntax of a function with parameters in C++ is:

void addNum(int a, int b) // function definition with parameters
{
  int result;
  result = a + b;
  cout << "Result = " << result;
}

Here, the int variables a and b are the function addNum parameters.

We pass a value to the function argument when we call a function.

int main() {
addNum(1, 2) ;// C++ function call with values 1 and 2
return 0;
}
Note
When invoking a function, the arguments given must match the type of the corresponding parameters specified in the function declaration.

In the program below, we used a function with two int parameters, a and b. The datatype at the time of function definition and call is an integer. Then, we pass the values of arguments 1 and 2 in the main.


Example C++ Function Parameters

#include <iostream>

using namespace std;
void addNum(int a, int b) // function definition
{
  int result;
  result = a + b;
  cout << "Result = " << result;
}
int main() {
  addNum(1, 2); // C++ function call with parameter values
  return 0;
}

C++ Return Statement

We used the function declaration keyword void in the applications mentioned above. For instance,

void addNum(int a, int b) // function definition
{
  int result;
  result = a + b;
  cout << "Result = " << result;
}

The above code indicates that the function returns no value. It is also possible for a function to return a value. For this, we must provide  returnType for the function during function declaration.

int addNum(int a, int b) {
  int result;
  result = a + b;
  cout << "Result = " << result;
  return result;
}

A value from a function can then be returned using the return statement.

Instead of the void, we have the data type int in this case, indicating that the function returns an int value. The function’s code will end after the return.

Example

#include <iostream>

using namespace std;
int addNum(int a, int b) // function definition
{
  int result;
  result = a + b;
  cout << "Result = " << result; //return sum in result
  return result;
  cout << "Result after return = " << result; //return sum in result
}
int main() {
  addNum(1, 2); // C++ function call
  return 0;
}

Output

Result = 3

C++ Function Prototype

At this point, you were aware of the distinction between a declaration and a definition. A function prototype in C++ is a declaration of the function that informs the program about the ‘number,’ ‘type of arguments’, and the ‘type of return value’. The prototype declaration resembles a function definition but lacks a body of code.

The function prototype gives the compiler details about the name and parameters of the function. Because of this, we can call a function using code even before it has been defined.

A C++ function prototype’s basic syntax is:

returnType funName(arg1, arg2, ...);

The code above is similar to the C++ return statement. The function is defined here after the function call, which is the only distinction. For this reason, a function prototype was employed in this case.

Example

#include <iostream>

using namespace std;
int addNum(int a, int b); // C++ function prototype
int main() {
  addNum(1, 2); // C++ function call before function declaration
  return 0;
}
int addNum(int a, int b) //C++ function definition
{
  cout << "Result = " << (a + b); //return sum in (a + b)
  return (a + b);
}

Output

Result = 3

C++ User-defined functions types

User-defined functions can be divided into the following categories to help understand parameters and returns in functions. We will explain with examples here.


C++ function without return value and an argument

Example

#include <iostream>

using namespace std;
void addNum(); // C++ function prototype
int main() {
  addNum(); // C++ function call before function declaration
  return 0;
}
void addNum() //C++ function definition
{
  int a = 1, b = 2;
  cout << "Result = " << (a + b); //return sum in (a + b)
}

Output

Result = 3
  • addNum() in the function above is called without arguments from main().
  • addNum() prints the sum of a + b.
  • Since addNum(return) ‘s type is void, the function returns nothing.

C++ function without arguments but with a return value

Example

#include <iostream>

using namespace std;
int result;
int addNum(); // C++ function prototype
int main() {
  addNum(); // C++ function call before function declaration
  cout << "Result = " << result; //return sum in (a + b)
  return 0;
}
int addNum() //C++ function definition
{
  int a = 1, b = 2;
  result = (a + b);
  return result;
}

Output

Result = 3
  • addNum() in the function above is called without any arguments from main() but returns the result.
  • addNum() stores the sum of¬† a + b in the result, which is defined globally.
  • Since addNum(return) ‘s type is int, the function returns the result of type int.

C++ function without a return value but with an argument

Example

#include <iostream>

using namespace std;
void addNum(int a, int b); // C++ function prototype
int main() {
  addNum(1, 2); // C++ function call before function declaration
  return 0;
}
void addNum(int a, int b) //C++ function definition
{
  cout << "Result = " << (a + b); //return sum in (a + b)
}

Output

Result = 3
  • addNum() in the function above is called with 2 arguments from main() but returns no result.
  • addNum() prints the sum of a + b in the result passed through the main.
  • Since addNum(return) ‘s type is void, the function returns the result of type void.

C++ function that takes arguments and returns a value

Example

#include <iostream>

using namespace std;
int addNum(int a, int b); // C++ function prototype
int main() {
  cout << "Result = " << addNum(1, 2);
  return 0;
}
int addNum(int a, int b) //C++ function definition
{
  return (a + b);
}

Output

Result = 3
  • addNum() in the function above is called with 2 arguments from main() and returns a result.
  • addNum() returns the sum of a + b in the result passed through the main.
  • Since addNum(return) ‘s type is int, the function returns the result of type int, and the code will print the result in the main.

C++ Common Library functions

In C++ programming, library functions are equivalent to built-in functions. Instead of writing functions, programmers can use library functions by calling them directly. The C++ library functions sqrt(), abs(), isdigit(), etc., are some examples. The header file in the C++ library must include these library functions to use. For instance, we need to include the header file cmath to use mathematical functions like sqrt() and abs().

Example

#include <iostream>

#include <cmath>

using namespace std;
int main() {
  int num1 = -25;
  long int num4 = 58;
  float num3 = 250;
  double num2 = 50.347;

  cout << "The absolute value of " << num1 << " is " << abs(num1) << endl;
  cout << "The largest integer, " << num2 << ",is " << floor(num2) << endl;
  cout << "The square root: " << num3 << " is " << sqrt(num3) << endl;
  cout << "The base to exp power: " << num4 << " is " << pow(num4, 2) << endl;
  return 0;
}

Output

The absolute value of -25 is 25
The largest integer, 50.347, is 50
The square root: 250 is 15.8114
The base to exp power: 58 is 3364

This concludes the C++ Function lesson. In The next lesson, you will learn about function overloading in C++, and it’s usage.