Home > Backend Development > C++ > What does a function in c++ consist of?

What does a function in c++ consist of?

下次还敢
Release: 2024-05-01 13:06:12
Original
365 people have browsed it

C function consists of function name, parameter list, return type and function body. The function name consists of letters, numbers and underscores, starting with a letter; the parameter list specifies the type and number of input parameters, listed in parentheses; the return type specifies the type of output value, if there is no return value, it is void; the function body contains the execution operation statement, surrounded by braces.

What does a function in c++ consist of?

Construction of a function in C

A function in C is composed of the following elements:

1. Function name

The function name identifies the function and consists of letters, numbers and underscores, and must start with a letter.

2. Parameter list

The parameter list specifies the parameters accepted by the function. The type and number of parameters are specified in parentheses.

3. Return type

The return type specifies the type of value returned by the function. If the function does not return any value, the return type is void.

4. Function body

The function body contains statements that perform function operations. It is enclosed by curly brackets ({ }).

Example:

<code class="cpp">int add(int x, int y) {
  return x + y;
}</code>
Copy after login

In this example:

  • ##Function name: add
  • Parameter list: (int x, int y)
  • Return type: int
  • Function body: return x y ;

The above is the detailed content of What does a function in c++ consist of?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
c++
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template