Inline Function, Global Variable And Local Variable.

Function With Default Arguments

  • C++ lets a function call without specifying all its arguments. In such instances, a parameter that does not have a corresponding argument in the function call is given a default value. Default values are specified upon declaration of the function. 
  • From the prototype the programmer knows how many arguments a function uses to make a call.
  • The concept behind arguing by default is plain. If a function is called by passing arguments, the function will use those arguments. 
  • But if the argument is not passed while a function is invoked then the default values are used. 
  • In the prototype function the default value is transferred to argument.
Example
float result(int marks1, int marks2, int marks3=75); 
a subsequent function call
average = result(60,70);
passes the value 60 to marks1, 70 to marks2 and lets the function use default value of 75 for marks3.
The function call 
average = result(60,70,80); 
passes the value 80 to marks3.

Inline Function

www.mskuthar.blogspot.com
Inline Function
  • Functions conserve memory space, since all function calls trigger the same code to be executed. In memory the body functions need not be duplicated. 
  • When a function call is viewed by the compiler it normally jumps to the function. The function finishes. 
  • It normally jumps back to the subsequent call statement.
  • While event sequence may save space in the memory, it takes some extra time. 
  • Inline feature is used to save execution time in short functions. 
  • Every when a function call occurs, the actual function code is inserted in place of a hop to the method. 
  • The inline feature is only used for shortened code.

inline int cube(int r)
{
    return r*r*r;
}

Some important points to be noted
  1. Inline function should be declared before main() function.
  2. It does not have function prototype.
  3. Function is made inline by putting a word inline in the beginning.
  4. Only shorter code is used in inline function If longer code is made inline then compiler ignores the request and it will be executed as normal function.

Global Variable And Local Variable

Local Variable:
  • A attribute declared within a function's body will only be measured within the function. 
  • The portion of the program which holds a variable in memory is known as the variable's scope. 
  • The local variable's scope is a function in which it is set. 
  • A variable may function locally or be a compound statement.

Global Variable:
  • A variable which is defined as a global variable outside of any function is named. Such a variable's duration lasts until the end of the program. 
  • These variables can be accessed for all functions that follow their declaration. Therefore it should be defined at the outset, before any function is defined.
Unary Scope Resolution Operator (::)
  • Local and global variables may be declared with the same name. 
  • C++ provides the operator of unary scope resolution (:) for accessing a global variable when a local variable of the same name is in scope. 
  • Without the unary scope resolution operator, a global variable can be accessed directly if the name of the global variable does not match that of a local variable in scope.

Variables and storage Class


  • The storage class of a variable determines which parts of a program can access it and how long it stays in existence. 
  • The storage class tell us some following points.
  1. The variable scope.
  2. The location where the variable will be stored.
  3. The initialized value of a variable.
  4. A lifetime of a variable.
  5. Who can access a variable.
Automatic variable

  • All variables by default are auto i.e. they are similar to the statements int a and auto int. Auto variables maintain their scope until the end of the function they are being described in. 
  • There is no automatic variable created until the function in which it has been defined is called. When the calling program returns the function exits and control, the variables are destroyed and their values are lost. 
  • The term automatic is used because when a function is called the variables are automatically generated and automatically deleted when it returns.
Register variable 

  • A declaration on a list is an auto declaration. 
  • An auto variable has all the characteristics of a registry variable. The difference is that the register variable offers quick access, as it is stored in CPU registers rather than in memory.
Static variable

  • A static variable has local variable visibility but global variable lifespan. 
  • Thus it is only apparent within the context in which it is described, but it remains in existence for the program's life.
External variable 

  • A large program may be written in different files by a number of persons. 
  • A variable declared global in one file won't be available in another file for a function. 
  • Such a variable should be declared global in one file if needed by functions in both files, and declared local in the second file at the same time.



C++ Storage Classes

  • Space class is used to describe a variable and/or function's lifetime and visibility within a C++ program. 
  • Lifetime refers to the duration during which the variable remains active and visibility refers to the program module in which the variable is available.
  • There are five types of storage classes, which can be used in a C++ program.


  1. Automatic
  2. Register
  3. External.
  4. Static.
  5. Mutable.

Automatic Storage Class
  • Automatic storage class assigns the default storage type to one variable. 
  • Auto keyword is used to automatically declare the variables. However, if a variable is declared inside a function without any keyword, it is automatic by default. 
  • This attribute is only available within the specified function, and its lifetime is also the same as the lifetime of the function. 
  • Once the function execution is complete the variable will be destroyed.
Syntax of Automatic Storage Class
datatype var_name1 [= value];
or
auto datatype var_name1 [= value];

Example of Automatic Storage Class
auto int x;
float y = 5.67;

2. Register Storage Class

  • File management assigns location of a variable in the CPU registers instead of the primary memory. The same as automatic feature, it has its lifetime and visibility. 
  • The purpose of creating registry variable is to increase access speed and faster running of program. 
  • If there is no space available in the register, these variables will be stored in the main memory and will act similarly to automatic storage class variables. 
  • So register should be made only for those variables that require quick access.
