Topic
• 2 replies
• Latest Post - ‏2013-06-10T15:09:29Z by özgür_ak
özgür_ak
10 Posts

# Pinned topic Function Script in OPL

‏2013-06-07T11:59:54Z | function script

execute{

function swap(p1, p2) {   //this function basicly changes the values of p1 and p2.

temp=p1; p1=p2; p2=temp;

}

p1=10; p2=20;

swap(p1, p2);

writeln("p1=", p1 ); writeln(''p2='' , p2); }

the out put must be p1=20 ; p2=10

but it is not. how can i make this function work???

• AlexFleischer
3151 Posts

#### Re: Function Script in OPL

‏2013-06-09T14:42:39Z

Hi,

here you can find an example that works

int a[i in 1..5]=i;

execute{

function swap(p1, p2) {

temp=a[p1]; a[p1]=a[p2]; a[p2]=temp;

}

writeln(a);
swap(2,3);
writeln(a);
}

which gives

[1 2 3 4 5]
[1 3 2 4 5]

In your example, the values were read by the fuction, not the references

regards

• AlexFleischer
3151 Posts

#### Re: Function Script in OPL

‏2013-06-09T14:42:39Z

Hi,

here you can find an example that works

int a[i in 1..5]=i;

execute{

function swap(p1, p2) {

temp=a[p1]; a[p1]=a[p2]; a[p2]=temp;

}

writeln(a);
swap(2,3);
writeln(a);
}

which gives

[1 2 3 4 5]
[1 3 2 4 5]

In your example, the values were read by the fuction, not the references

regards

• özgür_ak
10 Posts

#### Re: Function Script in OPL

‏2013-06-10T15:09:29Z

Hi,

here you can find an example that works

int a[i in 1..5]=i;

execute{

function swap(p1, p2) {

temp=a[p1]; a[p1]=a[p2]; a[p2]=temp;

}

writeln(a);
swap(2,3);
writeln(a);
}

which gives

[1 2 3 4 5]
[1 3 2 4 5]

In your example, the values were read by the fuction, not the references

regards

thanks :) it worked...