Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Raihan Khan
Raihan Khan
2,168 Points

What is the purpose of using Parameter?

I want to know the difference between using just Variable and Parameter. Why not just use Variable instead of a parameter. It is confusing to use parameter I want more clarification.

1 Answer

Steven Parker
Steven Parker
231,261 Points

Variables are used anywhere in a program, but parameters are used specifically to pass information to a function. Using parameters with functions makes them easier to maintain, easier to re-use in other programs, and easier for other developers reading the code to understand what information the function requires and uses.

Raihan Khan
Raihan Khan
2,168 Points

But why not use a variable inside the function. I understand easier to maintain and easier to read code but still not clear about it.

Steven Parker
Steven Parker
231,261 Points

It is "safer" in a way, since the scope is limited to the function, and it is also more efficient.

For example, without using a parameter:

public void PrintNumber() {
    Console.WriteLine(number);
}

public int number;   // a variable is created
number = 7;          // the variable is assigned with the desired value
PrintNumber();       // then the function is called

But if you do use a parameter:

public void PrintNumber(num) {
    Console.WriteLine(num);
}

PrintNumber(7);      // the function is called with argument, no extra steps