6 function

6.1 an overview of the

What it does: Encapsulate a piece of frequently used code to reduce the need for reset code

A large program can be divided into many program blocks, each module can be encapsulated as a function function.

6.2 Definition of functions

Define the function:

  1. Return value type
  2. The function name
  3. The list of parameters
  4. The body of the function
  5. Return the expression

6.3 Function calls

Syntax: FunctionName (PeremeterList)

The parameters of a function are called formal parameters, or parameters for short.

The arguments we pass in when we call them are actual arguments, called arguments for short.

When a function is called, the value of the argument is passed to the parameter. This value passing is also called passing.

6.4 value transfer

When a function is called, the value of the argument is passed to the function’s parameter. This process is known as value passing.

Therefore, if the parameter changes while passing a value, the value of the argument itself is not affected. Only an address or a reference can change the value of an argument.

6.5 Function declarations

What it does: Tells the compiler the name of the function and how to call the function. The actual body of the function can be defined separately later.

Functions can be declared many times, but can only be defined once.

The function declaration itself is more important for the developer to know in advance how some of the functions in the program work, and then to look directly at the main function. For the compiler, the function definition is written directly in the declaration place. When the compiler finds the function declaration, it goes straight to the corresponding function definition and compiles the function into it.

But function declarations have one advantage. A function is defined in sequence when it needs to call another function. You must define the called function before you define the function that called the function. But if you declare it in the header, you won’t have this problem. And the declarations don’t need to be ordered.

6.6 Writing of functions in separate files

What it does: It makes the structure of the function clearer


  1. Create a header file with the suffix.h
  2. Create a source file with the suffix.cpp
  3. Write the declaration of a function in a header file
  4. Write the function definition in the source file

Make a simple function that swaps two numbers, and place the function in a separate program.

Start by writing the source file of the function itself

void swap(int& a, int& b) {
    int space;
    space = a;
    a = b;
    b = space;

Then write the declaration of the function and put it in a header file

#pragma once
void swap(int&, int&);

Here, #pragma once works like this:

#ifndef A_H
#define A_H

A pragma is a compilation note, which means that the header file can only be compiled once. You can only open the file once during the entire build.


Method 1: pragma once

Method two: ifndef A_H

There is not much difference between the two methods. In detail, method two is more portable, and some compilers don’t support method one. Approach one, on the other hand, is that a file on the physical level is never compiled twice. But if there are multiple copies of a file, they will all be open.

Method two focuses on the file name. As long as the file is the same, any number of copies will only be compiled once. However, if multiple header files are the same name, it is possible that the remaining header files will not be compiled.

Finally we write the main function:

//main.cpp #include<iostream> #include"swap.h" using namespace std; int main() { int a = 10, b = 20; Cout << "before swapping:" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; swap(a, b); Cout << "after swapping:" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; }

Note that home-written headers are enclosed in double quotation marks “”, while system-supplied headers are enclosed in Angle brackets <>.