Understanding Method Parameters and Return Values

Tutorial 2 of 5

Understanding Method Parameters and Return Values in C

1. Introduction

Tutorial's Goal

The goal of this tutorial is to help you understand the concept of method parameters and return values in C#. By the end of this tutorial, you will be able to pass data into methods and get results back.

What the User Will Learn

The user will learn:
- The concept of method parameters and return values.
- How to pass data into methods.
- How to get results back from methods.

Prerequisites

The user should have a basic understanding of:
- C# syntax and data types.
- Basic programming concepts like variables, loops, and conditionals.
- How to create and run a simple C# program.

2. Step-by-Step Guide

Understanding Method Parameters

In C#, a method parameter is a variable used in a method where the value is provided by the caller of the method. When calling a method, you can pass values to these parameters which the method then uses in its code block.

Here is an example method with parameters:

public void Greet(string name, int times)
{
    for (int i = 0; i < times; i++)
    {
        Console.WriteLine("Hello, " + name);
    }
}

In this example, name and times are parameters of the Greet method.

Understanding Return Values

A return value is the value that a method returns to the caller when it has finished executing. The return type is defined in the method signature, and it can be any valid C# data type.

Here is an example method with a return value:

public int Add(int a, int b)
{
    int sum = a + b;
    return sum;
}

In this example, sum is the return value of the Add method.

3. Code Examples

Example 1: Method with Parameters

public void PrintFullName(string firstName, string lastName)
{
    Console.WriteLine("Your full name is " + firstName + " " + lastName);
}

In this example, firstName and lastName are parameters. When calling this method, you would provide two strings.

Example 2: Method with Return Value

public double CalculateAverage(int a, int b, int c)
{
    double average = (a + b + c) / 3.0;
    return average;
}

In this example, average is the return value. When you call this method, it will return the average of the three integer parameters.

4. Summary

In this tutorial, we covered method parameters and return values in C#. Method parameters are values that are passed into a method, and a return value is the value that a method sends back to the caller.

5. Practice Exercises

Exercise 1

Create a method called Multiply that takes two integers as parameters and returns their product.

Solution 1

public int Multiply(int a, int b)
{
    return a * b;
}

Exercise 2

Create a method called Introduce that takes a string parameter name and an integer parameter age and prints a message introducing the person.

Solution 2

public void Introduce(string name, int age)
{
    Console.WriteLine("Hello, my name is " + name + " and I am " + age + " years old.");
}

For further practice, try creating methods with different numbers and types of parameters, and methods with different return types.