Learn more about functions in C++

  • 2020-05-05 11:32:31
  • OfStack

A large program cannot be completed entirely by one person, let alone put everything in a main function. In order to facilitate planning, organization, programming and debugging, it is common practice to divide a large program into several program modules (that is, program files), each of which implements a part of the function. Different modules can be done by different people. When the program is compiled, the program module is used as the compilation unit, that is, each compilation unit is compiled separately. If you find errors, you can check and correct them within the scope of this program module. After compiling separately, the connection is made to connect the object files and system files of each module into an executable file.

A program file can contain several functions. No matter how many modules a program is divided into, there can only be one main function. The program always starts with the main function. While the program is running, other functions are called by the main function, and other functions can be called to each other. There are no classes or objects in C, and functions are defined directly in the program module. An C program can be thought of as composed of several functions, and the C language is considered a function-oriented language. C++ procedural programming follows the use of functions in C. In C++ object-oriented programming, functions other than main functions are mostly encapsulated in classes. A main function or other function can call a function in a class through a class object. Whether it is C or C++, each operation in the program is basically implemented by the function, the program writer to write a function according to the need, each function is used to achieve a certain function. Therefore, the reader must master the concept of functions and learn to design and use them.

The term "function" is translated from the English function, but actually function means "function". As the name implies, a function is a function.

In the actual application of the program, the main function is written very simple, its role is to call each function, the function of each part of the program is all realized by each function. The main function is equivalent to the general scheduling, the function in turn to achieve the function.

Developers and software developers will write some commonly used function modules into functions, put in the function library for public selection. Program developers should be good at making use of library functions to reduce the workload of repeatedly writing program segments.

Call other functions in the main function.


#include <iostream>
using namespace std;
void printstar(void)  // define printstar function 
{
  cout<< " ******************************  " <<endl; // The output 30 A" * " 
}

void print_message(void) // define print_message function 
{
  cout<< " Welcome to C++! " <<endl; // Output a line of text 
}

int main(void)
{
  printstar( ); // call printstar  function 
  print_message( ); // call print_message function 
  printstar( ); // call printstar  function 
  return 0;
}

Here's how it works :


******************************
Welcome to C++!
******************************

From the user's perspective, there are two types of functions:
System functions, library functions. This is provided by the compilation system, so users don't have to define these functions themselves, they can use them directly.
User-defined functions. To address the specific needs of users.

From the form of functions, there are two types of functions:
No argument function. You don't have to give an argument when you call a function.
It has arguments. When you call a function, you give it an argument. There is data passing between the calling function and the called function.

function definition and call
The general form of defining a parameter free function is
      type identifier function name ([void])


  {
     Declaration section 
     statements 
  }


Specify the type of the function with a type identifier, that is, the type of the value that the function takes back.

The general form of the argument function is defined as


   Type identifier   The function name ( Formal parameter table column )
  {
     Declaration section 
     statements 
  }


For example:


int max(int x,int y) // The function first , The value of the function is an integer , There are two integer parameters 
{
  int z; // The declared part of a function body 
  z=x>y?x:y; // will x and y The value of the larger of the z
  return (z);// will z Returns the call point as a function value 
}

Note: C++ requires that you specify the type of the function when you define it.


Related articles: