如果一个函数试图一次返回多个值,会发生什么?
先决条件:如何从 C 或 C++ 中的函数返回多个值?
在处理函数调用时,经常会出现使用 return 语句的情况。通常,根据函数的返回类型,无论是原始数据类型(如整数、字符等)还是非原始数据类型(如数组、字符串、向量等),都只会返回一件事。
但是如果我们尝试通过 return 语句返回多个值会发生什么?
本文重点讨论通过 return 语句返回多个值的场景。
预测输出:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function declaration and
// definition
int AddSub(int c, int d)
{
int x, y;
x = c - d;
y = c + d;
// Returning two integers
// instead of one
return (x, y);
}
// Driver code
int main()
{
// Initializing the variables
int i = 100, j = 200, k;
// Calling AddSub function
k = AddSub(j, i);
// Printing k
cout << "The value of k = " << k;
}
C
// C program to implement
// the above approach
#include
// Function declaration and
// definition
int AddSub(int c, int d)
{
int x, y;
x = c - d;
y = c + d;
// Returning two integers
// instead of one
return (x, y);
}
// Driver code
int main()
{
// Initializing the variables
int i = 100, j = 200, k;
// Calling AddSub function
k = AddSub(j, i);
// Printing k
printf("The value of k = %d", k);
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function declaration and
// definition
static int[] AddSub(int c, int d)
{
int x, y;
x = c - d;
y = c + d;
// Returning two integers
// instead of one
return new int[]{x, y};
}
// Driver code
public static void main(String[] args)
{
// Initializing the variables
int i = 100, j = 200;
int []k;
// Calling AddSub function
k = AddSub(j, i);
// Printing k
System.out.print("The value of k = " + (k[k.length-1]));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program to implement
# the above approach
# Function declaration and
# definition
def AddSub(c, d):
x, y = 0, 0;
x = c - d;
y = c + d;
# Returning two integers
# instead of one
return [x, y];
# Driver code
if __name__ == '__main__':
# Initializing the variables
i = 100; j = 200;
k = 0;
# Calling AddSub function
k = AddSub(j, i);
# Printing k
print("The value of k = ", (k[len(k) - 1]));
# This code is contributed by 29AjayKumar
C#
// C# program to implement
// the above approach
using System;
public class GFG{
// Function declaration and
// definition
static int[] AddSub(int c, int d)
{
int x, y;
x = c - d;
y = c + d;
// Returning two integers
// instead of one
return new int[]{x, y};
}
// Driver code
public static void Main(String[] args)
{
// Initializing the variables
int i = 100, j = 200;
int []k;
// Calling AddSub function
k = AddSub(j, i);
// Printing k
Console.Write("The value of k = " + (k[k.Length-1]));
}
}
// This code is contributed by shikhasingrajput
Javascript
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function declaration and
// definition
int Operations(int c, int d)
{
int p, q, r, s, t;
p = c - d;
q = c + d;
r = c * d;
s = c / d;
t = c % d;
// Returning multiple integers
// instead of one
return p, q, r, s, t;
}
// Driver code
int main()
{
// Initializing the variables
int i = 100, j = 200, k;
// Calling Operations function
k = Operations(j, i);
// Printing k
printf("The value of k = %d", k);
}
C
// C program to implement
// the above approach
#include
// Function declaration and
// definition
int Operations(int c, int d)
{
int p, q, r, s, t;
p = c - d;
q = c + d;
r = c * d;
s = c / d;
t = c % d;
// Returning multiple integers
// instead of one
return (p, q, r, s, t);
}
// Driver code
int main()
{
// Initializing the variables
int i = 100, j = 200, k;
// Calling Operations function
k = Operations(j, i);
// Printing k
printf("The value of k = %d", k);
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function declaration and
// definition
static int []Operations(int c, int d)
{
int p, q, r, s, t;
p = c - d;
q = c + d;
r = c * d;
s = c / d;
t = c % d;
// Returning multiple integers
// instead of one
return new int[]{ p, q, r, s, t };
}
// Driver code
public static void main(String[] args)
{
// Initializing the variables
int i = 100, j = 200;
int []k;
// Calling Operations function
k = Operations(j, i);
// Printing k
System.out.printf("The value of k = %d",
k[k.length - 1]);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python program to implement
# the above approach
# Function declaration and
# definition
def Operations(c, d):
p, q, r, s, t = 0,0,0,0,0;
p = c - d;
q = c + d;
r = c * d;
s = c / d;
t = c % d;
# Returning multiple integers
# instead of one
return [p, q, r, s, t ];
# Driver code
if __name__ == '__main__':
# Initializing the variables
i = 100; j = 200;
k = 0;
# Calling Operations function
k = Operations(j, i);
# Printing k
print("The value of k = ", k[len(k) - 1]);
# This code is contributed by shikhasingrajput
C#
// C# program to implement
// the above approach
using System;
public class GFG
{
// Function declaration and
// definition
static int []Operations(int c, int d)
{
int p, q, r, s, t;
p = c - d;
q = c + d;
r = c * d;
s = c / d;
t = c % d;
// Returning multiple integers
// instead of one
return new int[]{ p, q, r, s, t };
}
// Driver code
public static void Main(String[] args)
{
// Initializing the variables
int i = 100, j = 200;
int []k;
// Calling Operations function
k = Operations(j, i);
// Printing k
Console.Write("The value of k = {0}",
k[k.Length - 1]);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
The value of k = 300
解释:
你们中的大多数人一定想知道上面代码的输出会是一个“错误”,因为在上面的代码中,超过允许的值被返回给函数调用。
在这种情况下,如果返回多个值而没有采取特殊的预防措施,如数组、指针、结构、引用、元组、类和对象,那么在这种情况下,将返回最后一个值,而之前的所有值都将被忽略。
返回值的数量可以很多,但只会返回最后一个出现的值,也可以忽略括号或大括号“()”,并且可以不使用大括号编写,只需用逗号分隔多个值,如下面的代码所示:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function declaration and
// definition
int Operations(int c, int d)
{
int p, q, r, s, t;
p = c - d;
q = c + d;
r = c * d;
s = c / d;
t = c % d;
// Returning multiple integers
// instead of one
return p, q, r, s, t;
}
// Driver code
int main()
{
// Initializing the variables
int i = 100, j = 200, k;
// Calling Operations function
k = Operations(j, i);
// Printing k
printf("The value of k = %d", k);
}
C
// C program to implement
// the above approach
#include
// Function declaration and
// definition
int Operations(int c, int d)
{
int p, q, r, s, t;
p = c - d;
q = c + d;
r = c * d;
s = c / d;
t = c % d;
// Returning multiple integers
// instead of one
return (p, q, r, s, t);
}
// Driver code
int main()
{
// Initializing the variables
int i = 100, j = 200, k;
// Calling Operations function
k = Operations(j, i);
// Printing k
printf("The value of k = %d", k);
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function declaration and
// definition
static int []Operations(int c, int d)
{
int p, q, r, s, t;
p = c - d;
q = c + d;
r = c * d;
s = c / d;
t = c % d;
// Returning multiple integers
// instead of one
return new int[]{ p, q, r, s, t };
}
// Driver code
public static void main(String[] args)
{
// Initializing the variables
int i = 100, j = 200;
int []k;
// Calling Operations function
k = Operations(j, i);
// Printing k
System.out.printf("The value of k = %d",
k[k.length - 1]);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python program to implement
# the above approach
# Function declaration and
# definition
def Operations(c, d):
p, q, r, s, t = 0,0,0,0,0;
p = c - d;
q = c + d;
r = c * d;
s = c / d;
t = c % d;
# Returning multiple integers
# instead of one
return [p, q, r, s, t ];
# Driver code
if __name__ == '__main__':
# Initializing the variables
i = 100; j = 200;
k = 0;
# Calling Operations function
k = Operations(j, i);
# Printing k
print("The value of k = ", k[len(k) - 1]);
# This code is contributed by shikhasingrajput
C#
// C# program to implement
// the above approach
using System;
public class GFG
{
// Function declaration and
// definition
static int []Operations(int c, int d)
{
int p, q, r, s, t;
p = c - d;
q = c + d;
r = c * d;
s = c / d;
t = c % d;
// Returning multiple integers
// instead of one
return new int[]{ p, q, r, s, t };
}
// Driver code
public static void Main(String[] args)
{
// Initializing the variables
int i = 100, j = 200;
int []k;
// Calling Operations function
k = Operations(j, i);
// Printing k
Console.Write("The value of k = {0}",
k[k.Length - 1]);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
The value of k = 0
解释:
这里输出为 0,因为最后一个变量 t 等于 c % d = 0。所以当返回多个值时, t的值被分配给变量k。