关于退货声明的小问题...... [英] Small question about return statements...

查看:73
本文介绍了关于退货声明的小问题......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int divide(int a,int b)

{

int r;

r = a / b;

return(r);

}

int main()

{

int result = divide (20,4);

cout<<结果

返回0;

}

--------------


我理解使用return语句,如上面的

示例所示。但是,我想知道什么是

函数返回多个值的正确方法,然后将这些值存储在它们自己的独立变量中。例如,如果上面的

函数返回两个不同的结果,我如何通过return语句将它们分配给它们自己的变量?


我想我可以通过在主要的
函数中声明变量然后通过引用函数传递它们来实现这一点,所以它们是

可以在内部更改(这可以在不使用返回

函数的情况下工作),但有些东西告诉我这种方法并不完全是

kosher?


你们怎么想,这是怎么做的?

解决方案

SpiralCorp写道:


int divide(int a,int b)

{

int r;

r = a / b;

返回(r);

}

int main()

{

int result = divide(20,4);

cout<<结果

返回0;

}

--------------


我理解使用return语句,如上面的

示例所示。但是,我想知道什么是

函数返回多个值的正确方法,然后将这些值存储在它们自己的独立变量中。例如,如果上面的

函数返回两个不同的结果,我如何通过return语句将它们分配给它们自己的变量?


我想我可以通过在主要的
函数中声明变量然后通过引用函数传递它们来实现这一点,所以它们是

可以在内部更改(这可以在不使用返回

函数的情况下工作),但有些东西告诉我这种方法并不完全是

kosher?


你们怎么想,这是怎么做的?



它是犹太教的,是一种很好的方式从中返回多个值一个

的功能。另一种方法是定义一个包含多个

值的结构,并使用该结构作为函数的返回。


-

Scott McPhillips [VC ++ MVP]





1月27日下午12:25, Scott McPhillips [MVP]" < org-dot-mvps-at-

scottmcpwrote:


SpiralCorp写道:


int divide(int a,int b)

{

int r;

r = a / b;

返回(r);

}

int main()

{

int result =除(20,4);

cout<<结果

返回0;

}

--------------


我理解使用return语句,如上面的

示例所示。但是,我想知道什么是

函数返回多个值的正确方法,然后将这些值存储在它们自己的独立变量中。例如,如果上面的

函数返回两个不同的结果,我如何通过return语句将它们分配给它们自己的变量?


我想我可以通过在main

函数中声明变量然后通过引用传递它来实现这一点功能所以他们可以在内部更改
(这样可以在不使用返回功能的情况下工作),但有些东西告诉我这种方法并不完全是

kosher?


你们怎么想,正确的做法是什么?它是犹太教的,是从$ b返回多个值的好方法$ b



函数。另一种方法是定义一个包含多个

值的结构,并使用该结构作为函数的返回。


-

Scott McPhillips [VC ++ MVP]



很高兴知道。我会继续使用参考方法,因为我不是那么熟悉结构,但我会记下我的时间。谢谢

的帮助。


---

SpiralCorp

< br>

2007年1月27日07:47:40 -0800,SpiralCorp < sp *************** @ gmail.com>

写道:


> ; int divide(int a,int b)
$

int r;

r = a / b;

return(r );
}
int main()

int result = divide(20,4);

cout<< ;结果

返回0;
}
--------------

我理解使用返回声明在上面的例子中举例说明。但是,我想知道
函数返回多个值并随后将这些值存储在它们自己的独立变量中的正确方法是什么。例如,如果上面的
函数返回两个不同的结果,我如何通过return语句将它们分配给它们自己的变量?

我想我可以通过在主要函数中声明变量然后通过引用函数传递它们来实现这一点,这样它们就可以在内部进行更改(这样可以在不使用返回函数的情况下工作) ),但有些东西告诉我这种方法并不完全是犹太人的?

你们怎么想,这是怎么做的?



它是犹太洁食,它传统上已经完成了,虽然它并不漂亮。


然后是返回一个结构。其中返回的值实际上是一个

数据结构:


struct Retval

{

Retval (int a,int b):a(a),b(b){}

int a;

int b;

} ;


Retval fn()

{

返回Retval(1,2);

}


int main()

{

Retval retval = fn();

int my_a = retval.a;

int my_b = retval.b;

}


他们既丑陋又笨拙。有关更优雅的解决方案,请参阅boost.tuple。它可以让你用这样的代码编写代码:


#include" boost / tuple.hpp"


使用boost :: tuples :: tuple;

使用boost :: tuples :: make_tuple;

