• 20 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 10/31/23

Understand parameters and return values

Since the beginning of this course, we have been using functions (or methods), such as  Console.WriteLine(). In this chapter, you will learn the different components of a method declaration (parameters and return value), and the difference between value and reference types.

What is a method declaration composed of?

Let's say you want to print the perimeter of a 6 (length) by 4 (width) rectangle.

public static void DisplayPerimeter()
{
   int perimeter = 2*(6 + 4);
   Console.WriteLine(perimeter); // -> 20
}

This is a correctly implemented method, but let's agree, quite useless as you'll always end up working with the same numbers.

Functions with parameters

To address this limitation, you need to make your function accept the numbers from the outside. This can be done by defining a function with parameters.

In C#, the variable name and type of the parameter are part of function declaration. Here's what it looks like:

public static void DisplayPerimeter(int length, int width)
{
   int perimeter = 2 * (length + width);
   Console.WriteLine(perimeter); // -> depends on the values of length and width
}

Parameters are variables listed in the function declaration that are specified inside () by the name and type. Now you can call this function and pass any values you want to it:

DisplayPerimeter(10, 11); // -> 42
DisplayPerimeter(2, 2); // -> 8

Each value is assigned to a parameter in the order they are defined.

Let's get deeper into the terminology. Parameters are the variables declared in a function. The values passed to that function are called arguments.

This is great, we've added some purpose to our function.

Now, what can you do about the result?

Often, the code that called the function needs an answer to perform its work. This answer can be provided by the return value of the function.

Define a return value

To define a return value, you need to:

  1. Alter the declaration of the function to indicate that it's expected to return a result.

  2. End the function with the return keyword.

Here is how to create a Sum function, which will return the result of the calculation so it can be used by the calling function:

public static int Sum(int a, int b) 
{   
   int calc = a + b;
   return calc;
}

You could also forgo the sum variable and return the result of the calculation expression directly:

public static int Sum(int a, int b)
{
   return a + b;
}

Once your function is defined, you can use it as many times as you want. Here are a couple of variations:

int sumOfSmallNumbers = Sum(3,7);
Console.WriteLine(sumOfSmallNumbers); // -> 10
int sumOfLargerNumbers = Sum(sumOfSmallNumbers,999);
Console.WriteLine(sumOfLargerNumbers); // -> 1009

In the example above, we used the result of the first calculation as a parameter for the next one.

You also need to understand that some variables store a value directly, while others store a reference. This impacts how they are sent as arguments to a function.

Differentiate value types and reference types

You've learned about variable's data type which can be simple - like numbers or strings, or, more complex, like classes.

Each of those types are classified as either a value type or a reference type.

Let's start with the value type.

Value types

Let's use integers as an example. If you create an integer variable and then assign it to another variable, the value of the first variable will be copied to the second one:

// declare variable a as an int and initialize it with a value of 10
int a = 10;
// declare variable b as an int and initialize it with a copy of the value of a
int b = a;

Since int is a value type, b is assigned a copy of the value of a. That means that if you later change the value of a, the value of b will not be changed.

Console.WriteLine(a); // -> 10 
Console.WriteLine(b); // -> 10
a = 15;
Console.WriteLine(a); // -> 15 
Console.WriteLine(b); // -> 10

That's pretty straightforward. Next comes reference types.

Reference types

We'll work with classes in this section. When you create a variable and assign an instance of class to it, it creates the object; however, the variable holds a reference to that object which is the location where it's stored in the memory. Then, when you assign the value of that variable to another variable, the object will not be copied. The new variable will hold a reference to it - the same address.

Here's an example:

public class Car
{
   public String color = "red"; 
}


Car car = new Car();
Car carToPaint = car;

If you change the paint color using the second variable, the first variable will be also affected as both point to the same instance of the car class:

public class Car
{
   public String color = "red";
}

Car car = new Car();
Car carToPaint = car; 
Console.WriteLine(car.color); // -> red 
Console.WriteLine(carToPaint.color); // -> red


carToPaint.color = "yellow";
Console.WriteLine(car.color); // -> yellow 
Console.WriteLine(carToPaint.color); // -> yellow

See the difference? ⚡️This saves lots of memory space when you need to pass the same object around in your program. However, it can also be dangerous as changing an object in one part of the program will affect all other parts of the program working with the same object.

This is something you'll need to pay attention to with function arguments. When you pass reference types as arguments, all modifications you perform on them within those functions modify the original objects:

public class Car
{
   public String color = "red"; 
}


public static void Paint( Car car, String color)
{ 
   car.color = color; 
}


Car car = new Car(); 
Console.WriteLine(car.color); // -> red
Paint(car, "green"); 
Console.WriteLine(car.color); // -> green 

Hmm...what about function arguments being constants in C#? Arguments of a function in C# are constants and cannot be modified, which means you cannot assign a new value to the whole new object (or, a reference to an object). But you can modify attributes of that object, which is exactly what we did in the example above.

Lots of new knowledge here. You are progressing so fast!

Let's recap!

In this chapter, you've learned:

  • Functions can have parameters and return values.

  • A return value is a result of the function's execution. It can be returned to the block of code that called the function and then used as needed.

  • Parameters are the input for a function that are necessary for it to execute and produce a result.

  • Parameters are variables defined by name and type. They are specified in the function's declaration.

  • When calling a function, you pass values to its parameters. Those values are called arguments.

Example of certificate of achievement
Example of certificate of achievement