[SystemC]SystemC Functions

                                     SystemC Functions

       function is group of statements to perform the specific task.

一、Syntax:

type function_name (arguments) {
  function_body;
}
  • where,

       type – functions will return an value, type specifies the return type

       function_name – name of the function

       arguments – arguments to the function

       function_body – body of the function, which may contain declarations, assignments, expressions etc.

       function can return a specific value by specifying return in function body, otherwise the result of last expression will be returned.

二、Example

#include "systemc.h"
 
//function add, to add two integer numbers
int add (int a,int b)
{
 return a+b;
}
 
int sc_main (int argc, char* argv[]) {
  //declaration
  int x,y,z;
   
  //initialization
  x=10;
  y=20;
   
  //function calling
  z =  add(x,y);
  cout <<" x+y = "<<z<<endl;
     
  // Terminate simulation
  return 0;
}
  • SIMULATOR OUTPUT:

        x+y = 30

三、void functions 

       Generally functions will return an value, where as void functions will not return any value.by specifying type as void function can be declared as void function.

#include "systemc.h"
 
//function add, to add two integer numbers
void display (int a,int b)
{
 cout <<" recived a = "<<a<<" b = "<<b<<endl;
}
 
int sc_main (int argc, char* argv[]) {
   
  //function calling
  display(10,20);
     
  // Terminate simulation
  return 0;
}
  • SIMULATOR OUTPUT:

       recived a = 10 b = 20

四、function call as expression 

#include "systemc.h"
 
//function add, to add two integer numbers
int add (int a,int b)
{
 return a+b;
}
 
int sc_main (int argc, char* argv[]) {
  //declaration
  int x,y,z;
   
  //initialization
  x=10;
  y=20;
   
  //function call in expression
  z =  10 + add(x,y) + 20;
  cout <<" Value of z = "<<z<<endl;
     
  // Terminate simulation
  return 0;
}
  • SIMULATOR OUTPUT:

        Value of z = 60

五、functions declaration 

#include "systemc.h"
 
//function display_1
void display_1 ()
{
  display_2;
}
 
//function display_1
void display_2 ()
{
  display_1;
}
 
int sc_main (int argc, char* argv[]) {
     
  // Terminate simulation
  return 0;
}
  • SIMULATOR OUTPUT:

       testbench.cpp: In function ‘void display_1()’:testbench.cpp: error: ‘display_2’ was not declared in this scope Exit code expected: 0, received: 1

       The above example is locked situation where as display_1 is calling display_2 and display_2 is calling display_1, this leads to an compilation error.

       This problem can be overcome by declaring the functions and writing the definitions separately.

       Arguments  and type must match in declaration and definition.

#include "systemc.h"
 
void display_1();
void display_2();
 
//function display_1
void display_1 ()
{
  display_2();
}
 
//function display_1
void display_2 ()
{
  display_1();
}
 
int sc_main (int argc, char* argv[]) {
  cout <<"Inside Main"<<endl;
  // Terminate simulation
  return 0;
}
  • SIMULATOR OUTPUT:

       Inside Main

发布了185 篇原创文章 · 获赞 118 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/gsjthxy/article/details/105243157