Table of Contents
CPP return keyword
Return to return, C++ Reserved words, C++, Reserved Words, CPP Glossary, CPP Topics
This comparison explores the `return` keyword in C++ and its equivalents in Python, Java, C#, Kotlin, JavaScript, TypeScript, PHP, Go, Rust, Swift, Transact-SQL, and PL/SQL. The `return` keyword is used in many programming languages to exit a function and, optionally, return a value to the caller.
- C++
In C++, `return` exits a function and optionally sends a value back to the caller. Functions not returning a value are declared with `void`.
```cpp int add(int x, int y) {
return x + y;} ```
C++ documentation: s://en.cppreference.com/w/cpp/language/return(https://en.cppreference.com/w/cpp/language/return)
- Python
Python's `return` statement exits a function and returns a value. Functions without a `return` statement automatically return `None`.
```python def add(x, y):
return x + y```
Python documentation: s://docs.python.org/3/reference/simple_stmts.html#the-return-statement(https://docs.python.org/3/reference/simple_stmts.html#the-return-statement)
- Java
Java uses `return` in a similar manner to C++, to exit a method and optionally return a value. Methods declared as `void` do not return a value.
```java int add(int x, int y) {
return x + y;} ```
Java documentation: s://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html(https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html)
- C#
C# also uses `return` to exit a method and return a value to the caller. Void methods do not return a value.
```csharp int Add(int x, int y) {
return x + y;} ```
C# documentation: s://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/return(https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/return)
- Kotlin
Kotlin's `return` statement works similarly, used to exit a function and return a value. Functions in Kotlin that do not return a value explicitly return `Unit`.
```kotlin fun add(x: Int, y: Int): Int {
return x + y} ```
Kotlin documentation: s://kotlinlang.org/docs/reference/functions.html#returning-a-value-from-functions(https://kotlinlang.org/docs/reference/functions.html#returning-a-value-from-functions)
- JavaScript
JavaScript functions use `return` to exit and send a value back to the caller. Functions without a return statement return `undefined`.
```javascript function add(x, y) {
return x + y;} ```
JavaScript documentation: s://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return)
- TypeScript
TypeScript, extending JavaScript, uses `return` in the same way, with the addition of type annotations for the return value.
```typescript function add(x: number, y: number): number {
return x + y;} ```
TypeScript documentation: s://www.typescriptlang.org/docs/handbook/functions.html#return-types(https://www.typescriptlang.org/docs/handbook/functions.html#return-types)
- PHP
PHP's `return` statement exits a function or method and optionally passes a value back.
```php function add($x, $y) {
return $x + $y;} ```
PHP documentation: s://www.php.net/manual/en/function.return.php(https://www.php.net/manual/en/function.return.php)
- Go
Go uses `return` to end function execution and optionally return one or more values.
```go func add(x int, y int) int {
return x + y} ```
Go documentation: s://golang.org/ref/spec#Return_statements(https://golang.org/ref/spec#Return_statements)
- Rust
Rust functions return the last expression implicitly, but `return` can be used for early exit and to return a value explicitly.
```rust fn add(x: i32, y: i32) → i32 {
return x + y;} ```
Rust documentation: s://doc.rust-lang.org/book/ch03-03-how-functions-work.html#return-values(https://doc.rust-lang.org/book/ch03-03-how-functions-work.html#return-values)
- Swift
Swift's `return` statement exits a function and returns a value. Functions that do not return a value are marked with `Void`.
```swift func add(x: Int, y: Int) → Int {
return x + y} ```
Swift documentation: s://docs.swift.org/swift-book/Language Guide/Functions.html#ID164(https://docs.swift.org/swift-book/LanguageGuide/Functions.html#ID164)
- Transact-SQL
In Transact-SQL, `RETURN` is used within stored procedures or functions to exit and optionally return an integer value to the calling context.
```sql CREATE FUNCTION Add(@x int, @y int) RETURNS int AS BEGIN
RETURN @x + @yEND ```
Transact-SQL documentation: s://docs.microsoft.com/en-us/sql/t-sql/language-elements/return-transact-sql(https://docs.microsoft.com/en-us/sql/t-sql/language-elements/return-transact-sql)
- PL/SQL
PL/SQL uses `RETURN` within functions to return a value. Procedures use `RETURN` to exit early without returning a value.
```plsql FUNCTION Add(x NUMBER, y NUMBER) RETURN NUMBER IS BEGIN
RETURN x + y;END; ```
PL/SQL documentation: s://docs.oracle.com/cd/B19306_01/appdev.102/b14261/return_statement.htm(https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/return_statement.htm)
Each programming language uses its version of the `return` statement to exit a function and, in many cases, return a value to the caller. The syntax and capabilities around this keyword are tailored to fit the language's paradigm, offering flexibility in function design and control flow management.
Fair Use Sources
- 9781617298509 ([2024]])
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.