Table of contents
1.
Introduction
2.
About Carbon
2.1.
Why Choose Carbon
2.2.
Why Carbon Over C++
3.
Return Statement in Carbon
3.1.
Syntax
4.
Ways to Use Return Statements
4.1.
Return Type is Specified
4.2.
Example
4.3.
Implementation in Carbon 
4.3.1.
Output
4.4.
Returning Empty Tuple
4.5.
Example
4.6.
Implementation in Carbon
4.6.1.
Output
5.
Returned Var Statements in Carbon
5.1.
Syntax
5.2.
Example
5.3.
Implementation in Carbon 
5.3.1.
Output
6.
Return Vs Returned Var in Carbon
6.1.
Example
6.2.
Implementation in Carbon
6.2.1.
Output
7.
Frequently Asked Questions
7.1.
What does Carbon provide the programmers that make it unique?
7.2.
What are the variables in Carbon?
7.3.
Is carbon programming language Bi-directional?
7.4.
Why are data types necessary in Carbon?
7.5.
How are arrays declared in the Carbon language?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

Return vs Returned Var in Carbon

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hello ninja,
Typescript is the successor of JavaScript, and C++ is the successor of C language, so do you want to learn about the successor of C++ language? You have come to the right article. 

Return vs Returned Var in Carbon

In this article, we will discuss the return and returned Var statements. We will also go through return vs returned Var in Carbon.

About Carbon

Carbon programming language is an open-source and general-purpose language developed by Google. It was introduced in July 2022 at a conference held in Toronto. It is a language that advances C++. It can work with the existing C++ systems, making switching to Carbon easier.

About Carbon

Carbon is still in its experimental phase right now.

Why Choose Carbon

There are currently 250-2500 programming languages worldwide, so the reasons to learn and use Carbon are as follows:

  • Solid language foundations.
     
  • If you are familiar with C++, then Carbon is extremely easy for you.
     
  • Fast performance.
     
  • Scalable libraries.
     
  • Interoperates with existing C++.

Why Carbon Over C++

Carbon is similar to C++. Even the performance of these languages matches by 100%.  

The difference is that Carbon provides a better developer experience. This is because, in Carbon, you can extend libraries easily as there is a massive decrease in the technical debt of the existing libraries. Carbon also provides a wide range of new developer-friendly features. We will look into return vs retired statements in Carbon.

Return Statement in Carbon

The return statements in Carbon are used to end the flow of execution of a function and bring it back to the function call.

Return Statement in Carbon

The control of the code is returned to the function caller by stopping the flow of the code as soon as the return statement is executed. In other words, a return statements does two things while it is executed in the compiler:

  • Stops the flow of program execution. This means that any line of code written after a return statement is not executed until the values are successfully returned to the function caller. 
     
  • Returns values and control of the program to the function. This means that if there is a result value specified, it will first return that. Then the program after the return statement will continue to be executed.  

Syntax

The syntax to write a return statement in Carbon is as follows:

return <expression>;


There are two ways in which this syntax is used:

  • Returning only the control of the program to the function caller (no use of expression).
Returning only the control of the program to the function caller
  • Returning value and control of the program to the function caller (expression provides the value). 
Returning value and control of the program to the function caller

Ways to Use Return Statements

There are two ways in which you can use return statements in Carbon:

Return Type is Specified

Return statements with return type are used to return a variable value to the function caller 
In this method, the value is returned before the execution of all the codes in a function. The below given example will make this concept clear to you.

Example

We will take an example of finding the square of the number 32 and specify the return type to get the result. 

Implementation in Carbon 

package returntypeexample api;

fn Square(x: i32) -> i32 {
  return x * x;
}

fn Main() -> i32 {
  return Square(12);
}

Output

output image

Returning Empty Tuple

This is used when there is no value to return to the function caller. In this method, the return statement returns an empty tuple, and its return type is implicitly (). A tuple in Carbon can hold elements of different data types. We do not use the expression argument in this case.

