Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Methods with Multiple Parameters and Return Types | Functions with Different Signatures
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Methods & Modular Thinking

bookMethods with Multiple Parameters and Return Types

When you start building more complex programs, you often need methods that can accept multiple pieces of information and return a result. The combination of a method's name, the number and type of its parameters, and its return type is called its method signature. This signature tells you exactly how to use the method and what kind of result to expect.

A method can take any number of parameters, separated by commas in the parentheses. Each parameter must specify its type and name. You can also define what type of value the method returns by specifying the return type before the method name. If the method should not return a value, you use the keyword void as the return type.

Program.cs

Program.cs

copy
1234567891011121314151617181920
using System; namespace ConsoleApp { public class Program { // This method takes two integers and returns their sum. public static int Add(int a, int b) { return a + b; } public static void Main(string[] args) { int result = Add(3, 5); Console.WriteLine("The sum of 3 and 5 is: " + result); } } }

When a method returns a value, you can use that value in the rest of your code—such as storing it in a variable, printing it, or passing it to another method. Returning data from methods is powerful because it lets you break down your program into small, reusable pieces that can compute and deliver results. For example, a method might calculate a value, format a string, or even process user input and return a decision.

You can also create methods that accept different types of parameters and return different types of results. This flexibility allows you to write code that is both reusable and adaptable to many situations.

Program.cs

Program.cs

copy
1234567891011121314151617181920
using System; namespace ConsoleApp { public class Program { // This method takes a name and a score, and returns a formatted message. public static string FormatScoreMessage(string name, int score) { return "Player " + name + " scored " + score + " points!"; } public static void Main(string[] args) { string message = FormatScoreMessage("Jordan", 42); Console.WriteLine(message); } } }
Note
Definition

Definition: The return type of a method is the data type that the method sends back to the code that called it. If a method does not send back any value, its return type is void. The main difference is that a method with a return type must use the return keyword to provide a value of the specified type, while a void method simply ends when it is done.

1. What does the 'void' keyword indicate in a method signature?

2. How do you specify that a method should return an integer value?

3. Fill in the blank to specify the correct return type for a method that adds two integers and returns the result.

question mark

What does the 'void' keyword indicate in a method signature?

Select the correct answer

question mark

How do you specify that a method should return an integer value?

Select the correct answer

question-icon

Fill in the blank to specify the correct return type for a method that adds two integers and returns the result.

 Add(int a, int b) { return a + b; }
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 3

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookMethods with Multiple Parameters and Return Types

Scorri per mostrare il menu

When you start building more complex programs, you often need methods that can accept multiple pieces of information and return a result. The combination of a method's name, the number and type of its parameters, and its return type is called its method signature. This signature tells you exactly how to use the method and what kind of result to expect.

A method can take any number of parameters, separated by commas in the parentheses. Each parameter must specify its type and name. You can also define what type of value the method returns by specifying the return type before the method name. If the method should not return a value, you use the keyword void as the return type.

Program.cs

Program.cs

copy
1234567891011121314151617181920
using System; namespace ConsoleApp { public class Program { // This method takes two integers and returns their sum. public static int Add(int a, int b) { return a + b; } public static void Main(string[] args) { int result = Add(3, 5); Console.WriteLine("The sum of 3 and 5 is: " + result); } } }

When a method returns a value, you can use that value in the rest of your code—such as storing it in a variable, printing it, or passing it to another method. Returning data from methods is powerful because it lets you break down your program into small, reusable pieces that can compute and deliver results. For example, a method might calculate a value, format a string, or even process user input and return a decision.

You can also create methods that accept different types of parameters and return different types of results. This flexibility allows you to write code that is both reusable and adaptable to many situations.

Program.cs

Program.cs

copy
1234567891011121314151617181920
using System; namespace ConsoleApp { public class Program { // This method takes a name and a score, and returns a formatted message. public static string FormatScoreMessage(string name, int score) { return "Player " + name + " scored " + score + " points!"; } public static void Main(string[] args) { string message = FormatScoreMessage("Jordan", 42); Console.WriteLine(message); } } }
Note
Definition

Definition: The return type of a method is the data type that the method sends back to the code that called it. If a method does not send back any value, its return type is void. The main difference is that a method with a return type must use the return keyword to provide a value of the specified type, while a void method simply ends when it is done.

1. What does the 'void' keyword indicate in a method signature?

2. How do you specify that a method should return an integer value?

3. Fill in the blank to specify the correct return type for a method that adds two integers and returns the result.

question mark

What does the 'void' keyword indicate in a method signature?

Select the correct answer

question mark

How do you specify that a method should return an integer value?

Select the correct answer

question-icon

Fill in the blank to specify the correct return type for a method that adds two integers and returns the result.

 Add(int a, int b) { return a + b; }
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 3
some-alt