Main Content

Static uncalled function

Function with static scope not called in file

Description

This defect occurs when a static function is not called in the same file where it is defined.

Risk

Uncalled functions often result from legacy code and cause unnecessary maintenance.

Fix

If the function is not meant to be called, remove the function. If the function is meant for debugging purposes only, wrap the function definition in a debug macro.

See examples of fixes below.

If you do not want to fix the issue, add comments to your result or code to avoid another review. See:

Examples

expand all

Save the following code in the file Initialize_Value.c

#include <stdlib.h>
#include <stdio.h>

static int Initialize(void) 
/* Defect: Function not called */
  {
   int input;
   printf("Enter an integer:");
   scanf("%d",&input);
   return(input);
  }
  
 void main()
  {
   int num;

   num=0;

   printf("The value of num is %d",num);
  }

The static function Initialize is not called in the file Initialize_Value.c.

Correction — Call Function at Least Once

One possible correction is to call Initialize at least once in the file Initialize_Value.c.

#include <stdlib.h>
#include <stdio.h>

static int Initialize(void)
  {
   int input;
   printf("Enter an integer:");
   scanf("%d",&input);
   return(input);
  }

 void main()
  {
   int num;

   /* Fix: Call static function Initialize */
   num=Initialize();
    
   printf("The value of num is %d",num);
  }

Result Information

Group: Data flow
Language: C | C++
Default: Off
Command-Line Syntax: UNCALLED_FUNC
Impact: Low

Version History

Introduced in R2013b