Passing Arguments to Methods and Functions - Gnorion/BizVR GitHub Wiki

Passing Arguments to Methods and Functions

When passing arguments to a function or method there are two alternatives: Positional or Keyword.

  1. Positional - the values of the arguments must be passed in exactly the right order as comma separated values. That order must match the declaration of the function or method. Any arguments not being passed in must still use ,, placeholders to preserve the order
  2. Keyword - each argument passed in specifies the attribute name it is destined to set. Order is not important. Arguments not passed in are simply omitted.

Example

Suppose we have a class PERSON with the following attributes name, gender, citizenship, age, ssn, height, weight, eye-color etc (maybe there are dozens of attributes) How would we create a new instance of the PERSON class and populate its attributes

Positional Approach - arguments are comma separated values

PERSON.new('Joe',45,'456-23-5678') You then have to define the method "new" that takes these positional arguments, creates a new instance and then populates each of the attributes How do you know what the correct order is? What if you now want to set the gender with PERSON.new('Joe','male',45,'456-23-5678')

Example of the "new" class method defined as a decision table

Keyword Approach - arguments are passed along with their name

PERSON.new(name='Joe',gender='male',age=45,ssn='456-23-5678') In this case there is no need to define a special method. The built in "new" method is entirely generic and just needs the appropriate keyword-value pairs supplied.