Home > Backend Development > C++ > body text

Why can't void function be assigned a value in c++?

下次还敢
Release: 2024-05-09 03:12:16
Original
1134 people have browsed it

In C, the void function cannot be assigned for the following reasons: type mismatch: the void function has no return value, the type is void, and is incompatible with the assignment operator operand type. The essence of function pointers: The void function has no return address, making it impossible to store it in the function pointer, causing the assignment to be invalid. Avoid confusion: prohibiting assignment prevents the mistaken belief that void functions can return values. Keep it simple: disabling assignment simplifies the language and improves consistency and readability.

Why can't void function be assigned a value in c++?

The reason why the void function cannot be assigned a value in c

In C, the void function cannot be assigned a value. This is because The following reasons:

Type mismatch

  • void The function has no return value, so its type is void.
  • Assignment operators require operands to be of the same type.
  • Since void is a special type, it is not compatible with any other type.

The essence of function pointers

  • The function pointer points to the address of the function code.
  • void function has no return value, so it has no return address.
  • This prevents the void function pointer from storing a valid address, causing the assignment operation to be invalid.

Avoid confusion

  • Allowing assignments to void functions may cause confusion because it may give people the wrong impression that A void function can return a value.
  • Disallowing assignment to void functions helps avoid such confusion.

Keep the language simple

  • Disallowing assignment to void functions makes the C language simpler because it eliminates the need for this operation .
  • This helps keep the language consistent and readable.

The above is the detailed content of Why can't void function be assigned a value in c++?. 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