All programs must at least have the main() function.


Functions are used to modularise a program, breaking it down into smaller chunks that can be called upon when necessary.



return_datatype function_name (parameter, list) {

code block statements ;



return_datatype is the expected return datatype as required by the caller invoking the function. If the function doesn't need to return anything, the void return data type should be used.


function_name is the programmer defined identifier for the function.


The parameter list is contained within the parentheses following the function name, and is made up of zero of more comma separated parameters (aka arguments, aka formal parameters). The data type, order and number of parameters must match what is being sent to the function by the caller. The parameter names can be different from those of the caller, as long as the function uses the same identifier names within its { code block }.


For instance, say a function is being called within main() using a and b as its sending parameters: myFunc(a, b) ;  The function can then use its own parameter names within the function, e.g.


int myFunc(int x, int y) {

return(+ y) ;


int main() {

int a = 2, b = 3 ;

myFunc(a, b) ;

return 0 ;



Notice that the caller used the variables a and b, but the function accepted them to be used within its code block as x and y. The main thing that we should be worried about is that the return value is of the correct data type.


The function name and parameter list are known as the function signature. Also sometimes referred to as the extended name.


The function body contains a collection of statements defining what the function does. This can be as simple as sending a string to the console, or computing and returning a value.


The term for using a function is to 'call' a function.


A function is called by using the function name along with any parameters required.


#include <iostream>
using namespace std;

int add(int x, int y){
	return x + y ;

void hi(){
	cout << "Hello, World!" << endl ;

int main () {


	int a = 17, b = 42, c ;

	c = add(a, b) ;

	cout << a << " + " << b << " = " << c << endl ;

	return 0;

Compile & Run:

Hello, World!

17 + 42 = 59



Initialised parameters

It is also possible to initialise the value of the parameters, so that in the event of no parameter value being passed in, the function will use its initialised value as a default for use within the function:


int add(int x, int y = 23) ; {

return x + y ;


Leave a Reply