Main Content

Number of Calling Functions

Number of distinct callers of a function

Description

This metric measures the number of distinct callers of a function.

In C++ , Polyspace® does not calculate this metric for virtual functions and compiler generated implicit functions, such as default constructors and destructors. The metric is calculated for user-defined constructors and destructors. In a class hierarchy, if a base class has user-defined constructors, Polyspace counts this metric for corresponding constructors of the derived classes.

The recommended upper limit for this metric is 5. 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.

Computation Details

Note that the metric:

  • Takes into account direct callers only.

  • Does not consider calls through a function pointer.

  • Takes into account all function calls, including ones in unreachable code.

    However, if a caller calls a function more than once, the caller is counted only once when this metric is calculated.

Examples

expand all

#include <stdio.h>


    int getVal() {
    int myVal;
    printf("Enter a value:");
    scanf("%d", &myVal);
    return myVal;
}

int func() {
    int val=getVal();
    if(val<0)
        return 0;
    else
        return val;
}

int func2() {
    int val=getVal();
    while(val<0)
        val=getVal();
    return val;
}

In this example, the number of calling functions for getVal is 2. The calling functions are func and func2.

#include <stdio.h>




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

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

In this example, the number of calling functions for fibonacci is 2. The calling functions are main and 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 calling functions for A::A is two. A::A is called once to create obj and again to create objB. Similarly, the number of calling function for B:: is one.

  • Because both A::bar and B::bar are virtual functions, Polyspace does not calculate their number of calling functions.

  • The number of calling function for func is one.

Metric Information

Group: Function
Acronym: CALLING
HIS Metric: Yes