从函数返回值 [英] Returning values from a function

查看:79
本文介绍了从函数返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的函数来初始化3个变量到pesudo

随机数。


我有一个函数如下


int randomise(int x,int y,intz)

{

srand((unsigned)time(NULL));

x = rand();

y = rand();

z = rand();


}

我用这个函数调用


随机(a,b,c);


但是a,b ,c不包含随机整数,我理解这个

是我函数返回值的问题,

我试过了

返回x,y,z;

但这也不起作用。


有谁可以指出我的问题在哪里?

解决方案

Materialized写道:

我正在写一个简单的函数来初始化3个变量到pesudo
随机数。

我有一个如下功能

int ran domise(int x,int y,intz)
{/ / srand((unsigned)time(NULL));
x = rand();
y = rand();
z = rand();

}
我把这个函数称为

随机(a,b,c);

但是a,b,c不包含随机整数,我明白
这是我函数返回值的问题,
我试过
返回x,y ,z;
但这也不起作用。

任何人都可以指出我的问题在哪里吗?




Ehem。第一个是你尝试使用C ++进行编程而没有一本好的

教科书。 :-(


首先,你的程序没有返回值。你没有返回

任何东西。没有回复声明。


第二种:你希望如何在一个中返回3个整数?


第三个:当你编写函数定义(签名)时,它需要

参数值,所以x,y和z(你明确输入错误)是ab和c的

*副本。写入它们将写入复制。


您可以通过引用获取参数,然后您就可以更改

。*如果*您使用C ++工作。如果您这样做在C工作,你需要带上

指针给他们。


-

WW又名阿提拉


Materialized写道:


我正在写一个简单的函数来初始化3个变量到pesudo
随机数。

我有一个如下函数

int randomise(int x,int y,intz )
{/ / srand((无符号)时间(NULL));
x = rand();
y = rand();
z = rand() ;

}
我用随机函数(a,b,c)来调用这个函数;

然而a,b, c不包含随机整数,我明白这是我的函数返回值的问题,
我试过
返回x,y,z;
但是这个也没有用。

任何人都可以指出我的问题在哪里?




这些是comp.lang.c中的问题4.8和20.1

常见问题(FAQ)列表

http://www.eskimo.com/~scs/C-faq/top.html


-
Er*********@sun.com


Materialized写道:

我正在编写一个简单的函数来初始化3个变量到pesudo
随机数。

int randomise(int x,int y,intz)
{/ / srand((unsigned)time(NULL));
x = rand();
y = rand();
z = rand();

}
我用<调用此函数随机(a,b,c);

然而a,b,c不包含随机整数,我明白这个回归是个问题我的功能的价值,
我试过
返回x,y,z;
但这也不起作用。

任何人都可以指出我的问题在哪里?




#include< stdio.h>

#include< stdlib.h>

#include < time.h>


void randomise(int * x,int * y,int * z)

{

srand((unsigned)time(NULL));

* x = rand();

* y = rand();

* z = rand();

}


int main(无效)

{

int a,b,c;

随机(& a,& b,& c);

printf(" %d%d%d \ n",a,b,c);

返回0;

}


-

Martin Ambuhl


I am writing a simple function to initialise 3 variables to pesudo
random numbers.

I have a function which is as follows

int randomise( int x, int y, intz)
{
srand((unsigned)time(NULL));
x = rand();
y = rand();
z = rand();

}
And I call this function with

randomise(a,b,c);

However a,b,c do not contain the randomised integers, I understand this
is a problem with the return value of my function,
I have tried
return x,y,z;
But this doesnt work either.

Can anyone point out where my problem is?

解决方案

Materialised wrote:

I am writing a simple function to initialise 3 variables to pesudo
random numbers.

I have a function which is as follows

int randomise( int x, int y, intz)
{
srand((unsigned)time(NULL));
x = rand();
y = rand();
z = rand();

}
And I call this function with

randomise(a,b,c);

However a,b,c do not contain the randomised integers, I understand
this is a problem with the return value of my function,
I have tried
return x,y,z;
But this doesnt work either.

Can anyone point out where my problem is?



Ehem. The first one is that you try to program in C++ without a good
textbook. :-(

First of all, your program has not return value. You do not return
anything. There is no return statement.

The second: how do you expect to return 3 integers in one?

The third: as you wrote the function definition (signature) it takes the
arguments by value, so x,y and z (which you have clearly typed wrong) is a
*copy* of a b and c. Writing into them will write into the copy.

You can take the arguments by reference, then you will be able to change
them. *If* you work in C++. If you do work in C, you will need to take
pointers to them.

--
WW aka Attila


Materialised wrote:


I am writing a simple function to initialise 3 variables to pesudo
random numbers.

I have a function which is as follows

int randomise( int x, int y, intz)
{
srand((unsigned)time(NULL));
x = rand();
y = rand();
z = rand();

}
And I call this function with

randomise(a,b,c);

However a,b,c do not contain the randomised integers, I understand this
is a problem with the return value of my function,
I have tried
return x,y,z;
But this doesnt work either.

Can anyone point out where my problem is?



These are Questions 4.8 and 20.1 in the comp.lang.c
Frequently Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

--
Er*********@sun.com


Materialised wrote:

I am writing a simple function to initialise 3 variables to pesudo
random numbers.

I have a function which is as follows

int randomise( int x, int y, intz)
{
srand((unsigned)time(NULL));
x = rand();
y = rand();
z = rand();

}
And I call this function with

randomise(a,b,c);

However a,b,c do not contain the randomised integers, I understand this
is a problem with the return value of my function,
I have tried
return x,y,z;
But this doesnt work either.

Can anyone point out where my problem is?



#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void randomise(int *x, int *y, int *z)
{
srand((unsigned) time(NULL));
*x = rand();
*y = rand();
*z = rand();
}

int main(void)
{
int a, b, c;
randomise(&a, &b, &c);
printf("%d %d %d\n", a, b, c);
return 0;
}

--
Martin Ambuhl


这篇关于从函数返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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