Functions and Storage Classes
Functions and Storage Classes
RV College of
Engineering
Contents
1
1. Introduction
2 2. Using functions
3. Types of functions
3
4. Category of functions
4
Introduction Go, change the world
RV College of
Engineering
following methods:
• Solving a large problem, by breaking it into several pieces and working on each piece
separately
• Solving each piece by treating it as a new problem that can itself be broken down into
smaller problems
• Repeating the process with each new piece until each can be solved directly, without
further decomposition
Concept of function Go, change the world
RV College of
Engineering
particular task.
• The separate program segment is called a function and the program that calls it is
• All C programs contain at least one function, called main() where execution starts.
• Returning from this function, the program execution terminates and the returned
• When a function is called, the code contained in that function is executed, and
when the function has finished executing, control returns to the point at which that
• Functions are used by calling them from other functions. When a function is used,
2. Function Call.
3. Function Definition.
Function prototype declaration. Go, change the world
RV College of
Engineering
• A user-written function should normally be declared prior to its use to allow the
compiler to perform type checking on the arguments used in its call statement or
calling construct.
• The general form of this function declaration statement is as follows:
return_data_type function_name (data_type variable1, ...);
• function_name This is the name given to the function and it follows the same naming rules
as that for any valid variable in C.
• return_data_type : This specifies the type of data given back to the calling construct by the
function after it executes its specific task.
• data_type_list This list specifies the data type of each of the variables, the values of which
are expected to be transmitted by the calling construct to the function
Function prototype declaration. Go, change the world
RV College of
Engineering
• The collection of program statements in C that describes the specific task done by
the function is called a function definition.
• It consists of the function header and a function body, which is a block of code
enclosed in parentheses.
• The definition creates the actual function in memory. Function Header
• The general form of the function definition is as follows:
return_data_type function name(data_type variable1, data_type variable2,……)
{
Function Body
/* Function Body */
}
Function Definition. Go, change the world
RV College of
Engineering
• Function header is similar to the function declaration, but does not require the
semicolon at the end.
• The list of variables in the function header is also referred to as the formal
parameters.
Write a function that computes xn , where x is any valid number and n an integer value
Function Definition. Go, change the world
RV College of
Engineering
Write a function that computes xn , where x is any valid number and n an integer value
NOTE:
2. A function with return type void does not return any value.
3. There may be more than one return statement in a function, but only one
• A function will carry out its expected action whenever it is invoked (i.e. whenever the
function is called) from some portion of a program.
• The program control passes to that of the called function.
• Once the function completes its task, the program control is returned back to the
calling function.
• The general form of the function call statement (or construct) is:
function_name(variable1, variable2,…); function_name();
or or
variable_name = function_name(variable1, variable_name = function_name();
variable2,…);
Information will be passed to the function via special identifiers or expression called arguments or actual
parameters
Function Declaration, Definition and Call Go, change the world
RV College of
Engineering
#include<stdio.h> 5
int fact(int n)
int fact(int n); 1
{ 1. Function Declaration.
int main() 3
…… 2. Function Call.
{
…… 3. Function Definition.
….
return result; 4. Arguments
Z= fact(int n); 2
4
} 5. Parameters
….
}
Function Definition. Go, change the world
RV College of
Engineering
• Data is passed to a function using one of the two techniques: pass by value or call by value
• In call by value, a copy of the data is made and the copy is sent to the function. The copies
of the value held by the arguments are passed by the function call.
• In call by reference, sends the address of the data rather than a copy. In this case, the called
• In call by value, a copy of the data is made and the copy is sent to the function. The copies
of the value held by the arguments are passed by the function call.
• Since only copies of values held in the arguments are passed by the function call to the
formal parameters of the called function, the value in the arguments remains unchanged.
• As only copies of the values held in the arguments are sent to the formal parameters, the
• In call by reference, sends the address of the data rather than a copy. In this case, the called
• When an array is passed to a function, the address of the array is passed and not the copy of
Write a program that uses a function to perform addition and subtraction of two matrices having integer
numbers.
#include <stdio.h> for(i=0; i<row; i++) if(choice==1)
#define row 2 /** Read second matrix elements **/ c[i][j]= a[i][j] + b[i][j];
#define col 3 for(j=0; j<col; j++) else if(choice==2)
void mat_arith(int [][col], int [][col]); scanf(“%d”,&b[i][j]); c[i][j]= a[i][j] - b[i][j];
/* function prototype */ mat_arith(a,b); /** function call **/ else
int main() } {
{ void mat_arith(int a[][col], int b[][col]) printf(“\n Invalid choice. Task not done.”);
int a[row][col], b[row][col],i,j; { return;
printf(“\n Enter elements of the first int c[row][col],i,j,choice; }
matrix.\n”); printf(“\n For addition enter: 1 \n”) }
for(i=0; i<row; i++) printf(“For subtraction enter: 2\n”); printf(“\n The resulting matrix is:\n”);
/** Read first matrix elements **/ printf(“\nEnter your choice:”); for(i=0; i<row; i++)
for(j=0; j<col; j++) scanf(“%d”,&choice); {
scanf(“%d”,&a[i][j]); for(i=0; i<row; i++) for(j=0; j<col; j++)
printf(“\n Enter elements of the second for(j=0; j<col; j++) printf(“%d”, c[i][j]);
matrix.\n”); { printf(“\n\n”);
}
return;
}
Storage Classes in C Go, change the world
RV College of
Engineering
• In C, the variables are declared by the type of data they can hold.
• During the execution of the program, these variables may be stored in the CPU registers or
• To indicate where the variables would be stored, how long they would exist, what would be
their region of existence, and what would be the default values, C provides four storage
class specifiers that can be used along with the data type specifiers in the declaration
statement of a variable.
• These four storage class specifiers are automatic, external, register, and static
Storage Classes in C Go, change the world
RV College of
Engineering
• The storage class specifier precedes the declaration statement for a variable.
• The general form of the variable declaration statement that includes the storage class
• By default, all variables declared within the body of any function are automatic.
• The keyword auto is used in the declaration of a variable to explicitly specify its storage
class.
• Values stored in registers of the CPU are accessed in much lesser time than those stored in
• To allow the fastest access time for variables, the register storage class specifier is used.
NOTE:
2. In C, it is not possible to obtain the address of a register variable by using ‘&’ operator.
3. In addition, the only storage class specifier that can be used in a parameter declaration is
register
Storage Class Static Go, change the world
RV College of
Engineering
• Two types of variables are allowed to be specified as static variables: local variables and
global variables.
• The local variables are also referred to as internal static variables whereas the global
• To specify a local variable as static, the keyword static precedes its declaration statement
Storage Class Static Go, change the world
RV College of
Engineering
• A static local variable is allotted a permanent storage location in the primary memory.
• This variable is usable within functions or blocks where it is declared and preserves its
• However, once a function is invoked, the static local variable retains the value in it and exists
• The external static variables in a program file are declared like global variables with the
• These static variables are accessible by all functions in the program file where these variables
• A program in C, particularly when it is large, can be broken down into smaller programs.
• After compiling, each program file can be joined together to form the large program.
• These small program modules that combine together may need some variables that are
• These variables are global to all the small program modules that are formed as separate
files.
//first.c
extern int i;
/***** function definition of show()*********/
void show(void) /*** function header ***/
{ /*** fn. body starts..**/
printf("\n Value of i in pgm2.c=%d",i);
}
//second.c
#include <stdio.h>
#include "first.c" /*** link program pgm2.c ***/
int i; /*** external/global decl.**/
void show(void); /*** function prototype ***/
int main()
{
i=10;
show(); /* call to function in program file
pgm2.c */
printf("\nValue of i in pgm1.c=%d",i);
return 0;
}
Storage Class Summary Go, change the world
RV College of
Engineering