了解函数返回值 [英] Understanding function return values

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

问题描述

我正在尝试理解语言资源手册(第10.3.1节)中的SystemVerilogfunction返回值,但我在理解以下部分时遇到了困难。有人能帮我翻译一下吗?我试着在不同的网站上寻找,但信息并不那么深入。

在SystemVerilog中,函数返回可以是结构或联合。在这种情况下,在函数中使用且以函数名开头的分层名称被解释为返回值的成员。如果函数名在函数外部使用,则该名称指示整个函数的作用域。如果函数名在层次结构名称中使用,则它还指示整个函数的作用域。

a = b + myfunc1(c, d); //call myfunc1 (defined above) as an expression
myprint(a); //call myprint (defined below) as a statement
function void myprint (int a);
...
endfunction

推荐答案

您可以使用两种不同的方法从函数返回值。例如:

function int myfunc1(int c, d);
  myfunc1 = c+d;
endfunction

function int myfunc1(int c, d);
  return c+d;
endfunction

因此,当函数声明为结构或联合类型时,以函数名开头的分层名称也表示返回值的变量。 但旧的LRM描述现在并不正确和准确,因为分层名称现在也可以是函数作用域,而不是返回值。例如,

typedef struct { int c, d, n; } ST;

function ST myfunc1(int c, d);
  static int x = 1;
  myfunc1.c = c;          // myfunc1 refers to the return structure
  myfunc1.d = d;          // myfunc1 refers to the return structure
  myfunc1.n = c + d +     // myfunc1 refers to the return structure
              myfunc1.x;  // myfunc1 refers to function scope
endfunction

使用包含函数名的分层名称的另一个有趣示例。

typedef struct { int c, d; } ST ;

module top;
  function ST myfunc1(int c,d);
    top.myfunc1.c = c;
    myfunc1.c = 1;
    myfunc1.d = d;
  endfunction

  ST x;
  initial begin
    x = myfunc1(3,2);
    #1 $display("%d %d %d", x.c, x.d, top.myfunc1.c);
  end
endmodule
x = myfunc1(3,2)的函数调用为myfunc1构造调用框架并传递求值。myfunc1top.myfunc1的作用域不同。以myfunc1开头的层次化名称表示函数的当前调用框架,top.myfunc1表示模块top内部声明的函数作用域。因此消息将是1 2 3

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

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