Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hey Ninjas!
Do you know that Google company also creates programming languages? In the past few years, Google has created many languages like Kotlin, Golang (Go), Dart, and many more. Carbon is also one of the creations of Google. As we all know, n numbers of functions are available in Carbon, like the void, auto, etc.
So, today we will discuss the topic "Return Void and Multiple Values from Function in Carbon" in this blog. Here we will discuss the auto function, tuples, struct type, and returning void functions in detail.
Return Statement
A return statement is a function in Carbon. A return statement terminates the execution of a function and returns the control to the calling function. After the call, execution picks back up in the caller function. The calling function may get a value from a return statement.
Now many of you have a question about why a return statement is necessary after every function. So the reason is whenever a return statement is encountered by the compiler, it stops at that line and returns a value. Due to this, the remaining codes were left without getting compiled, which may lead to a variation in output. Now let's check out different types of return statements like auto, tuples, etc.
Let us now look into auto type return value in ‘return void and multiple values from function in Carbon’.
Auto Type Return Value
Auto is a type of Name-binding declaration function in Carbon. We can use auto to assign the variable type automatically. It can also be combined with var or let or as function return types.
Let's take an example to understand this auto in a better way.
package NinjaTest api;
fn Main() -> i32 {
var s: auto = "Hey Ninjas"; // Auto Variable
let x: i32 = 23; // Constant
var y: i32 = 6; // Integer variable
Print(s); // Print
return 0; //Return value
}
Output:
Returning multiple values is the next topic in our "Return Void and Multiple Values from Function in Carbon" blog.
Return Multiple Values
Here we will discuss the functions that return multiple values.
Tuple
A tuple is a collection of items with a definite size and a range of possible types, where each value is identified by its place in the collection. Tuples are used to store multiple data in a single variable. An example use of tuples is to return multiple values from a function.
In the above example, ‘x’ is a tuple with multiple coordinates. We can access them using the index.
Struct type
Structural types help you identify members using their name rather than their index or position. They are declared within curly braces var name. For example,
Struct type example: {.name: String, .count: i32}
Struct value example: {.name = "Ninja", .count = 5}
We have defined a function that will return an integer value: the difference between two numbers.
In the next part, an empty or void return type will return nothing, as you can see in the above example.
The last one is the Main method, from where the program starts running.
Frequently Asked Questions
What language is Carbon from?
A new programming language called Carbon was revealed this year. It is a systems language that was first introduced on 19 July by Chandler Carruth. He is a software engineer at Google, at the CppNorth conference in Canada. Carbon can act as a successor to C++ while it is still in the testing stage.
How can we compile Carbon on windows?
You can use compiler explorer to compile carbon language on windows or install LLVM on windows and then set up the carbon environment.
Is Carbon an Object-Oriented Language?
Carbon has support for Object Oriented Programming, just like C++.
Where can I get all the information about Carbon Language?
You can refer to the official documentation or keep an eye out for blogs from CodingNinjas.
What is the difference between Carbon and C++?
Carbon contains all the functionality of C++, but the inverse is invalid. The declarations start with the introducer keywords example, functions with fn, and variables with var. Imports in Carbon are more like the C++ modules.
Conclusion
In this article, we discussed the topic of return void and multiple values from function in Carbon. We started with the introduction of the return statement, and then we discussed the auto function, tuples, struct type, and returning empty functions.
We hope this blog has helped you enhance your knowledge of the Return Void and Multiple Values from Function in Carbon. If you want to learn more, then check out our articles.
But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundles for placement preparations.
However, you may consider our paid courses to give your career an edge over others!