Skip to content

Functions Intro

A function in C++ is a list of instructions designed to perform a specific task. Functions help organize your code, making it easier to read, maintain, and reuse. For example, the main function, which you’ve already encountered, is the entry point of every C++ program.

This section introduces the terms and concepts you’ll frequently encounter when working with functions in C++, along with examples to illustrate their use.

Arguments are the values sent to a function when it is called. These values are used by the function to perform its task. For example, the main functions you’ve written so far have taken no arguments, as shown below:

driver.cc
// Copyright 2024 CSCE2024
int main() {
// No arguments passed to main
return 0;
}

However, functions can accept arguments to process specific data. For instance:

void PrintNumber(int x) { cout << x << endl; }

Calling a function means executing it by passing the required arguments. This invokes the function’s code.

// Copyright 2024 CSCE2024
int main() {
cout << "Enter a number to square: " << endl;
int n;
cin >> n;
cout << n << " squared = " << Square(n) << endl; // Calling the Square function
return 0;
}

The definition or implementation of a function is the complete code of the function, including the body that specifies what the function does.

char GetLetter(int n) {
// Implementation of GetLetter()
if (n <= 1 || n > 26) return 'a';
return 'a' + (n - 1) % 26;
}

The name of a function is its identifier, used to call or reference the function in code. It must be unique within the same scope.

// Add is the name
int Add(int x, int y) { return x + y; }
// Greet is the name
void Greet() { cout << "Hello World" << endl; }

Parameters are the variables listed in the function definition that receive values when the function is called. They are like placeholders for the arguments passed to the function.

// x and y are the parameters
int Add(int x, int y) { return x + y; }

The function prototype is a declaration of a function that specifies the return type, name, and parameters but without the body. It informs the compiler about the function before its actual definition.

Function prototypes are found either above the main function or in a header file.

int Subtract(int x, int y);
// OR
int Subtract(int, int);

The return type of a function specifies the type of value the function returns after execution. If a function doesn’t return any value, the return type is void.

// int is the return type
int Max(int x, int y) { return x >= y ? x : y; }
// no return type
void ProductInfo() { cout << "Name:\nCategory:\nPrice:\nItem #:" << endl; }

A function’s signature consists of the function’s name and its parameter types (but not the return type). It uniquely identifies a function.

// int and int are the signatures
int Multiply(int x, int y);

These are the styling patterns shown in the previous code snippets:

  • The name of the function is capitalized and follows CamelCasing.
  • The parenthesis are next to the function name.

  • There is NOT a space after the starting parenthesis nor before the ending parenthesis (e.g. Add(int x, int y)).

  • There is a space between the ending parenthesis and the opening brace.

  • The opening brace is on the same line as the function declaration.

  • If short enough and only a single line, the function declaration and implementation can be written on a single line.

  • No blank lines occur after the function declaration or before the closing brace.

These are some of the errors thrown by cpplint when something is off with the way you styled a function:

Terminal window
Redundant blank line at the start of a code block should be deleted. [whitespace/blank_line] [2]
Redundant blank line at the end of a code block should be deleted. [whitespace/blank_line] [3]
Extra space after ( in function call [whitespace/parens] [4]
Extra space before ( in function call [whitespace/parens] [4]
Missing space before { [whitespace/braces] [5]
Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
{ should almost always be at the end of the previous line [whitespace/braces] [4]