Introduction
Before we start with discussing the functions in Carbon Language, let us have a glimpse of what exactly Carbon is and why it is being created when there are tons of programming languages in the market.
So, Carbon is an open-source, statically typed, compiled programming language developed by Google to replace C++. Carbon provides developers with modern programming practices such as generics, modular code organization, and simple syntax. Carbon "is designed around interoperability with C++ as well as large-scale adoption and migration for existing C++ codebases and developers." This would be similar to how Microsoft developed Typescript to improve JavaScript and Kotlin to reinforce Java's weak points.
Interesting right?

You can refer to below declaration types in Carbon to better write the Code.
Type |
Keyword |
Methods/Functions |
fn |
Variables |
var |
Constants |
let |
Packages |
package |
Comments |
// |
This must have given you an idea of what Carbon Language is. If we proceed with the discussion today, we will look at the functions of Carbon and its various types, followed by examples.

So, let us start:
Functions in Carbon Language
Functions in Carbon language or any other language are simply a "chunk" of Code you can reuse rather than writing it out multiple times. Functions allow programmers to break down or decompose a problem into smaller chunks, each of which performs a specific task.
As you have read above, functions are declared with the fn keyword.
Below is the syntax:
Syntax
fn functionName(var param: type ... ) -> return type
Example 1:
fn Main() -> i32 {
var s : auto = "Hello Ninja";
Print(s);
return 0;
}
Output
The above-mentioned code snippet describes the main function and prints some string to the console. If you look carefully, we haven’t provided any parameters to our main function. And yes, that is known as the default functions in Carbon.
Let us explore the main function in depth:
THE MAIN FUNCTION.
We must have often seen the main() function in the code snippets. So, Have you ever thought about the purpose of that function in Carbon or any other programming language? Let’s understand now!
fn Main() -> i32
{
// main program statements
}
Need for the Main()
The Main() serves as the beginning point for program execution. It handles the program execution by directing the invocation to other functions in the program.
When the main invokes other functions, it passes execution control to the respective function. The function then returns control to the Main() when the end of the function is reached or a return statement is executed.
Let us see another example:
Example 2: Parameterized Functions
package sample api;
fn Square(x: i32) -> i32 {
return x * x;
}
fn Main() -> i32 {
return Square(12);
}
Output
In the example above, we have a Square function, which takes one parameter and returns its square.
In the main function, we are invoking the Square function, which is printed on your screen.
You must be wondering why we haven’t called the main function in Example 1.
It is because the language itself defines the main function as the intended beginning of the program. As a result, your operating system (Linux, macOS, Windows, etc.) calls it for you, so you don't need to call it yourself.
Let us now look at how we can invoke the functions in Carbon: