Pascal是否支持将参数传递给函数? [英] Does Pascal support passing parameters to functions?

查看:109
本文介绍了Pascal是否支持将参数传递给函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Pascal的新手,我正在尝试编写一个简单的程序,但是在函数之间传递值时遇到了麻烦.这只是我所拥有的一小部分:

I'm new to Pascal and I am trying to write a simple program, but an having trouble passing values between functions. This is a small piece of what I have:

program numberConverter;

const
    maxValue = 4999;
    minValue = 1;

var num: integer;

function convertNumeral(number: integer):string;
var j: integer;
begin
if ((number < minValue) OR (number > maxValue)) then
    begin
    writeln(number);
    writeln('The number you enter must be between 1 and 4999. Please try again:');
    read(j);
    convertNumeral := convertNumeral(j);
    end
else 
 if (number >= 1000) then
convertNumeral := 'M' + convertNumeral(number -1000)
{more code here, left it out for space}
end;

begin
    writeln;
    writeln('Enter an integer between 1 and 4999 to be converted:');
    read(num);
    writeln;
    writeln(num);
    writeln(convertNumeral(num));
end.

我的问题是来自writeln(converNumeral(num))的值(主要是'num')没有传递到convertNumeral函数,并且想知道Pascal是否这样做.我知道它是因为我没有声明number为变量,但是当我这样做的时候,我收到了一个编译错误,它无法完成第二个if语句.谢谢您的宝贵时间.

My problem is that the value from the writeln(converNumeral(num)), mainly 'num', does not get passed to the convertNumeral function and was wondering if Pascal even does this. I figure its because I haven't declared number to be a variable, but when I do I get a compile error that it can't complete the second if statement. Thanks for your time.

推荐答案

是的,值肯定会传递给函数.我保证num确实会传递给convertNumeral.在该函数中,number获取num中的任何值.也许您观察程序行为的方式有问题.

Yes, values definitely get passed to functions. I promise that num really does get passed to convertNumeral. Within that function, number acquires whatever value resides in num. Perhaps there's a problem with how you're observing the behavior of your program.

您对number所做的更改(如果有)将不会反映在num中.参数是通过值传递的.,因此number存储num中存储的值的副本;它们是两个不同的变量.如果需要的话,可以使用var通过引用传递参数.

Changes you make to number, if any, will not be reflected in num. The parameter was passed by value, so number stores a copy of the value stored in num; they're two distinct variables. You can use var to pass parameters by reference, if that's what you want.

convertNumeral的每个递归调用都会得到number new 实例,因此,一旦函数返回到调用者,对number所做的更改(如果有)将不会出现.每个调用都有自己的numberj版本.

Each recursive call to convertNumeral gets a new instance of number, so changes made to number, if any, will not appear once the function returns to the caller. Each call gets its own versions of number and j.

这篇关于Pascal是否支持将参数传递给函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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