新程序员通常都在寻找从一个函数中返回多个值的方法。不幸的是,C和c++不允许直接这样做。但幸运的是,通过一些聪明的编程,我们可以很容易地实现这一点。下面是在C语言中从一个函数返回多个值的方法
通过使用指针。
通过使用数据结构。
通过使用数组。
示例:一个实际例子,其中的任务是找出两个不同数字中的较大和较小数。我们可以写出多个函数。主要的问题是调用多个函数的麻烦,因为我们需要返回多个值,当然,有更多的代码行要输入
1、使用指针返回多个值:将参数的地址传递给参数,并使用指针更改参数的值。这样值就变成了原始参数
// Modified program using pointers
#include <iostream>
using namespace std;
// add is the short name for address
void compare(int a, int b, int* add_great, int* add_small)
{
if (a > b) {
// a is stored in the address pointed
// by the pointer variable *add_great
*add_great = a;
*add_small = b;
}
else {
*add_great = b;
*add_small = a;
}
}
// Driver code
int main()
{
int great, small, x, y;
cout << "Enter two numbers: \n";
cin >> x >> y;
// The last two arguments are passed
// by giving addresses of memory locations
compare(x, y, &great, &small);
cout << "\nThe greater number is " << great << " and the smaller number is "
<< small;
return 0;
}
// This code is contributed by sarajadhav12052009
Output:
Enter two numbers:
5 8
The greater number is 8 and the smaller number is 5
使用结构返回多个值:因为结构是用户定义的数据类型。其思想是定义一个具有两个整数变量的结构,并将较大和较小的值存储到这些变量中,然后使用该结构的值
// Modified program using structures
#include <stdio.h>
struct greaterSmaller {
int greater, smaller;
};
typedef struct greaterSmaller Struct;
Struct findGreaterSmaller(int a, int b)
{
Struct s;
if (a > b) {
s.greater = a;
s.smaller = b;
}
else {
s.greater = b;
s.smaller = a;
}
return s;
}
// Driver code
int main()
{
int x, y;
Struct result;
printf("Enter two numbers: \n");
scanf("%d%d", &x, &y);
// The last two arguments are passed
// by giving addresses of memory locations
result = findGreaterSmaller(x, y);
printf("\nThe greater number is %d and the"
"smaller number is %d",
result.greater, result.smaller);
return 0;
}
输出
Enter two numbers:
5 8
The greater number is 8 and the smaller number is 5
3、使用数组返回多个值(仅当返回的项类型相同时有效):当数组作为参数传递时,它的基址将传递给函数,因此无论对数组的副本做了什么更改,它都将在原始数组中更改。下面是使用数组返回多个值的程序,即在arr[0]存储较大的值,在arr[1]存储较小的值。
// Modified program using array
#include <iostream>
using namespace std;
// Store the greater element at 0th index
void findGreaterSmaller(int a, int b, int arr[])
{
// Store the greater element at
// 0th index of the array
if (a > b) {
arr[0] = a;
arr[1] = b;
}
else {
arr[0] = b;
arr[1] = a;
}
}
// Driver code
int main()
{
int x, y;
int arr[2];
cout << "Enter two numbers: \n";
cin >> x >> y;
findGreaterSmaller(x, y, arr);
cout << "\nThe greater number is " << arr[0] << " and the "
"smaller number is " << arr[1];
return 0;
}
// This code is contributed by sarajadhav12052009
Output:
Enter two numbers:
5 8
The greater number is 8 and the smaller number is 5
c++专用方法
使用引用返回多个值:我们在c++中使用引用来存储返回值。
// Modified program using References in C++
#include <stdio.h>
void compare(int a, int b, int &add_great, int &add_small)
{
if (a > b) {
add_great = a;
add_small = b;
}
else {
add_great = b;
add_small = a;
}
}
// Driver code
int main()
{
int great, small, x, y;
printf("Enter two numbers: \n");
scanf("%d%d", &x, &y);
// The last two arguments are passed
// by giving addresses of memory locations
compare(x, y, great, small);
printf("\nThe greater number is %d and the"
"smaller number is %d",
great, small);
return 0;
}
使用Class和Object返回多个值:这个想法类似于结构。我们创建一个具有两个整数变量的类,并将较大和较小的值存储到这些变量中,然后使用该结构的值。
// Modified program using class
#include <stdio.h>
class GreaterSmaller {
public:
int greater, smaller;
};
GreaterSmaller findGreaterSmaller(int a, int b)
{
GreaterSmaller s;
if (a > b) {
s.greater = a;
s.smaller = b;
}
else {
s.greater = b;
s.smaller = a;
}
return s;
}
// Driver code
int main()
{
int x, y;
GreaterSmaller result;
printf("Enter two numbers: \n");
scanf("%d%d", &x, &y);
// The last two arguments are passed
// by giving addresses of memory locations
result = findGreaterSmaller(x, y);
printf("\nThe greater number is %d and the"
"smaller number is %d",
result.greater, result.smaller);
return 0;
}