The return of a function is a value that, upon its completion (and optionally), a function can return to the code that called it. For this, the reserved keyword return is used.
This value can be of any data type, including basic types (like int, float, string), complex types (like objects and structures), and even custom data types.
It is also possible for a function not to return any value. This is indicated by the void type.
On the other hand, only one value can be returned. Although we can return a grouping of values (like a collection, a tuple, or a class).
If you want to learn more, check out the Introduction to Programming Course
Void Return
When a function does not need to return any value, it is declared with the return type void. These functions typically perform actions (like modifying an object’s state or printing to the console).
For example, the greet() function only performs an action; it doesn’t need to return any value. In that case, we use the reserved keyword void.
public void Greet()
{
Console.WriteLine("Hello!");
}
Returning a Single Value
As mentioned, a function can return a single value. For this, the return keyword is used. The moment a return is reached, the function’s execution stops, and control is returned to the calling function.
For example, the Add function returns a value of type int.
public int Add(int a, int b)
{
return a + b;
// if there were something here, it would not be executed
}
Logically, the function’s return type must match the type of the value we return.
Returning Multiple Values
As we said, only a single value can be returned from a function. However, we can return a grouping of values.
For example, we can return multiple values by encapsulating them in a class or structure.
public class Result
{
public int Number { get; set; }
public string Text { get; set; }
}
public Result GetResult()
{
return new Result { Number = 42, Text = "Example" };
}
This is the most common way to return multiple values in C#.
We can also use tuples when we want to return multiple values from a function.
public (int, string) GetData()
{
return (42, "Example");
}
This is useful when the grouping we are going to return is temporary, and it’s not worth creating a structure or object solely for the return.
Functions can also return objects of any collection type, such as arrays, lists, dictionaries.
For example, like this.
public List<int> GetList()
{
return new List<int> { 1, 2, 3, 4, 5 };
}
Practical Examples
Void Function Example
This example shows how to define a function that does not return any value using the void return type.
// Function that prints a message to the console
public void ShowMessage(string message)
{
Console.WriteLine(message); // Print the received message
}
// Usage
ShowMessage("Hello, World!"); // Call the function with the message "Hello, World!"
Function with Return Value Example
This example shows how to define a function that returns a value. In this case, it calculates the area of a circle.
// Function that calculates the area of a circle given its radius
public double CalculateCircleArea(double radius)
{
return Math.PI * radius * radius; // Return the calculated area
}
// Usage
double area = CalculateCircleArea(5.0); // Call the function and store the result
Console.WriteLine($"The area of the circle is: {area}"); // Print the area of the circle
Function with Tuple Return Example
This example shows how to define a function that returns a tuple. In this case, it calculates the sum and product of two numbers.
// Function that calculates the sum and product of two numbers
public (int sum, int product) CalculateSumAndProduct(int a, int b)
{
return (a + b, a * b); // Return a tuple with the sum and product
}
// Usage
var result = CalculateSumAndProduct(3, 4); // Call the function and store the result in a tuple
Console.WriteLine($"Sum: {result.sum}, Product: {result.product}"); // Print the sum and product
Function with Class Return Example
This example shows how to define a function that returns an instance of a class. In this case, it creates an instance of the Person class.
// Class that represents a person with name and age
public class Person
{
public string Name { get; set; } // Property for the name
public int Age { get; set; } // Property for the age
}
// Function that creates an instance of the Person class
public Person CreatePerson(string name, int age)
{
return new Person { Name = name, Age = age }; // Return a new instance of Person
}
// Usage
Person person = CreatePerson("Luis", 30); // Call the function and store the result in a variable
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}"); // Print the name and age of the person
