Main Content

Number of Called Functions

Number of distinct functions called within the body of a function

Description

Note

Use Bug Finder instead of Code Prover for computing code metrics. Support for computing code metrics in Code Prover will be removed in a future release. See Version History.

This metric specifies the number of distinct functions that are called by a function within its body.

This metric includes:

  • Calls in unreachable code.

  • Calls to standard library functions.

  • Calls to user defined constructors and destructors.

The metric excludes:

  • Calls through a function pointer.

  • Calls to virtual functions.

  • Calls to assert. Polyspace® considers assert as a macro and not a function.

  • Inlined functions. Compilers might inline certain function calls, such as the default or implicit constructors and destructors.

The recommended upper limit for this metric is 7. For more self-contained code, try to enforce an upper limit on this metric.

To enforce limits on metrics, see Compute Code Complexity Metrics Using Polyspace. To enforce violation of code metrics limits as Bug Finder defects, use the Software Complexity checkers. See Reduce Software Complexity by Using Polyspace Checkers

Examples

expand all

int func1(void);
int func2(void);

int foo() {
    return (func1() + func1()*func1() + 2*func2());
}

In this example, the number of called functions in foo is 2. The called functions are func1 and func2.

#include <stdio.h>
int fibonacci(int);

void main() {
 int count;
 printf("How many numbers ?");
 scanf("%d",&count);
 fibonacci(count);
}

int fibonacci(int num)
{
   if ( num == 0 )
      return 0;
   else if ( num == 1 )
      return 1;
   else
      return ( fibonacci(num-1) + fibonacci(num-2) );
} 

In this example, the number of called functions in fibonacci is 1. The called function is fibonacci itself.

#include<iostream>
class A{
    public:
	A(){
		std::cout<<"Create A\n";
	}
	~A() = default;
	A(const A&)=default;
	A(A&&) = default;
	virtual void bar(){ std::cout<<"A";}
};
class B: public A{
    public:
	B() = default;
	 void bar() override {std::cout<<"B";}
};

void func(A& a){
	a.bar();
}

int main(){
	A obj;
	A obj2 = obj;
	B objB;
	func(obj);
	return 0;
}

In this example, the number of called function in main is three:

  1. The constructor of class A. This user defined constructor counts as a function call.

  2. The constructor of class B. Because the constructor of the base class A is user-defined, the constructor of B counts as a function call even though B::B() is declared as =default.

  3. The function func.

The class A uses the default or implicit copy constructor. The call to the copy constructor in A obj2 = obj; does not count as a function call.

Metric Information

Group: Function
Acronym: CALLS
HIS Metric: Yes

Version History

expand all