ChatGPT uses Java, JavaScript, Go, C, C++ and Python to exchange two variable values ​​​​with functions

Original link: https://fugary.com/?p=506

Languages ​​that support pointers are very convenient to implement variable exchange. Languages ​​that do not support pointers are easier to implement with tuple or array destructuring. However, Java does not seem to be able to exchange the values ​​​​of two variables directly with functions, unless they are wrapped with objects or arrays.

The following are sample codes for exchanging two variable values ​​with functions using Java, JavaScript, Go, C, C++, and Python respectively, and provide a brief description:

Java

 public class Main { public static void main(String[] args) { int a = 5; int b = 10; System.out.println("Before swap: a = " + a + ", b = " + b); swap(a, b); System.out.println("After swap: a = " + a + ", b = " + b); } public static void swap(int x, int y) { int temp = x; x = y; y = temp; } }

In Java, parameters are passed by value, so what is exchanged in swap function is the temporary variable in the function, without affecting the variables in main function.

JavaScript

 function swap(a, b) { [a, b] = [b, a]; return [a, b]; } let x = 5; let y = 10; console.log(`Before swap: x = ${x}, y = ${y}`); [x, y] = swap(x, y); console.log(`After swap: x = ${x}, y = ${y}`);

In JavaScript, the values ​​of two variables can be easily swapped using the destructuring assignment syntax.

Go

 package main import "fmt" func main() { a := 5 b := 10 fmt.Printf("Before swap: a = %d, b = %d\n", a, b) swap(&a, &b) fmt.Printf("After swap: a = %d, b = %d\n", a, b) } func swap(x, y *int) { temp := *x *x = *y *y = temp }

In Go, variables outside functions can be modified by passing pointers.

C

 #include <stdio.h> void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } int main() { int a = 5; int b = 10; printf("Before swap: a = %d, b = %d\n", a, b); swap(&a, &b); printf("After swap: a = %d, b = %d\n", a, b); return 0; }

In C language, variables outside functions can be modified by passing pointers.

C++

 #include <iostream> void swap(int &x, int &y) { int temp = x; x = y; y = temp; } int main() { int a = 5; int b = 10; std::cout << "Before swap: a = " << a << ", b = " << b << std::endl; swap(a, b); std::cout << "After swap: a = " << a << ", b = " << b << std::endl; return 0; }

In C++, you can use references to modify variables outside functions.

Python

 def swap(a, b): return b, ax = 5 y = 10 print(f"Before swap: x = {x}, y = {y}") x, y = swap(x, y) print(f"After swap: x = {x}, y = {y}")

In Python, you can use tuple unpacking to swap the values ​​of two variables.

These sample codes demonstrate how to use functions to swap the values ​​of two variables and provide brief instructions. Note that different programming languages ​​may differ in variable passing and syntax.

This article is transferred from: https://fugary.com/?p=506
This site is only for collection, and the copyright belongs to the original author.