c-plus-plus
  1. c-plus-plus-storage-classes

C++ Functions Storage Classes

In C++, storage classes define the scope, visibility, and life cycle of variables and functions. C++ supports four storage classes for functions: auto, static, register, and extern. In this article, we will discuss these storage classes in detail.

Syntax

The syntax of storage classes for functions is given below:

<storage-class> <return-type> <function-name>(<parameter-list>) {
   // function body
   return <expression>;
}

Example

Let's see an example to understand the use of different storage classes for functions.

#include <iostream> 
using namespace std; 

auto add(int a, int b) -> int { 
   auto c = a + b;
   return c; 
} 

static auto subtract(int a, int b) -> int { 
   auto c = a - b;
   return c; 
} 

register auto multiply(int a, int b) -> int { 
   auto c = a * b;
   return c; 
} 

extern auto divide(int a, int b) -> double;

int main() { 
   int x = 10, y = 5;

   cout << "Addition of " << x << " and " << y << " is " << add(x, y) << endl; 
   cout << "Subtraction of " << x << " and " << y << " is " << subtract(x, y) << endl; 
   cout << "Multiplication of " << x << " and " << y << " is " << multiply(x, y) << endl; 
   cout << "Division of " << x << " and " << y << " is " << divide(x, y) << endl; 

   return 0; 
} 

auto divide(int a, int b) -> double { 
   auto c = static_cast<double>(a) / b;
   return c; 
}

Output

Addition of 10 and 5 is 15
Subtraction of 10 and 5 is 5
Multiplication of 10 and 5 is 50
Division of 10 and 5 is 2

Explanation

In the above example, we have defined four functions: add(), subtract(), multiply(), and divide(), with different storage classes for each function.

Auto

The auto storage class is used to specify that the variable is local to the block it is declared in. Therefore, local variables defined inside the function are declared with the auto storage class by default. In the above example, the add() function has a return type of int and uses the auto storage class.

Static

The static storage class is used to specify that the variable is local to the function but keeps its value between function calls. In the above example, the subtract() function has a return type of int and uses the static storage class.

Register

The register storage class is used to specify that the variable should be stored in a CPU register for faster access. In the above example, the multiply() function has a return type of int and uses the register storage class.

Extern

The extern storage class is used to declare a variable or function that is defined in another module or file. In the above example, we have declared the divide() function with the extern storage class in the beginning and defined it later after the main() function.

Use

Here are some important use cases for each storage class:

Auto

  • It is used to declare local variables within the function.
  • This storage class is useful in recursive algorithms where you need to declare variables repeatedly.

Static

  • It is used to keep the value of a variable between multiple function calls.
  • This storage class is useful in maintaining a counter or a state of a function.

Register

  • It is used to define a variable that should be stored in the CPU's register for faster access.
  • This storage class is useful when you need to define variables that are frequently accessed by a function.

Extern

  • It is used to declare a variable or a function that is defined in another module or file.
  • This storage class is useful when you have to work with a large project having multiple files or modules.

Important Points

Here are some important points to keep in mind while using storage classes for functions:

  • The default storage class for a function is auto.
  • The static storage class is used to specify that the variable should keep its value between function calls.
  • The register storage class is used to specify that the variable should be stored in the CPU register for faster access.
  • The extern storage class is used to declare a variable or function that is defined in another module or file.

Summary

In this article, we have discussed the different storage classes for functions in C++. We have learned how to use auto, static, register, and extern storage classes for functions. We have also discussed their syntax, example, output, explanation, use, important points, and summary.

Published on: