# Swap Function Won't Accept Correct Parameters

**URL:** <https://discourse.processing.org/t/swap-function-wont-accept-correct-parameters/3435>\
**Category:** Coding Questions\
**Created:** [September 10, 2018, 3:57pm UTC](https://discourse.processing.org/t/swap-function-wont-accept-correct-parameters/3435 "2018-09-10T15:57:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![OrangeC7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/orangec7/32/1467_2.png) [@OrangeC7](https://discourse.processing.org/u/OrangeC7)\
**Post date:** [September 10, 2018, 3:57pm UTC](https://discourse.processing.org/t/swap-function-wont-accept-correct-parameters/3435/1 "2018-09-10T15:57:59Z")

</div>

I’m trying to use a simple swap function, but for some reason it won’t accept the array of custom objects as a parameter even though that’s what I specified when making the function. I made sure the array I put in is the correct type, but it isn’t working. I’m sure the fix is simple, perhaps I missed something when I set up the “Card” class, but I’ve tried searching it on Google and nothing helpful comes up. There wasn’t much code to cut out since I just started this, so I hope I didn’t leave too much in.

The problem is on line 29, the error message is  
_The function swap() expects parameters like: " swap(Card[], int, int)"_

```auto
class Card {
  
  boolean faceUp = true;
  
  void flip() {
    this.faceUp = !this.faceUp;
  };
  
}

Card[] cards = new Card[5];

void setup() {
  size( 256, 512 );
  background(0);
  
  for ( int i = 0; i < 5; i++ ) {
    cards[i] = new Card();
  }
}

void doFlip( int amt ) {
  if ( amt % 2 == 0 ) {    
    for ( int i = 0; i < amt / 2; i++ ) {
      swap( cards, cards[i], cards[amt - i] );
    }
  }
}

void swap( Card[] array, int a, int b ) {
  Card temp = array[a];
  array[a] = array[b];
  array[b] = temp; 
}

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 10, 2018, 4:05pm UTC](https://discourse.processing.org/t/swap-function-wont-accept-correct-parameters/3435/2 "2018-09-10T16:05:27Z")

</div>

Hi OrangeC7,

When you do `cards[i]` you are getting a Card, not an integer.

So your swap function should be `void swap(Card[] array, card a, card b)`.

That’s why you are getting your error 😉

EDIT

Actually, after reading your swap function you should change the call to your function, not the function itself.

Replace `swap(cards, cards[i], cards[amt - i] );` by `swap(cards, i, amt - i);`

---

<div class="post-metadata">

**Author:** ![OrangeC7](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/orangec7/32/1467_2.png) [@OrangeC7](https://discourse.processing.org/u/OrangeC7)\
**Post date:** [September 10, 2018, 4:16pm UTC](https://discourse.processing.org/t/swap-function-wont-accept-correct-parameters/3435/3 "2018-09-10T16:16:58Z")

</div>

Ah, thanks! I knew the fix would be as simple as that. 😃
