// HowTo write Functions with a Variable Number of arguments // James Brink 2/10/2020 // Globals let commandColor; let resultColor; let sectionColor; let example1Color // Example 1 function function testing(a, b, c) { var s = "a: " + a + ", b: " + b + ", c: " + c; if (a == null) { s += ". There are no arguments"; } return s; } // Example 2 class class Person { constructor(itsFirstName, itsLastName) { this.firstName = itsFirstName; this.lastName = itsLastName; } name(f, l){ if (f == null) { return this.firstName + " " + this.lastName; } this.firstName = f; if (l != null) { this.lastName = l; } return; } // name } // end person // Required setup function setup() { let canvas = createCanvas(450, 290); canvas.parent("p5-left"); sectionColor = color(0, 0, 0); example1Color = color(100, 0, 100); commandColor = color(60, 60, 0); resultColor = color(0, 100, 100); } // setup // show the results function draw() { background(color(168, 168, 255)); // Example 1 fill(sectionColor); text('Example 1', 1, 20); fill(example1Color); text('testing(10, 20, "book") = ' + testing(1, 2, "book"), 20, 40); text('testing(10, 20) = ' + testing(10, 20), 20, 60); text('testing(10) = ' + testing(10), 20, 80); text('testing() = ' + testing(), 20, 100); // Example 2 fill(sectionColor); text('Example 2', 1, 140); text('Code', 50, 160); text('Result', 250, 160); let child = new Person("John", "Doe"); fill(commandColor); text('let child = new Person("John", "Doe");', 50, 180); text('child.name();', 50, 200); fill(resultColor); text(child.name(), 250, 200); child.name("Sally"); fill(commandColor); text('child.name("Sally");' , 50, 220); text('child.name();', 50, 240); fill(resultColor); text(child.name(), 250, 240); child.name("Mary", "Smith"); fill(commandColor); text('child.name("Mary", "Smith");', 50, 260); text('child.name();', 50, 280); fill(resultColor); text(child.name(), 250, 280); } // draw