0% found this document useful (0 votes)
20 views

06 Functions in C

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

06 Functions in C

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

C PROGRAMMING.

WORKING WITH FUNCTIONS

 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.

USERS DEFINE FUNCTIONS:

Subject: C Programming (Function). Page 2 of 8


The functions developed by C are common and necessary task in programming. But C Language also allows
programmer to develop his own set of functions, as per the requirement of application / software / program. These
types of functions are called as user define functions.
To develop your own functions you must have to go with the following steps.
1. Function Declaration : Informs to the compiler the necessary information about the prototype
of function e.g. name, parameters and return type etc.
2. Function Call : Here, programmer can call the function by it’s name and pass the
necessary parameters. Compiler executes all the statement one by one
which are defined in function definition.
3. Function Definition : In this section, a programmer can write a set of statements to fulfil the
requirement of that function.

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….
}

// Step 3 ….. Function Defination


void Show( )
{
printf ( “\n\t Galaxy Computer Education” ) ;
printf ( “\n\t 2nd Floor, Mukul Plaza, Gujar Ali, ” ) ;
printf ( “\n\t Chopda Dist - Jalgaon” ) ;
}

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 );

Return Type : When Return_type is not given then it is by default int.


When it is void ( It indicate, function is not returning any value).
or Return_type is any valid data type.

SOME POINTS ABOUT FUNCTIONS.


 Scope of variables remain only in a function, in which it is declared.
 Variable Declare in one function, it is not accessible in another function directly.
 We can communicate with function by passing values to it.
 We can call function by it’s name followed by ( ) and semicolon. Eg. clrscr();
 We can define functions with it’s name and statements are enclosed in { …. }
 Function is called from main() , from another functions or you can call function from itself ( Recursion ).
 Function can declare and define once but it may call any no of times.
 Function can called from another functions but cant define in another function.

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.

Declaration of Parameterised Function…

Subject: C Programming (Function). Page 4 of 8


void addition ( int , int ) ;

Return Type Prototypes Of Arguments

Function Name

Example : Write a programme to print addition of 2 no’s using function.


void Addition( int , int );
main()
{
Addition ( 10, 20 ) ;
}
void Addition( int no1, int no2 )
{
int res = no1 + no2 ;
printf(“\n\n\t\tAddition is %d “, res );
}

FUNCTIONS WITH RETURN VALUE :


Every function have its own return_type. It may be any valid data type or void. Function return the value from
called function. Function can only return one value to the calling function.
When Return_type of function is
void : Then function is not returning any value.
int : Then function must return int type of value e.g. return( int value );
float : Then function must return float type of value e.g. return( float value );

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.

Subject: C Programming (Function). Page 5 of 8


FUNCTIONS WITH RECURSSION:
Once function is defined it is either called from main() or from other functions. But when function called itself then
it is called as Recursive function. It must have a stop condition, for the recursion process.

EXAMPLE : W.A.P. to print factorial of number using recurssion.


int factorial( int );
main()
{
int no, fact ;
no = 5;
fact = factorial ( no ) ;
printf(“\n\n\t\tFactorial Is %d “, fact);
getch();
}
int factorial( int no1 )
{
int f;
if( no1 == 1)
{
return(1);
}
else
{
f = no1 * factorial ( no1 – 1) ;
}
return( f );
}

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.

Subject: C Programming (Function). Page 6 of 8


 Static Storage Class
o static is the default storage class for global variables.
o The two variables below (count and road) both have a static storage class.
static int Count;
int Road;

{
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);

extern int count;

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.

Subject: C Programming (Function). Page 7 of 8


o Following is a simple example which checks if there is any argument supplied from the command
line and take action accordingly
#include <stdio.h>

int main( int argc, char *argv[] )


{
if( argc == 2 )
{
printf("The argument supplied is %s\n", argv[1]);
}
else if( argc > 2 )
{
printf("Too many arguments supplied.\n");
}
else
{
printf("One argument expected.\n");
}
}
o Important points to be noted.
 The argv[0] holds the name of the program itself and argv[1] is a pointer to the first
command line argument supplied, and *argv[n] is the last argument.
 If no arguments are supplied, argc will be one, otherwise and if you pass one argument
then argc is set at 2.
 You pass all the command line arguments separated by a space, but if argument itself
has a space then you can pass such arguments by putting them inside double quotes ""
or single quotes ''.

Subject: C Programming (Function). Page 8 of 8

You might also like