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
EasyEach 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:
- Call the
Biggermethod withnum1andnum2, and save the result in a variable - Identify which original variable (
num1ornum2) holds the bigger value - Divide that original variable by 2 and update it
- Example: If
num1is bigger, then do:num1 = num1 / 2
- Example: If
- 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
}
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic Shortcuts10Methods (Functions)
Declaring MethodsMethod ParametersReturn TypesOptional ParametersRecap - Validation FunctionVoid Methods5Operators Part 2
Comparison OperatorsLogical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3