使用boost :: tuples :: tie;


元组< int,intfn()

{

返回make_tuple(1,2);

}


int main()

{

int my_a;

int my_b;

tie(my_a,my_b)= fn(); //将值分配给my_a和my_b

}


int divide (int a, int b)
{
int r;
r=a/b;
return (r);
}
int main ()
{
int result = divide (20,4);
cout << result
return 0;
}
--------------

I understand the use of the return statement as exemplified in the
example above. However, I was wondering what was the proper way for a
function to return more than one value and subsequently store those
values in their own independent variables. For instance, if the above
function were to return two different results, how would I assign them
each to their own variable by means of the return statement?

I figured I could achieve this by declaring the variables in the main
function and then passing them by reference to the function so they
could be changed within (This would work without the use of the return
function ofcourse), but something tells me this method is not exactly
kosher?

What do you guys think, whats the proper way of doing it?

解决方案

SpiralCorp wrote:

int divide (int a, int b)
{
int r;
r=a/b;
return (r);
}
int main ()
{
int result = divide (20,4);
cout << result
return 0;
}
--------------

I understand the use of the return statement as exemplified in the
example above. However, I was wondering what was the proper way for a
function to return more than one value and subsequently store those
values in their own independent variables. For instance, if the above
function were to return two different results, how would I assign them
each to their own variable by means of the return statement?

I figured I could achieve this by declaring the variables in the main
function and then passing them by reference to the function so they
could be changed within (This would work without the use of the return
function ofcourse), but something tells me this method is not exactly
kosher?

What do you guys think, whats the proper way of doing it?

It is kosher and is a fine way to return multiple values from a
function. Another way is to define a struct that contains multiple
values, and use the struct as the function''s return.

--
Scott McPhillips [VC++ MVP]




On Jan 27, 12:25 pm, "Scott McPhillips [MVP]" <org-dot-mvps-at-
scottmcpwrote:

SpiralCorp wrote:

int divide (int a, int b)
{
int r;
r=a/b;
return (r);
}
int main ()
{
int result = divide (20,4);
cout << result
return 0;
}
--------------

I understand the use of the return statement as exemplified in the
example above. However, I was wondering what was the proper way for a
function to return more than one value and subsequently store those
values in their own independent variables. For instance, if the above
function were to return two different results, how would I assign them
each to their own variable by means of the return statement?

I figured I could achieve this by declaring the variables in the main
function and then passing them by reference to the function so they
could be changed within (This would work without the use of the return
function ofcourse), but something tells me this method is not exactly
kosher?

What do you guys think, whats the proper way of doing it?It is kosher and is a fine way to return multiple values from a

function. Another way is to define a struct that contains multiple
values, and use the struct as the function''s return.

--
Scott McPhillips [VC++ MVP]

Good to know. I''ll keep using the reference method as I''m not so
familiar with structs yet, but I''ll make a note for when I am. Thanks
for the help.

---
SpiralCorp


On 27 Jan 2007 07:47:40 -0800, "SpiralCorp" <sp***************@gmail.com>
wrote:

>int divide (int a, int b)
{
int r;
r=a/b;
return (r);
}
int main ()
{
int result = divide (20,4);
cout << result
return 0;
}
--------------

I understand the use of the return statement as exemplified in the
example above. However, I was wondering what was the proper way for a
function to return more than one value and subsequently store those
values in their own independent variables. For instance, if the above
function were to return two different results, how would I assign them
each to their own variable by means of the return statement?

I figured I could achieve this by declaring the variables in the main
function and then passing them by reference to the function so they
could be changed within (This would work without the use of the return
function ofcourse), but something tells me this method is not exactly
kosher?

What do you guys think, whats the proper way of doing it?

It''s kosher, and it''s traditionally done, although it''s not pretty.

Then there is the "return a struct" where the returned value is actually a
data structure:

struct Retval
{
Retval(int a, int b): a(a), b(b) {}
int a;
int b;
};

Retval fn()
{
return Retval(1, 2);
}

int main()
{
Retval retval = fn();
int my_a = retval.a;
int my_b = retval.b;
}

They''re both ugly and kludgy. For a more elegant solution, see boost.tuple. It
lets you write code like this:

#include "boost/tuple.hpp"

using boost::tuples::tuple;
using boost::tuples::make_tuple;
using boost::tuples::tie;

tuple<int, intfn()
{
return make_tuple(1, 2);
}

int main()
{
int my_a;
int my_b;
tie(my_a, my_b) = fn(); // Assigns values to my_a and my_b
}


这篇关于关于退货声明的小问题......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