Function Basics: Parameter, Arguments, and Returning Values
Explore the core concepts of functions in C#, including the differences between parameters and arguments. Learn how to write functions that accept inputs and return values with practical examples like tax calculation. This lesson helps you understand how to structure and use functions effectively for robust application development.
We'll cover the following...
Most developers will use the terms argument and parameter interchangeably in daily usage. Strictly speaking, the two terms have specific and subtly different meanings. But just like a person can be both a parent and a doctor, the terms often apply to the same thing.
Parameter and arguments
A parameter is a variable in a function definition. For example, startDate is a parameter of the Hire function, as shown in the following code:
When a method is called, an argument is the data we pass into the method’s parameters. For example, when is a variable passed as an argument to the Hire function, as shown in the following code:
Example
We might prefer to specify the parameter name when passing the argument, as shown in the following code:
When talking about the call to the Hire function, startDate is the parameter, and when is the argument.
Note: The official Microsoft documentation uses the phrases “named and optional arguments” and “named and optional parameters” interchangeably. We can ...