Main Content

Unused parameter

Function prototype has parameters not read or written in function body

Description

This defect occurs when a function parameter is neither read nor written in the function body. The checker does not flag unused parameters in functions with empty bodies.

Risk

Unused parameters can indicate that the code is possibly incomplete. The parameter is possibly intended for an operation that you forgot to code.

If the copied objects are large, redundant copies can slow down performance.

Fix

Determine if you intend to use the parameters. Otherwise, remove parameters that you do not use in the function body.

You can intentionally have unused parameters. For instance, you have parameters that you intend to use later when you add enhancements to the function. Add a code comment indicating your intention for later use. The code comment helps you or a code reviewer understand why your function has unused parameters.

Alternatively, add a statement such as (void)var; in the function body. var is the unused parameter. You can define a macro that expands to this statement and add the macro to the function body.

Examples

expand all

void func(int* xptr, int* yptr, int flag) {
    if(flag==1) {
        *xptr=0;
    }
    else {
        *xptr=1;
    }
}

int main() {
    int x,y;
    func(&x,&y,1);
    return 0;
}

In this example, the parameter yptr is not used in the body of func.

Correction — Use Parameter

One possible correction is to check if you intended to use the parameter. Fix your code if you intended to use the parameter.

void func(int* xptr, int* yptr, int flag) {
    if(flag==1) {
        *xptr=0;
        *yptr=1;
    }
    else {
        *xptr=1;
        *yptr=0;
    }
}

int main() {
    int x,y;
    func(&x,&y,1);
    return 0;
}
Correction — Explicitly Indicate Unused Parameter

Another possible correction is to explicitly indicate that you are aware of the unused parameter.

#define UNUSED(x) (void)x

void func(int* xptr, int* yptr, int flag) {
    UNUSED(yptr);
    if(flag==1) {
        *xptr=0;
    }
    else {
        *xptr=1;
    }
}

int main() {
    int x,y;
    func(&x,&y,1);
    return 0;
}

Result Information

Group: Good practice
Language: C | C++
Default: Off
Command-Line Syntax: UNUSED_PARAMETER
Impact: Low

Version History

Introduced in R2015b