scitools.com

← Metric catalog

Functions is one of the source-code metrics Understand computes across 17+ languages — browse them in the GUI, export and track them, or script them with the Python API.

Functions

API IDCountDeclFunction
TargetsArchitectures Files Project
LanguagesAssembly C# C++ Java Python Rust Web
CategoriesContents
CostLookup
TagsProject Summary

Number of functions.

Combines free functions and member functions (methods) into one count, and only counts ones that are actually defined, not just declared.

For example:

class SayHello {
public:
  SayHello() {}
  void printHello();
};

int i,j;
void SayHello::printHello() {
  switch (i) {
    case 0:
      cout << "Hello World" << endl;
    case 1:
      cout << "HELLO WORLD!" << endl;
    default: // a comment here
      for (int m = 0; m < j; m++);
      cout << "hello world" << endl;
  }
#ifdef A_VERY_NICE_VARIABLE

  cout << "Inactive Line" << endl; // Inactive
#endif

}

void cyclomaticDemo() {
  bool a = true, b = true, c = true;

  if (a || (b && c)) {
    while (a ? b : c) {
      for (int i = 0; i < 10; i++) {
        switch(i) {
          case 1:
          case 2:
            cout<<i<<endl;
            break;
          case 5:
            break;
          default:
            cout <<i<<endl;
        }
      }
    }
  } else {
    try {
      do {
        cout << a << b << c << endl;
      } while(a);
    }
    catch(...){

    }
  }
}


int func();
int main() {
  for (int i = 0;
       i < 10;
       i++)
    ;

  int j = func();
  int k = 0;
  int l = 1;

  int m
    = func();
  int n;
  n = 1;

#ifdef A_VERY_NICE_VARIABLE

  int j = 0;
  cout << j << endl;
#endif

  return 0;
}

sample.cpp defines 4 functions: the SayHello() constructor and SayHello::printHello() (methods), plus the free functions cyclomaticDemo() and main() — so CountDeclFunction = 4. func() is only declared, never defined in this file, so it doesn't count.

See Classes for the equivalent count of class-like declarations, and Subprograms for the same concept in Ada, Basic, Fortran, Jovial, and Pascal, which use that term instead.

Targets By Language: