algorithmflutterdartswap

I have problem swap variables function in Dart


I'm gonna crazy everything is looking good where is the problem?

void main(){
    int num1 = 10;
    int num2 = 11;
    
    print("First : $num1, $num2");
    
   swap(num1,num2);
    
    print("Last : $num1, $num2");
}
void swap(int num1,int num2){
    int temp;
    
    temp = num1;
    num1 = num2;
    num2 = temp;

}

MY OUTPUT: First: 10,11 Last: 10,11


Solution

  • @Ketan Ramteke is right. Or you can return a list and use that value like this:

    void main(){
        int num1 = 10;
        int num2 = 11;
        
        print("First : $num1, $num2");
        
        List arr = swap(num1,num2);
        
        print("Last : ${arr[0]}, ${arr[1]}");
    }
    
    List swap(int num1,int num2){
        int temp;
        
        temp = num1;
        num1 = num2;
        num2 = temp;
        return [num1, num2];
    }