Menu
Coddy logo textTech

Method Parameters

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

An argument is a value that you pass into a method when you call it. A parameter is a variable in the method definition that receives that value. Parameters are written inside the parentheses () of the method definition:

public static return_type method_name(data_type param1, data_type param2, ...) {
	// code
}

The return_type specifies what type of value the method will return. When a method doesn't need to return any value (like when it just prints something), use void as the return type.

For now, just always write public static before your method name - we'll explain what each word means later.

We can name the parameters as we want and we can write as many parameters as we need.

To call a method and pass arguments to it we write:

method_name(value1, value2, value3, ...);

Passing too many arguments to a method that is expecting less arguments will cause the program to fail

Example of usage:

public static void IsEven(int number) {
	if (number % 2 == 0) {
		Console.WriteLine(number + " is even");
	} else {
		Console.WriteLine(number + " is odd");
	}
}

for (int i = 15; i < 34; i++) {
	IsEven(i);
}
for (int i = 153; i < 219; i++) {
	IsEven(i);
}

Here we have a method called IsEven that defines one parameter called number and prints if the number is even or odd. Then we call the method twice, passing arguments: one time for all the numbers between 15 and 34, second time for all numbers between 153 and 219.

challenge icon

Challenge

Easy

Write a program that gets two inputs, numbers. The input numbers are the arguments of the method. 

Create a method that gets two arguments, calculates the product of them and prints it, name the method however you like.

Call the method with the input numbers.

Note! In your code, write the method before it's call/execution statements.

Cheat sheet

Method parameters are variables defined in the method signature that receive values. Method arguments are the actual values passed when calling the method. Define parameters inside parentheses with their data types:

public static return_type method_name(data_type param1, data_type param2, ...) {
	// code
}

The return_type specifies what type of value the method will return. When a method doesn't need to return any value (like when it just prints something), use void as the return type.

Call a method by passing arguments:

method_name(value1, value2, value3, ...);

Example:

public static void IsEven(int number) {
	if (number % 2 == 0) {
		Console.WriteLine(number + " is even");
	} else {
		Console.WriteLine(number + " is odd");
	}
}

IsEven(15); // Call method with argument 15

Note: Passing too many arguments to a method will cause the program to fail.

Try it yourself

using System;

public class Program {
    // Method declaration
    
    
    public static void Main(string[] args) {
        int a = int.Parse(Console.ReadLine());
        int b = int.Parse(Console.ReadLine());
        // Call the method with a and b as arguments
        
    }
}
quiz iconTest yourself

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

All lessons in Fundamentals