Syntax of Register Storage Class Declaration
register datatype var_name1 [= value];

For example,
register int id;
register char a;

Example of Storage Class
C++ program to create automatic, global, static and register variables.
#include<iostream.h>
#include<conio.h>
int g;    //global variable, initially holds 0

void test_function()
{
    static int s;    //static variable, initially holds 0
    register int r;    //register variable
    r=5;
    s=s+r*2;
    cout<<"Inside test_function"<<endl;
    cout<<"g = "<<g<<endl;
    cout<<"s = "<<s<<endl;
    cout<<"r = "<<r<<endl;
}

int main()
{
    int a;    //automatic variable
    g=25;
    a=17;
    test_function();
    cout<<"Inside main"<<endl;
    cout<<"a = "<<a<<endl;
    cout<<"g = "<<g<<endl;
    test_function();
    return 0;
}

G is a global variable in the above program, s is static, r is a register and an is an automatic variable. We defined two functions: first is main) (and second is test function). Given that g is global variable, it can be used for both functions. In test function) (the variables r and s are declared so that they can only be used inside that function. S being static, however, is not broken until the end of the program. When test_function() is called for the first time, r is initialized to 5 and the value of s is 10 which is calculated from the statement,

s=s+r*2;

After test function) (is terminated, r is destroyed but s still retains 10. When the second time it is called, r is created and initialized again to 5. Now, since s initially held 10, the value of s becomes 20. Parameter a is declared within main) (and may only be used within main).

Expected Output:-
Inside test_function
g = 25
s = 10
r = 5
Inside main
a = 17
g = 25
Inside test_function
g = 25
s = 20
r = 5


3. External Storage Class

  • External storage class assigns a reference to a global variable that has been declared outside of the given system. 
  • External keyword means external variables are declared. 
  • Throughout the program they are visible, and their lifetime is the same as that of the program where it is declared. 
  • This is clear to all of the roles in the system.

Syntax of External Storage Class
extern datatype var_name1;

For example
     extern float var1;

Example of External Storage Class
C++ program to create and use external storage.

File: sub.cpp
int test=100;  // assigning value to test

void multiply(int n)
{
    test=test*n;
}
File: main.cpp
#include<iostream>
#include "sub.cpp"  // includes the content of sub.cpp
using namespace std;

extern int test;  // declaring test

int main()
{
    cout<<test<<endl;
    multiply(5);
    cout<<test<<endl;
    return 0;
}
In main.cpp a variable check is declared as being external. It is a global variable and under sub.cpp is allocated to 100. Available in both formats. The multiply) (function multiplies the test value by the parameter that is passed to it while invoking it. The program executes the multiplication and adjusts to 500 the global variable check.

Note: Run the main.cpp program

Expected Output:-

100
500

4. Static Storage Class

  • Static storage class ensures that a variable has local variable visibility mode but external variable lifetime. 
  • It can only be used in the function where it is declared but only destroyed after execution of the program has been completed. 
  • When calling a function, the variable described as static inside the function retains and operates on its preceding value. 
  • Mostly this is used to save values within a recursive function.

Syntax of Static Storage Class
static datatype var_name1 [= value];

For example

static int x = 101;
static float sum;

5. Mutable Storage Class


  • In C++, keyword const can be used to keep a class object constant. 
  • This does not permit alteration of the class object's data members during program execution. 
  • Nevertheless, there are cases when some data members need to be modified from this constant entity. 
  • For example, a money transaction has to be locked during a bank transfer so that no information can be changed, but even then its state has changed from-started processing to completion. 
  • In those cases, we can use a mutable storage class to make these variables modifiable.
Syntax for Mutable Storage Class
mutable datatype var_name1;

For example
mutable int x;
mutable char y;

Example of Mutable Storage Class
C++ program to create mutable variable.

#include<iostream.h>
#include<conio.h>
class test
{
    mutable int a;
    int b;
    public:
        test(int x,int y)
        {
            a=x;
            b=y;
        }
        void square_a() const
        {
            a=a*a;
        }
        void display() const
        {
            cout<<"a = "<<a<<endl;
            cout<<"b = "<<b<<endl;
        }
};

int main()
{
    const test x(2,3);
    cout<<"Initial value"<<endl;
    x.display();
    x.square_a();
    cout<<"Final value"<<endl;
    x.display();
    return 0;
}

In the course, a class test is specified. It consists of a member of the mutable data a. A constant object of the class test is generated, and user-defined constructor initializes the value of the data members. Since b is a regular data member its value after initialization can not be modified. However a mutable being, its value can be modified by invoking the method square a). Method display) (shows the value of the members of the data.

Expected Output

Initial value
a = 2
b = 3
Final value
a = 4
b = 3

Post a Comment

Previous Post Next Post