Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Jae Kim
Jae Kim
3,044 Points

What function do parameters serve in methods?

I read some of the Java documentation on methods and parameter formatting, but still feel like I am missing some knowledge in method signatures.

I understand that defining a method takes: A modifier, return type, method name, parameter, exception list, and a method body with code that goes within the curly braces.

However, I am confused as to what function a parameter has within a method.

E.g. int total (int aNumber) {int aValue = aNumber + 20}; return aValue;}

I pulled this example online and just do not seem to get what the purpose of declaring another variable name like aValue serves. I get that the evaluation takes 'aNumber + 20' and then returns that value, but is this stored in 'int total'?

3 Answers

Nicholas Grenwalt
Nicholas Grenwalt
46,626 Points

No problem. "int total" is what you have named the entire function. It could be named "int" followed by any name you'd like, but since this function that we created returns a total it is best to have its name be descriptive of what the function is/does. This is common practice in naming variables/functions in most languages. Another good name for this function can be "someNumberPlusTwenty" since whatever number that you pass as an argument will be added to 20 inside the function. Imagine that there are a million different complex calculations to a formula inside that function that you wrote out once. All you would have to do is call that function name again with a different number passed in as an argument and it would do all the calculations and pass you back the answer to do whatever you want with it. The answer is returned and stored inside the "aValue" variable. Perhaps plug it into another function, or display it in an alert. Tons of options. Your function doesn't store your answer, just contains all of the 'function'ality used to get to your answer.

Jae Kim
Jae Kim
3,044 Points

That actually cleared things up so much more now. Now this might be getting a little bit ahead, but can you combine functions, or is this generally bad practice to do so?

So can I utilize all the functionality in one function and then merge it with another?

Nicholas Grenwalt
Nicholas Grenwalt
46,626 Points

The 'aValue' variable is holding the answer to your equation so that you can make the function more versatile. The integer parameter 'aNumber' that you pass in will be a piece to that equation. Let's say you want to pass in 5. 5 will go into the function where it says 'aNumber' and replace it. 5 will be added to 20 and stored in the variable 'aValue' and then that variable that has stored your answer 25 will be returned to you via the 'return' statement used in combination with the variable.

Now anytime you call that function again, passing in another number, it will be able to send you back your answer it one simple line of code.

Hope that helped clear some things up.

Jae Kim
Jae Kim
3,044 Points

Thank you. I was wondering what happens to int total then. So the number that is returned is 25 due to the 'return' statement, but what purpose int total serve then? Is the value of 25 stored in the variable name total?

John Marley
John Marley
8,740 Points

So effectively the parameter is the thing you want to be able to change?