NOTE: if a function signature consists of “-> ()”, then the return type must be explicit. This means that we use the method where the return type is specified using expression arguments.

Example

We will take an example of printing hello world with an implicit return type and specify the return type to get the result. 

Implementation in Carbon

package returnexample api;

fn Returnimplicit() {
    Print("Hello World");
	return;
}

fn Main() -> i32 {
    Returnimplicit();
    return 0;
}

Output

output image


Also check out - Phases of Compiler

Returned Var Statements in Carbon

It can be used only for variables. This means that we mark the variable with a return Var statement. The variable's value is the only value that can be returned to the function caller. This means that no “return” statement can be used within the function where the “returned var” statement is used. You cannot use expression arguments in a returned Var statement.

Returned Var Statements in Carbon  

NOTE: if the flow of the function ends because of any other reason than returned var, then return statements can be used as the return var will not be executed.

Syntax

The syntax to write a return var statement in Carbon is as follows:

Returned var <statement>;
return var;


The syntax has two statements:

  • Returned var - This is used as a substitute to the keyword “Var” which is used to specify a variable name and variable value.
     
  • Return var - This is used to return the result value of the returned var statement.

Example

We will take an example to understand the above-explained concepts in-depth.

Implementation in Carbon 

package returnexample API;

fn ReturnVarexample () {
    returned var output: String = "hello there";
 	return var;
}

Output

output image

We have used the returned var statement to declare the variable “output”. Then we used the return var to return the value of the variable to the function caller “ReturnVarexample”.

Return Vs Returned Var in Carbon

If we do return vs returned var in Carbon, we will learn that it is better to use a returned var statement than a return statement. 

The primary reason to choose return var statements in return vs returned var in Carbon is that the return statements create copies of the value to be returned to the function caller, whereas the returned var statements avoid making copies of the values to be returned because it can directly access the address of the variable to return the value to it. 

This makes returned var easier to be executed by the compiler and only one memory is accessed while making changes or executing the program rather than accessing  memory of the copies as well. So it is recommended to use returned var in return vs returned var in Carbon

NOTE: “Var” is used to declare global variables in Carbon.

Example

We will see the below example where we will use the returned var statement and understand how the above-explained concept applies and makes returned var statements better in return vs returned var in Carbon.

Implementation in Carbon

package returnvsreturnvar api;

fn Sumofxandy(x: i32, y: i32)
 {
  	let x: i32=50;
  	let y: i32=50;
  	returned var output: i32 = x+y;
  	return var;
}

Output

output image

In the above example, we understood that the value to be returned itself accessed the variable’s address directly. Thus we get 100 as an output. This is the reason we choose return var in return vs returned var in Carbon.

The following table shows an in-detail return vs returned statements in Carbon.

retrun vs returned var table

In the above-given table, we have discussed the return vs returned var in Carbon.

Now it's time to discuss some FAQs.

Frequently Asked Questions

What does Carbon provide the programmers that make it unique?

Carbon offers programmers modern practices, like generics, modular code organization, and simple syntax. Carbon matches the performance of C++ and alongside provides an ease to extending the existing libraries, which is not possible in C++.

What are the variables in Carbon?

Whenever we want to store data, it requires a particular memory location. To call or use these stored data, we need to give these locations a variable name.

Is carbon programming language Bi-directional?

Carbon language is Bi-directional as it allows us to call C++ language code from Carbon and also Carbon language code to C++.

Why are data types necessary in Carbon?

Data Types are required for carbon or any programming language because they help interpreters or compilers understand the type of any value.

How are arrays declared in the Carbon language?

To declare arrays in Carbon language using array types and array size. For example, var arr: [i32; 5] = (1,2,3,4,5);

Conclusion

In this article, we learned what is return and returned var statements in Carbon. We also learned how to use these statements in Carbon. We also looked into return vs returned var in Carbon.

To learn more about the Carbon programming language, refer to,

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Happy Learning!

Live masterclass