Home > Back-end >  Forward declare a function/task
Forward declare a function/task

Time:07-20

I browsed through the internet, and I've only saw people doing forward declarations on class using the typedef keyword. But, I was wondering how'd I do that with functions/tasks?

I wanted to put the main function above the definitions of other functions/tasks to ease the reader when one is previewing it. In C , forward declaration for a function looks something like this:

//forward declaration of sub2
int sub2(int A, int B);

int main(){
    cout << "Difference: " << sub2(25, 10);
    return 0;
}

int sub2(int A, int B) //Defining sub2 here{
    return A - B;
}

For SystemVerilog, will it be something like this?

function somefunction();

virtual task body();
    somefunction();
endtask: body

function somefunction();
    // do something here.
endfunction: somefunction

Should I use typedef for forward declarations with functions/tasks?

CodePudding user response:

The typedef keyword can not be used for forward declarations of functions or tasks; it is for data types.

For functions/tasks declared in classes, the extern keyword can be used. Refer to IEEE Std 1800-2017, section 8.24 Out-of-block declarations.

class foo;
    extern task body();
    extern function somefunction();
endclass

virtual task foo::body();
    somefunction();
endtask

function foo::somefunction();
    // do something here.
endfunction

Example 1

Example 2

CodePudding user response:

Function declaration order doesn't matter, like in C. You can call somefunction in body before declaring it.

You don't need to do any kind of declarations.

  • Related