Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Return Statement
3.
Auto Type Return Value
4.
Return Multiple Values
4.1.
Tuple
4.2.
Struct type
5.
Return Void Function in Carbon
6.
Frequently Asked Questions
6.1.
What language is Carbon from?
6.2.
How can we compile Carbon on windows?
6.3.
Is Carbon an Object-Oriented Language?
6.4.
Where can I get all the information about Carbon Language?
6.5.
What is the difference between Carbon and C++?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Return Void and Multiple Values from Function in Carbon

Author Sagar Mishra
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

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.

Return Void and Multiple Values from Function in Carbon

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’.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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:

output of auto

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.

Let's take an example for better understanding.

package NinjaTest api;

fn Main() -> i32 {
    var x: auto = (0, 1, 4, 9, 0); // Tuple
    Print("{0}", x[3]);
    return x[3];
}


Output:

output of Tuple

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}
     
package NinjaTest api;

fn Main() -> i32 {
  var test: auto = {.x_axis = 0, .y_axis = 1};
  test = {.x_axis = 10, .y_axis = -10};
  var result: i32 = test.x_axis * test.x_axis + test.y_axis * test.y_axis;
  Print("Result : {0}", result);
  return 0;
}


Output:

output of struct type

In the above code, 

  • We have declared the main function in line 1.
     
  • Then, we created a variable test and assigned the values to it.
     
  • Next, we added the product of both variables.
     
  • At last, we printed the result.
     

The last topic of our "Return Void and Multiple Values from Function in Carbon" blog is - Return Void Function in Carbon.

Return Void Function in Carbon

Now, let's check an example of the void function in carbon that returns nothing.

package NinjaTest api;
 
fn Subtract(var a: i32, var b: i32) -> i32 {
    return a - b;
}

fn Count(var count: i32) {
    Print("The count is {0}", count);
}

fn Main() -> i32 {
    Print("Difference is {0}", Subtract(6, 3));
    Count(5);
    return 0;
}


Output:

output of void Function in Carbon

In the above code,

  • 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.

And many more on our platform Coding Ninjas Studio.

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

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 problemsinterview experiences, and interview bundles for placement preparations.

However, you may consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass