![]() You may also pass the values of expressions as arguments. Parameterful Methods: Literal Arguments (3) Since 5 is passed as the argument, then the method will display as follows: Inside the displayValue method, the variable num will contain the value of whatever argument was passed into it. The argument that is listed inside the parentheses is copied into the method's parameter variable, num. Here is an example of a call to the displayValue method, passing 5 as an integer literal argument: displayValue(5) This statement executes the displayValue method. This is the declaration of a parameter variable, which enables the displayValue method to accept an integer value as an argument. Notice the integer variable declaration that appears inside the parentheses (int num). Here is another definition of a method that uses a parameter: public static void displayValue(int num) //END displayValue(int): static void Parameterful Methods: Literal Arguments (2) public static void displayValue(int num) This statement calls the method and passes "Hello" as a String literal argument. Parameterful Methods: Literal Arguments (1) Example: ("Hello") A parameter variable,, sometimes simply referred to as a parameter, is a special variable that holds a value being passed to a method. ParseInt() converts the String "700" to the integer 700 to be stored in the variable number.īy using parameter variables, a method can be designed to accept data in various ways. The assignment statement takes the "700" in str, and passes it as a String to the parseInt() from the Integer class. Method arguments Parameterful Methods: Variable Arguments Example: String str = "700" Version 3: Value-Receiving Method with Complex Return Type public static String calcSalesTax(double taxRate, double subtotal) When arguments are passed to a method, the arguments can take several forms: A variable argument Version 2: Value-Receiving Method with Primitive Return Type public static double calcSalesTax(double taxRate, double subtotal) return types for a value-receiving methods and their method calls Version 1: Value-Receiving Method with void Return Type public static void calcSalesTax(double taxRate, double subtotal) return types for a value-receiving methods and their method calls return types for a value-receiving methods and their method calls The return type of parameterful/value-receiving methods\ can be void, a primitive data type, or a complex data type such as a class. Too many parameter variables reflect a method that is doing too much, so distributing its code across multiple methods that more accurately represent the code's purpose is in order. In line with keeping the coding of methods simple, straightforward, and focused on a particular purpose or task, the parameter list should be limited to a few necessary variables even though there is no limit. A method's parameter variables are separate and distinct from the arguments that are listed inside the parentheses of a method call.
0 Comments
Leave a Reply. |