C++: A function that returns a value

This entry is part 17 of 61 in the series C++

In defining a function, we need to have the type, the name and the arguments declared correctly.

Here, we look at a function that takes in 2 integer arguments and return the sum of the 2 integers.

The type of the function sum is declared as an integer.

#include <iostream>
#include <string>
using namespace std;

int sum(int x, int y)
	return (x + y);

void main()
	int x = 10;
	int y = 8;

	cout << sum(x,y);
	cout << endl;

The 2 arguments are both integers and the function has been set to return an integer too.


Series Navigation<< C++: Function
C++: Function overloading >>