11 Arguments-Returns

11 Arguments-Returns - writing functions with arguments...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: writing functions with arguments writing functions with arguments Up to now, all the functions we have written have had empty parenthesis and looked like void function() Since the parenthesis are empty, the functions do not take any arguments We have used functions with arguments: point(x,y); line(30,50,30,250); ellipse(50,10,100,80); stroke(255,0,0); 1 Pass by Value arguments Pass by Value arguments To write a function with arguments, you add variable declarations inside the parenthesis of the function header void setup() { area(50,100); area(50,100); } void draw(){}//empty! void area (int someWidth, int someLength) { } println("The area is "+(someWidth*someLength)); println("The 2 Pass by Value arguments Pass by Value arguments Notice that the data types only go in the header, NOT in the function call void setup() { area(50,100); area(50,100); } void draw(){}//empty! void area (int someWidth, int someLength) someLength) { } println("The area is "+(someWidth*someLength)); println("The 3 Pass by Value arguments Pass by Value arguments When the function is called the arguments are copied into someWidth and someLength void setup() { area(50,100); area(50,100); } void draw(){}//empty! void area (int someWidth, int someLength) someLength { } println("The area is "+(someWidth*someLength)); println("The 4 Pass by Value arguments Pass by Value arguments void setup() { area(50,100); area(50,100); } void draw(){}//empty! void area (int someWidth, int someLength) { } println("The area is "+(someWidth*someLength)); println("The 5 Practice Quiz Question: What is the output of this Practice Quiz Question: What is the output of this program? void setup() { mystery1(4, 1); mystery1(4, int num = 5; int mystery1(4,num); mystery1(4,num); mystery2(5,2); mystery2(5,2); } void draw(){}//empty! void mystery1 (int a, int b) { println((a - 2) * (b + 3)); println((a } void mystery2 (float c, int d) { println(c / d); println(c } 6 void void Means "nothing" Every function we’ve written up to now has used void and looked like: void function (int num, float fNum); You can replace that “nothing” with “something” int function (int num, float fNum); This means the function returns an integer 7 Functions that return values Functions that void setup() { println("The answer is " + someFunction(4.5)); someFunction(4.5)); } void draw(){}//empty! float someFunction (float fNum) { return fNum/2; return } 8 Functions that return values Functions that void setup() { println("The answer is " + someFunction(4.5)); someFunction(4.5)); } void draw(){}//empty! float someFunction (float fNum) { return fNum/2; return } 9 Functions that return values Functions that void setup() { println("The answer is " + someFunction(4.5)); someFunction(4.5)); } void draw(){}//empty! float someFunction (float fNum) { return fNum/2; return } 10 Functions that return values Functions that void setup() { println("The answer is " + someFunction(4.5)); someFunction(4.5)); } void draw(){}//empty! float someFunction (float fNum) { return fNum/2; return } 11 return return Sends data back to the function call println("The answer is " + println("The someFunction(4.5)); someFunction(4.5)); becomes println("The answer is " + 2.25); 12 Practice Quiz Question: what Practice Quiz Question: what is the output of this program? void setup() { int num = mystery1(4,2); int float fract = mystery2(5,2); float println(num*fract); println(num*fract); } void draw(){}//empty! int mystery1 (int nA, int nB) { return nA + nB; return } float mystery2(int nA, int nB) { return float(nA)/nB; return } 13 ...
View Full Document

Ask a homework question - tutors are online