8.6: Pass by Value vs. Pass by Reference – Processing Tutorial

This video covers passing arguments into a function and what happens when you pass an object vs. a primitive value.

Book: Learning Processing A Beginner’s Guide to Programming, Images,Animation, and Interaction

Chapter: 8

Official book website:

Twitter:
annotations added: click here to subscribe

Help us caption & translate this video!


Posted

in

by

Tags:

Comments

25 responses to “8.6: Pass by Value vs. Pass by Reference – Processing Tutorial”

  1. Rishi Sanas Avatar

    Java doesnt support pass by reference

  2. 李少轩 Avatar

    hey sir i wanna ask is pass by value equal to pass by copy that you stated ?

  3. Fausto Mauricio Lagos Suárez Avatar

    How a can do that when overlap the particles change color but when no overlap conserv the original color, i.e. if I use one constructor than have color as a input argument, how can return to this color when no overlap the particles?

  4. Asaf Einihovich Avatar

    i love this man so much

  5. Juan Carlos Restrepo Saldarriaga Avatar

    I like very much your videos, thanks you. I have a doubt with this code. Why the Particle p3 do not behave the same with other Particles, p1 and p2; what is wrong here?

    Particle p1;
    Particle p2;
    Particle p3;

    void setup() {
    size(600,400);
    p1 = new Particle(100,220,50);
    p2 = new Particle(450,180,70);
    p3 = new Particle();
    }

    void draw() {
    background(0);

    p3.overlaps(p1);
    p3.overlaps(p2);

    p1.display();
    p2.display();
    p3.display();

    p3.x = mouseX;
    p3.y = mouseY;

    }

    class Particle {
    color col;
    float x;
    float y;
    float r;

    Particle() {
    x = random(width);
    y = random(height);
    r = random(30, 100);
    col = color(200);

    }
    Particle(float tempX, float tempY, float tempR) {
    x = tempX;
    y = tempY;
    r = tempR;
    col = color(200);
    }

    void display() {
    stroke(250);
    strokeWeight(2);
    fill(col);
    ellipse(x, y, r*2, r*2);

    }

    void overlaps(Particle other) {

    float d = dist(x, y, other.x, other.y);
    if (d < r + other.r) {
    col = color( 255, 0, 0, 200);
    other.col = color(0, 255, 0, 200);
    } else {
    col = color(0, 200);
    other.col = color(0, 200);
    }
    }

    }

  6. TheEnglishQuail Avatar

    UGH. It feels like nobody online is able to explain this concept in a way I can understand it 🙁

  7. Keshav Sapra Avatar

    lovely explanation!

  8. ahnaf nafi Avatar

    Great explanation

  9. zerocool 18 Avatar

    thank you very much , nicely explained

  10. mohamed saad Avatar

    Fantaaaaaaaaaaaaaastic

  11. KOUSIK daniel Avatar

    in first example you can pass two parameter in object but in method you have got only one argument….why? and number 50,100 are stored in memory but aP is how to replace 50 only

  12. Manuel A. Alonso Tarajano Avatar

    so cool your explanations ! great job.

  13. Liam Attard Avatar

    I'm a beginner, but why does it change if there is void, doesn't void mean no return 🙁 ?

  14. kevin benavides Avatar

    Thank you for clarifying this topic.

  15. Gamer Sparta Avatar

    It's the same in C++ ?

  16. Rick Cable Avatar

    Great video! Thanks

  17. Peter Bandsholm Avatar

    Shiffman for president!!

  18. Lucas Bitar Piragine Avatar

    tried to add a particle but… (read more!)

    Heyy, Big Up from Brasil!!

    thanks for everything!!!

    I tried to add one more particle and the overlap function doesn't apply fully to the first particle called(p2) . I mean, when it overlaps one of the particles, the mouseParticle (p2) goes red, and the other goes green, but when the p2 overlaps the other circle(p3), it doesn't go red, only the overlapped goes green but (p2) keeps black.

    i figured it out that the order of the functions does matter, and the "problem" occurs on the first function declared.

    example:

    p2.overlaps(p1);
    p2 overlaps(p3);

    in this case, when p2 overlaps p1, p2 is still black and p1 goes green, and p2 goes red normally when overlaps p3…. and if I change the order

    p2.overlaps(p3);
    p2.overlaps(p1);

    then p2 only goes red when overlaps p1…

    i just don't understand why….

    mycode:

    Particle p1;
    Particle p2;
    Particle p3;

    void setup() {
    size(600,400);

    p1 = new Particle(100, 100, 50);
    p2 = new Particle ();
    p3 = new Particle(200, 200, 30);

    }

    void draw(){

    background(0);

    p2.overlaps(p3);
    p2.overlaps(p1);

    p2.x = mouseX;
    p2.y = mouseY;

    p1.display();
    p2.display();
    p3.display();

    }

    ///

    class Particle {

    float x, y;
    float r;
    color col;

    Particle() {
    x = random(width);
    y = random(height);
    r = random(20, 50);
    col = color(0, 100);
    }

    Particle(float tempX, float tempY, float tempR) {

    x = tempX;
    y = tempY;
    r = tempR;
    }

    void overlaps(Particle other) {
    float d = dist(x, y, other.x, other.y);
    if (d < r + other.r) {
    col = color(255, 0, 0, 100);
    other.col = color(0, 255, 0, 100);
    } else {
    col = color(0, 100);
    other.col = color (0, 100);
    }
    }

    void display() {
    stroke(255);
    fill(col);
    ellipse(x, y, r*2, r*2);
    }
    }

  19. Mohsan KAYANI Avatar

    you confused me ! duahh !! but hats off liked your concept and java thingi !! :] thanks man !

  20. Hadeel Alharthi Avatar

    What a great teacher 🙂

  21. julia beilke Avatar

    Thank you so much. This is the first explanation I've found that actually helped rather than confusing me more

  22. Sentenced Avatar

    Thanks for the explanation. Do you have any more JAVA tutorials? I looked through you other videos but I can only find JS tutorials 🙁

  23. KingFiz Avatar

    Lmao I don't get the point of Pass by Value still, cause it just returns the same number. What's the point?

Leave a Reply

Your email address will not be published. Required fields are marked *