06 Functions in C
06 Functions in C
INTRODUCTION TO FUNCTIONS: -
NEED OF FUNCTIONS,
SCOPE AND LIFETIME OF VARIABLES,
DEFINING FUNCTIONS,
FUNCTION CALL, BY VALUE, BY REFERENCE,
FUNCTION RETURN VALUES,
STORAGE CLASSES.
CATEGORY OF FUNCTION –
o NO ARGUMENT NO RETURN VALUE
o NO ARGUMENT WITH RETURN VALUE,
o ARGUMENT WITH RETURN VALUE,
o RECURSION,
COMMAND LINE ARGUMENTS
INTRODUCTION TO FUNCTIONS: -
Basically, C is mostly function oriented programming language. Every task in C is perform using specific function.
We already use various functions like. clrscr( ) to clear the screen. But how it clear the screen ? …. Is it magic ?
…. No, it is ( Built in Function ) developed by developers and stored in header files. This is just an example but
lots of readymade functions are available to us for use.
Defination:
“Function is a self contained block of statements that perform a coherent task of some kind.”
FUNCTION…….
Function is a procedure to break your large program in small set of program.
Function is referred as a small program which we can use as per need.
Function is self-contained block of statements to perform a particular task.
C programme is collection of one or more functions.
Function is developed to complete the need of programmer.
Function makes programs easy, readable, portable & flexible.
NEED OF FUNCTIONS,
The benefits of using functions are:
Divide and Conquer: construct the program from simple, small pieces or components. Modularize the
program into self-contained tasks.
Avoid repeating codes: It is easy to copy and paste, but hard to maintain and synchronize all the copies.
Software Reuse: you can reuse the functions in other programs, by packaging them into library codes.
Subject: C Programming (Function). Page 1 of 8
SCOPE AND LIFETIME OF VARIABLES.
Scope Of Variable :
The area where a variable can be accessed is known as scope of variable.
Lifetime Of Variable :
The time period for which a variable exists in the memory is known as lifetime of variable.
Local Variable
o A variable declared inside a function is known as local variable.
o Local variables are also called automatic variables.
o Scope Of Local Variable:
Local variable can be used only in the function in which it is declared.
o Lifetime of local variable :
Lifetime of local variables starts when control enters the function in which it is declared
and it is destroyed when control exists from the function.
Global variable
o A variable declared outside any function is known as global variable.
o Global variables can be used by all functions in the program.
o Scope of Global variable
These variables are globally accessed from any part of the program.
They are declared before main function.
o Lifetime of Global variable
Global variables exist in the memory as long as the program is running.
These variables are destroyed from the memory when the program terminates.
These variables occupy memory longer than local variables.
TYPES OF FUNCTION…….
Functions are classified into two types.
Built In Function.
User Defined Function.
BUILT IN FUNCTIONS :
These categories of function are already developed in C language and stored in various header files. Once we
include header file in our program then programmer can freely use that functions. These functions are already
declared and defined by C Language, the programmer can call it as per the requirement. The following are some
categories of functions developed by C Language.
Functions to perform Mathematical task.
Function to perform operations on String.
Function to perform Standard Input and Output operations.
Function to work with Graphics environments.
Functions for the use of file Handling etc.
Example : Write a Programme to shows the address of Galaxy Computer Education using function.
void Show( ); // Step 1 … Function Declaration.
main( )
{
Show ( ); // Step 2 …. Function Call….
}
As we see the example of function but it is not enough …. We will see the details of function in this chapter.
CATEGORIES OF FUNCTIONS :
NO ARGUMENT NO RETURN VALUE ( Default Functions With No Return Value )
o The functions developed without any argument and return value.
NO ARGUMENT WITH RETURN VALUE ( Default Functions With Return Value )
o The functions developed without any argument and but return value.
ARGUMENT WITH RETURN VALUE ( Parameterised Functions With Return Value )
o The function developed by passing some type of values as argument to that function and it
return the value.
ARGUMENT WITH NO RETURN VALUE ( Parameterised Functions With No Return Value )
o The function developed by passing some type of values as argument to that function and
without return value.
Recursive Function. :
o The function can call the same function from it’s self contained block then it is called as Recursive
Function.
o Recursive function must have a stop condition, for the recurssion process.
What is Return Value?
Some function can return answer back from it call and that value is store in a variable of left side.
Subject: C Programming (Function). Page 3 of 8
Functions can maximum return one value.
The left side variable and return value of a function must have same data type.
When no return type is specified function is declare as void.
Syntax Of Function
Return_Type function_name( list of parameters );
DEFAULT FUNCTIONS.
In C Programming Language, when we create a new function, without passing any parameter to it then
it is called as “Default Function”. Default function is call without any parameter.
For example : void Display( ) ; This is default function
PARAMETERISED FUNCTIONS.
When function is called by passing the values to it within parenthesis, then it is called as parameterised function.
There are two types of parameters…
Actual Parameter : The values pass to the function at a time of calling is called as “Actual Parameter”.
e.g. add( 10 , 20 ) ; // Here add is name of function and 10 & 20 are actual parameters.
Now these parameters are received by formal parameters.
Formal Parameter : The values pass by calling function at a time of calling, are received in a variable having
same data type by called function are called as “Formal Parameter”.
e.g. void add( int no1 , int no2 ) { ….. } // Here add is name of function and no1 & no2 both are variable
declare to store the values of actual parameters. So no1 contains value 10 and no2 contains 20 etc.
Note : Number of actual parameters and it’s types must matches the number of parameters and types of the
formal parameters.
The following Figure Demonstrate this case in details.
Function Name
EXAMPLE : W.A.P. to pass a number to the square function and return it’s square.
int square( int );
main()
{
int no, s ;
no = 5;
s = square ( no ) ;
printf(“\n\n\t\tNumber Is %d and Square is %d “, no, s);
getch();
}
int square( int no1 )
{
int res;
res = no1 * no1 ;
return( res );
}
Some Points About Return Statement :
When return statement is executed, it immediately transfer the control to the calling function.
No other statements are executed after return statements.
It returns the value enclosed in parenthesis e.g. return( 10 );
Function can maximum return one value at a time.
Return type must match the type of return value.
FUNCTION CALL BY VALUE AND CALL BY REFERANCE (Is Cover in Chapter Pointer)
STORAGE CLASSES
A storage class defines the scope (visibility) and life time of variables and/or functions within a C Program. There
are following storage classes which can be used in a C Program
Auto Storage Class
o auto is the default storage class for all local variables.
{
int Count;
auto int Month;
}
The example above defines two variables with the same storage class. auto can only be used
within functions, i.e. local variables.
Register Storage Class
o register is used to define local variables that should be stored in a register instead of RAM.
o This means that the variable has a maximum size equal to the register size (usually one word)
and cant have the unary '&' operator applied to it (as it does not have a memory location).
{
register int Miles;
}
o Register should only be used for variables that require quick access - such as counters.
o It should also be noted that defining 'register' goes not mean that the variable will be stored in a
register. It means that it MIGHT be stored in a register - depending on hardware and
implimentation restrictions.
{
printf("%d\n", Road);
}
o static variables can be 'seen' within all functions in this source file.
o At link time, the static variables defined here will not be seen by the object modules that are
brought in.
o static can also be defined within a function. If this is done the variable is initalised at run time but
is not reinitalized when the function is called. This inside a function static variable retains its value
during vairous calls.
Extern Storage Class
o extern is used to give a reference of a global variable that is visible to ALL the program files.
o When you use 'extern' the variable cannot be initalized as all it does is point the variable name
at a storage location that has been previously defined.
o When you have multiple files and you define a global variable or function which will be used in
other files also, then extern will be used in another file to give reference of defined variable or
function.
o Just for understanding extern is used to decalre a global variable or function in another files.
File 1: main.c
int count=5;
main()
{
write_extern();
}
File 2: write.c
void write_extern(void);
void write_extern(void)
{
printf("count is %i\n", count);
}
o Here extern keyword is being used to declare count in another file.
COMMAND LINE ARGUMENTS
o When we pass some values from the command line to your C programs when they are executed,
these values are called command line arguments.
o The command line arguments are handled using main() function arguments where argc and
argv[].
argc refers to the number of arguments passed, and
argv[] is a pointer array which points to each argument passed to the program.