Function
Function
Pass By Value
The values of actual parameters are copied into formal parameters
The formal parameters contain only the copy of actual parameters
If the values of the formal parameters changes in the called function, the
values of the actual parameters are not changed
Pass by reference/Pass by pointers
The memory addresses of the variables rather than the copies of
values are sent to the called function
In this case, the called function directly works on the data in the
calling function and the changed values are available in the calling
function for its use
Storage Classes
• Storage class of a variable defines scope(visibility) and lifetime of a variables
and/or functions declared within a c program
• storage class gives the following information about the variable
- determines the part of memory where storage space will be allocated for that
variable or function. memory(register or RAM)
- Specifies how long the storage allocation will continue to exist for that
function or variable
- Specifies the scope of the variable or function. Whether the variable/function
can be referenced throughout the program or only within the function, block,
or source file where it has been defined
- Specifies whether the variable or function has internal, external or no linkage
1. Automatic
2. Register
3. External
4. static
1. Auto
It is a default storage class for variables declared inside a block
All the local variables declared within a function belong to automatic storage class
by default
They should be declared at the start of the program block(right after { )
If auto variables are not initialized at the time of declaration, then they contain
Example:
The register storage class is used to define local variables that should be
stored in a register instead of RAM
Ex: register int x;
the variable has a maximum size equal to the register size (usually one
word) and can't have the unary '&' operator applied to it (as it does not
have a memory location).
The register should only be used for variables that require quick access
Register variables also have automatic storage duration. Each time a
block is entered, the storage for register variables defined in that block is
accessible and the moment that block is exited , the variables becomes
no longer accessible for use
Example:
#include<stdio.h>
void main()
{
int a,b;
register int z;
printf(“Enter two number to add”);
scanf(“%d%d”,&a,&b);
z=a+b;
printf(“sum=%d”, z);
}
3.Extern
The extern storage class is used to give reference of a global variable that is visible to
Memory is allocated for external variables when the program begins execution and
In case the extern variable is not initialized, it will be initialized to zero by default
External variables have global scope i.e, these variables are and accessible from all the
The extern keyword is used with a variable to inform the compiler that this variable is
declared somewhere else. The extern declaration does not allocate storage for
variables
The main purpose of using extern variables is that they can be accessed
between two different files which are part of a large program.
In case of a local variable and a global variable have the same name, the
local variable will have precedence over the global one in the function
where it is declared.
Let’s consider the use of extern in functions.
when a function is declared or defined, the extern keyword is implicitly
assumed.
Ex: When we write.
Memory is allocated when the program begins and is freed when the
program terminates
Ex: static int x;
factorial( int n)
void main()
{
{
if(n==1)
int num,fact;
return 1;
printf(“enter number:”);
else
scanf(“%d”,&num);
return (n*factorial(n-1));
fact=factorial(num);
}
printf(“factorial of a number:”,fact);
}
Example 2: The fibonacci series