Parameters.

posted by: Ms. Martin 7 October 2009 3 Comments

We talked about the need to reuse similar code in slightly different situations. It seemed intuitive to think we would need some kind of variable but we saw that scope rules kept us from making information from one method available in another. The scope of a variable refers to where in the program it exists — only between the curly braces in which it is declared. For example, a for loop counter i’s scope is limited to the for loop body so it cannot be refered to before or after the loop.

We saw that the solution to this was to use parameters: information that is passed between methods. In order for a method to accept a parameter, the type and name of that parameter must be listed in the method header. Multiple parameters must be separated by a comma as in:

public static void oldAge(double age, int years) {
    System.out.println("In " + years + " years, you will be " + (age + years) + " years old.");

}

When the method is called, values for the parameters must be passed in.  For example:

oldAge(45, 10);

In that case, the values 45 and 10 are copied into the parameters age and years, respectively.  We discussed the importance of understanding that the values are copied: if we modify them inside the method they are passed to, they do not change in the method they were passed from.

Java knows which value to assign to which parameter based on order.  To test your mental model of parameter passing, I had you do a few confusing and irritating parameter mystery problems.

We also discussed an article from CMU discussing the incorporation of computational biology into the computer science department.  We talked about computational biology as a “hot” field we should pay attention to.  We also talked about why computation is so important in the sciences.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>