cpp_void_keyword

CPP void keyword

Return to void, C++ Reserved words, C++, Reserved Words, CPP Glossary, CPP Topics


This comprehensive comparison explores the C++ `void` keyword and its equivalents in Python, Java, C#, Kotlin, JavaScript, TypeScript, PHP, Go, Rust, Swift, Transact-SQL, and PL/SQL. The `void` keyword in C++ signifies the absence of a value or type, particularly in the context of functions that do not return a value. Each programming language has its approach to representing functions that do not return a value or indicating the absence of type information.

  1. C++

In C++, `void` is used to specify that a function does not return a value. It can also be used to declare pointers to any type.

```cpp void myFunction() {

   // does something but does not return a value
} ```

C++ documentation: s://en.cppreference.com/w/cpp/language/types(https://en.cppreference.com/w/cpp/language/types)

  1. Python

Python does not have a direct equivalent to `void`. Functions that do not explicitly return a value implicitly return `None`.

```python def my_function():

   # implicitly returns None
   pass
```

Python documentation: s://docs.python.org/3/reference/datamodel.html#objects-values-and-types(https://docs.python.org/3/reference/datamodel.html#objects-values-and-types)

  1. Java

Java uses `void` in a similar manner to C++, indicating a method does not return a value.

```java public void myMethod() {

   // does something but does not return a value
} ```

Java documentation: s://docs.oracle.com/javase/tutorial/java/javaOO/methods.html(https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html)

  1. C#

C# also uses `void` to declare methods that do not return a value.

```csharp void MyMethod() {

   // does something but does not return a value
} ```

C# documentation: s://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods(https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods)

  1. Kotlin

Kotlin uses `Unit` instead of `void` to indicate a function does not return a meaningful value. It's similar to `void`, but `Unit` is an actual object.

```kotlin fun myFunction(): Unit {

   // does something but does not return a value
} ```

Kotlin documentation: s://kotlinlang.org/docs/reference/functions.html#unit-returning-functions(https://kotlinlang.org/docs/reference/functions.html#unit-returning-functions)

  1. JavaScript

JavaScript functions without a return statement return `undefined`, which is the closest equivalent to `void`.

```javascript function myFunction() {

   // returns undefined implicitly
} ```

JavaScript documentation: s://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions)

  1. TypeScript

TypeScript introduces `void` to indicate functions that do not return a value, similar to C++, C#, and Java.

```typescript function myFunction(): void {

   // does something but does not return a value
} ```

TypeScript documentation: s://www.typescriptlang.org/docs/handbook/basic-types.html#void(https://www.typescriptlang.org/docs/handbook/basic-types.html#void)

  1. PHP

PHP uses `void` in type declarations for functions that do not return a value, introduced in PHP 7.1.

```php function myFunction(): void {

   // does something but does not return a value
} ```

PHP documentation: s://www.php.net/manual/en/functions.returning-values.php(https://www.php.net/manual/en/functions.returning-values.php)

  1. Go

Go uses a blank return type to indicate a function does not return a value.

```go func myFunction() {

   // does something but does not return a value
} ```

Go documentation: s://golang.org/doc/effective_go#functions(https://golang.org/doc/effective_go#functions)

  1. Rust

Rust uses `()` to indicate a function does not return a meaningful value, which is similar to using `void` in other languages.

```rust fn my_function() → () {

   // does something but does not return a value
} ```

Rust documentation: s://doc.rust-lang.org/rust-by-example/fn.html(https://doc.rust-lang.org/rust-by-example/fn.html)

  1. Swift

Swift uses `Void` (or an empty tuple `()`) to signify that a function does not return a value.

```swift func myFunction() → Void {

   // does something but does not return a value
} ```

Swift documentation: s://docs.swift.org/swift-book/LanguageGuide/Functions.html#ID164(https://docs.swift

.org/swift-book/LanguageGuide/Functions.html#ID164)

  1. Transact-SQL

Transact-SQL does not have a direct equivalent to `void`. Stored procedures that do not return values simply do not specify a return type.

```sql CREATE PROCEDURE MyProcedure AS BEGIN

   -- does something but does not return a value
END ```

Transact-SQL documentation: s://docs.microsoft.com/en-us/sql/t-sql/statements/create-procedure-transact-sql(https://docs.microsoft.com/en-us/sql/t-sql/statements/create-procedure-transact-sql)

  1. PL/SQL

PL/SQL uses procedures (as opposed to functions) to perform actions without returning a value, which is conceptually similar to `void`.

```plsql PROCEDURE MyProcedure IS BEGIN

   -- does something but does not return a value
END; ```

PL/SQL documentation: s://docs.oracle.com/database/121/LNPLS/procedures.htm#LNPLS009(https://docs.oracle.com/database/121/LNPLS/procedures.htm#LNPLS009)

Each of these languages has its way of handling functions that do not return values, with some using a specific keyword like `void` and others using different mechanisms to denote the absence of a return value.


Fair Use Sources

C++: C++ Fundamentals, C++ Inventor - C++ Language Designer: Bjarne Stroustrup in 1985; C++ Keywords, C++ Built-In Data Types, C++ Data Structures (CPP Containers) - C++ Algorithms, C++ Syntax, C++ OOP - C++ Design Patterns, Clean C++ - C++ Style Guide, C++ Best Practices ( C++ Core Guidelines (CG)) - C++ BDD, C++ Standards ( C++ 23, C++ 20, C++ 17, C++ 14, C++ 11, C++ 03, C++ 98), Bjarne Stroustrup's C++ Glossary, CppReference.com, CPlusPlus.com, ISOcpp.org, C++ Compilers (Compiler Explorer, MinGW), C++ IDEs, C++ Development Tools, C++ Linter, C++ Debugging, C++ Modules ( C++20), C++ Packages, C++ Package Manager ( Conan - the C/C++ Package Manager), C++ Standard Library, C++ Libraries, C++ Frameworks, C++ DevOps - C++ SRE, C++ CI/CD ( C++ Build Pipeline), C++ Data Science - C++ DataOps, C++ Machine Learning, C++ Deep Learning, Functional C++, C++ Concurrency, C++ History, C++ Topics, C++ Bibliography, Manning C++ Series, C++ Courses, CppCon, C++ Research, C++ GitHub, Written in C++, C++ Popularity, C++ Awesome , C++ Versions. (navbar_cplusplus – see also navbar_cpp_containers, navbar_cppcon, navbar_cpp_core_guidelines, navbar_cpp23, navbar_cpp20, navbar_cpp17, navbar_cpp14, navbar_cpp11)

Reserved Words: Programming Language Keywords, aka Reserved Identifiers. (navbar_reserved_words - see also navbar_programming)


© 1994 - 2024 Cloud Monk Losang Jinpa or Fair Use. Disclaimers

SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.


cpp_void_keyword.txt · Last modified: 2024/04/28 03:13 by 127.0.0.1