Menu
Coddy logo textTech

Return Types

Part of the Fundamentals section of Coddy's C# journey — lesson 51 of 69.

The return statement in a method is used to specify the value or values that the method should produce as its output. For example, the following method will output 100:

public static int FunctionName() {
	return 100;
}

To pass the value to a variable, write:

int number = FunctionName();

Now the number variable will hold 100 because this is what the method returned.

Note that the return type of the method (int in this case) must match the data type of the variable where you're storing the returned value.

challenge icon

Challenge

Easy

Each test case has three inputs:

  • The first input indicates how many times to do iterations
  • The last two inputs are numbers that we will do operations on

Create a method that receives two arguments and returns the bigger number of the two. If both are equal, return either one.

In the main method:

Iterate iterations times. For each iteration:

  1. Call the Bigger method with num1 and num2, and save the result in a variable
  2. Identify which original variable (num1 or num2) holds the bigger value
  3. Divide that original variable by 2 and update it
    • Example: If num1 is bigger, then do: num1 = num1 / 2
  4. Print the newly updated value

Stop the loop early if either num1 or num2 becomes smaller than 2.

Try it yourself

using System;

public class Program {
    public static double Bigger(double arg1, double arg2) {
        // Complete the method
        
    }

    public static void Main(string[] args) {
        int iterations = int.Parse(Console.ReadLine());
        double num1 = double.Parse(Console.ReadLine());
        double num2 = double.Parse(Console.ReadLine());

        for (int i = 0; i < iterations; i++) {
            // Write your code below
            
        }
        
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals