C++: Function

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

So far, the code that we have worked on is all contained within a function called main.  Main is the starting point of a program and so in a way, we have already used a function in our code.

A function is a self-contained block of code with a specific purpose.

A function prototype declaration consists of function’s return type, name, and arguments list.

In the example below, we create a function called printout.  This function did nothing but print out a string of characters.

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

void printout(string str)
	cout << str;

void main()
	string greeting = "Hi, there";


	cout << endl;

The type of the function printout is a void which means it will not return anything.  The name is called printout and it takes in a string argument.

Series Navigation<< C++: The for loop
C++: A function that returns a value >>

Leave a comment

Leave a Reply