Home > Backend Development > C++ > body text

What are the return value types of functions in C++?

WBOY
Release: 2024-04-12 12:30:02
Original
462 people have browsed it

The return value type of a function in C defines the type of value returned after execution: Basic types: void (no return value), bool, integer, floating point, character reference type: type reference, type pointer structure or class : Type instance

C++ 中函数的返回值类型有哪些?

The return value type of the function in C

The return value type of the function defines the value returned after the function is executed type.

Basic types

  • void: Returns no value.
  • bool: Returns a Boolean value.
  • Integer type (int, short, long): Returns an integer.
  • Floating point type (float, double): Returns a floating point number.
  • Character type (char): Returns a single character.

Reference type

  • T&: Returns a reference to type T.
  • T*: Returns a pointer to type T.

struct and class

  • struct or class name: Returns an instance of this type .

Practical case

Consider a function that calculates the sum of two numbers:

// 返回两个数字的和
int add(int a, int b) {
  return a + b;
}
Copy after login

The return value type of this function isint because it returns an integer.

Here's how to use this function:

int sum = add(10, 20);  // sum 将包含 30
Copy after login

Other notes

  • A function can have no return value type, in this case , they return void.
  • If a function does not explicitly specify a return value type, the compiler will default to int.
  • You can specify that the return value is a constant by using the const keyword.

The above is the detailed content of What are the return value types of functions in C++?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template