The document discusses storage classes and functions in C/C++. It explains the four storage classes - automatic, external, static, and register - and what keyword is used for each. It provides examples of how to declare variables of each storage class. The document also discusses different types of functions like library functions, user-defined functions, function declaration, definition, categories based on arguments and return values, actual and formal arguments, default arguments, and recursion.
2. Storage Classes
Storage class specifiers tell compiler the duration and
visibility of the variables or objects declared, as well
as, where the variables or objects should be stored.
In C++ program we have multiple files. In these files
we may have normal variables, array, functions,
structures, unions, classes etc. So, variables and
objects declared must have the visibility, the lifetime
and the storage, when values assigned.
3. Storage Classes
In C / C++ there are 4 different storage classes available:
automatic, external, static and register.
It is similar in C
Storage class
Automatic
External
Static
Register
Keyword
auto
extern
static
register
4. Automatic Variable - auto
Local variables are variables declared within a function or blocks (after the opening brace, { of the
block). Local variables are automatic by default. This means that they come to existence when the
function in which it is declared is invoked and disappears when the function ends.
Automatic variables are declared by using the keyword auto. But since the variables declared in
functions are automatic by default, this keyword may be dropped in the declaration as you found in
many source codes.
Example :
auto int x, y, z = 30; auto char firstname;
- Same as:
int x, y, z = 30; char firstname;
5. External Variable - extern
External variables are variables that are recognized globally, rather than locally. In other words,
once declared, the variable can be used in any line of codes throughout the rest of the program.
A variable defined outside a function is external. An external variable can also be declared
within the function that uses it by using the keyword extern hence it can be accessed by other
code in other files
Ex:
extern int value1;
extern char name;
extern double value2;
6. Static Variable - static
In a single file program, static variables are defined within individual functions that they are local
to the function in which they are defined. Static variables are local variables that retain their
values throughout the lifetime of the program. In other words, their same (or the latest) values
are still available when the function is re-invoked later.
- Their values can be utilized within the function in the same manner as other variables, but they
cannot be accessed from outside of their defined function.
Void show(){ static int s=0;s++;cout<<s;}
7. Register Variable - register
The above three classes of variables are normally stored in computer memory. Register variables
however are stored in the processor registers, where they can be accessed and manipulated
faster. Register variables, like automatic variables, are local to the function in which they are
declared.
Usually, only register variables are assigned the register storage class. If all things equal, a
program that makes use of register variables is likely to run faster than an identical program that
uses just automatic variables.
Ex: register int x;
8. Functions
Functions allow you to group commonly used code into a compact unit that can be used
repeatedly. You have already encountered one function, main()
It is a special function called at the beginning of the program. All other functions are directly or
indirectly called from main().
Suppose you want to write a program to compute the area of three triangles. You could write
out the formula three times, or you could create a function to do the work and then use that
function three times
9. Sections of a Function
Name
Name of the function
Description
Description of what the function does
Parameters
Description of each parameter to the function
Returns
Description of the return value of the function
10. Classification of function
Library functions
Or
Standard in-build
function
Or
Compiler function
(like sqrt(), pow(),
tan() etc)
User defined functions
Or
Self-contained program
Or
Call by value
Call by reference function
(like show(), sum(),
compute() etc)
11. Parts of a function in a Program:
Function declaration (giving info to compiler abt function)
int show(int);
Function call (calling a function)
i=show(35);
Function definition (giving body to a function)
int show(int x)
{
cout<<value of x is <<x;
x++;
return(x);
}
12. Categories of a function
Function with no argument no return value
Function with argument and no return value
Function with no argument and return value
Function with argument and return value
13. Actual and Formal Arguments
The arguments declared in the function header/
function declaration is called as formal arguments.
And
The arguments passed to the functions while the
function is called is known as the actual arguments,
14. Example
Ex. Suppose sum() is a function.
int sum(int x, int y); /*Here x and y are called formal arguments*/
{...}
void main()
{
int ans;
ans = sum(3,5); /*Here the arguments 3 and 5 are called actual arguments*/
getch();
}
15. Default Argument:
Default Argument instructs the compiler what value to pass
for an argument if the programmer deliberately misses the
argument when calling a function.
A default argument is a part of function declaration (not
definition)
16. Example
#include <iostream>
int showVolume(int length, int width, int height)
int showVolume(int length, int width = 1,
int height = 1);
{
int main()
int volume;
{
volume=length*width*height;
int vola,volb,volc;
cout<<"vol is "<<volume<<endl;
vola=showVolume(4, 6, 2);
return volume;
volb=showVolume(4, 6);
}
volc=showVolume(4);
return 0;
}
17. Recursion:
When a function calls itself, it is called
recursion.
It is also called self-calling program.
WAP to find factorial of a number using
recursion???