You are on page 1of 69

Functions

All c programs have at least one function


that is the main.
When the program grows into larger, may
have many disadvantages.
1. Difficult to write a larger programs
2. Difficult to identify the logical errors and
correct.
3. Difficult to read and understand.
4. Larger programs prone for more errors.
| 
     
   
| 
u   
           
  

      
         
                 
   
 
     
            
    

| 
     
   
| 
!      

| 
     
   
| 
_ f any module has sub modules that is called as calling
module and sub modules are called called modules.
_ one module is communicate with other module through
calling module.
_ the function which is calling other function is called
calling function.
_ A function whom it called is called called function
_ A called function receives control from the a calling
function, when it completes its task, it returns control to
the calling function.
_ the main function called by the operating system, in
turn main function calls the other function to do some
task.

| 
     
   
| 
A large program divided into series of
individual related programs called
modules. These modules are called
functions.
Functions are classified into two categories
1. Library functions ( built_in functions).
2. User_defined functions

| 
     
   
| 
Library (built in) functions
C is a library it is a collection of various types of functions
which perform some standard and predefined tasks.
ƥ This are part of the c compiler that have been written for
general purpose are called library functions.
example
printf()
scanf()
sqrt()
Advantage
1.No need to write the code because the functions are
already available so it makes the programmers job much
easier.
2. these functions are used anywhere in the program.

| 
     
   
| 
User defined functions

The functions are written by the


programmer to achieve some work

| 
     
   
| 
Advantages of function

1. Readability of the program can be


increased. Problem can be easily
understandable.
2. Reuse_ the function written in one
program can also used in other program.
(reduce the size).
1. Makes the program easy for testing,
coding and debugging.
2. Saves time as well as space.
| 
     
   
| 
!          !

| 
     
   
| 
ð  
 
 


¢     
      


    
          
         
  
             

   
 

      

!   "   
!   "  
 !   
| 
     
   
| 
Definition

A function is a self_ contained block of code


that performs a particular task.
t takes some data from the main program
and may or may not returns a value.

| 
     
   
| 
Ú
u         #      
      

| 
     
   
| 
Function declaration

Funtion prototype(syntax)
Return_type function_name (argument list);

| 
     
   
| 
Definition
Syntax for the function definition
Returntype function_name(argument list/parameters list)
argument list declaration
{
local variable declarations:
Statements;
return(expression);
}//function.
All the parts are not essential.

Argument list and declaration optional.


Return statement is also optional.

| 
     
   
| 
main()
{
___________
__________
func1();
_________
________
funct2();
________
_________
funct3();
_________
_______
}//main
func1()
{
__________
___________
}func1
func2()
{
________
________
}//func2
fucn3()
{
__________
_________
}//func3

| 
     
   
| 
Return type_ the type of value return by the
called function after its task finished off

Function _name_ give name to the function


as naming the variables.
Argument list/parameters_ contains the valid
names separated by commas and must
enclosed in parenthesis

| 
     
   
| 
main()\\calling function
{
func1();
printf(Ơwe are in main\nơ);
}//main
func1()\\ called funciton
{
printf(Ơwe are in func1\nơ);
}//func1

| 
     
   
| 
Points to remember
1. C program must have at least one function
2. f program have one function it must be main
3. f program have more than function, in which one must
be main because program execution starts from that
only.
4. We can use unlimited no of functions in one program.
5. Each function is called in the sequence we have
specified in the main function.
6. After each function task is over the control passed to the
calling function(main()).
7

| 
     
   
| 
main()
{
printf(Ơ\n  am calling other functionsơ);
func1();
func2();
func3();
Printf(Ơall functions are called\nơ);
}//main
func1()
{
printf(Ơin func1\nơ);
}//func1
func2()
{
printf(Ơin func2\nơ);
}//func2
func3()
{
printf(Ơin func1\nơ);
}//func3

Output:

 am calling other function


in func1
in func2
in func3
all functions are called.
| 
     
   
| 
7 One function can also call the other function which has already
been called.
main()
{
printf(Ơ am in main\nơ); output
func1();  am in main
printf(Ơ back in main\nơ); in func1
}//main
func1() n func2
{ n func3
printf(Ơin func1\nơ);  back in func2
func2();  back in func1
printf(Ơ back in func1\nơ);  back in main
}//func1
func2()
{
printf(Ơn func2\nơ);
func3();
printf(Ơ back in func2\nơ);
}//func2
func3()
{
printf(Ơin fac3\nơ);
}//func3

| 
     
   
| 
!  $ "      "    !   

| 
     
   
| 
!  %   !     ! 

| 
     
   
| 
!u&    !      ! 

| 
     
   
| 
!u&    !      ! 

| 
     
   
| 
!u&    !      ! 

| 
     
   
| 
!  ' ( 
 !     ! 

| 
     
   
| 
!  )    !        *

| 
     
   
| 
!u&     (   +

| 
     
   
| 
!u&     (   +

| 
     
   
| 
!u&     (   +

| 
     
   
| 
!u&     (   +

| 
     
   
| 
!  , !   "   

| 
     
   
| 
Categories of functions

Based on the functions return type and its


arguments the functions are categorized into 4
different types.
1. Functions with no arguments and no return
values
2.Functions with no arguments and return values
3. Functions with arguments and no return values
4. Functions with arguments and return values

| 
     
   
| 
1. Functions with no arguments
and no return values
* This type of function has no arguments,
meaning that it does not receive any data from
the calling function.
ƥ Simillary this type of function will not return any
value.
ƥ Here the calling function does not receive any
data from the called function. n effect, there is
no data transfer between the calling function
and the called function.

| 
     
   
| 
Example
rr                    
  
 
A X
 
!"
A #
$%&
A #

$%
A '
A  $%&
A $%&() 
* 
)(
A  $%&
A +
A #
$%
A '
A
,-.-/&
A 
*$01 2 # *, . 0%&
A   *$0334-5,-5.%&
A /6,7.&
A 
*$01 8634-/%&
A +
| 
     
   
| 
2. 9:|8;<:=;8>:< ?@A2:8 :B?28?:C D2

A There are two ways that a function terminates


execution and returns to the caller.
1. When the last statement in the function has
executed and conceptually the functionƞs ending
Ɲ}ƞ is encountered.
2. Whenever it faces return statement.
there is no data transfer from the calling function
to the called function. But, there is data transfer
from called function to the calling function.

| 
     
   
| 
example
rr                       
 
A X
 
!"
A
$%&
A #

$%
A '
A
 &
A  $%&
A 6$%&() 
* 
)(
A 
*$01 8634- %&
A  $%&
A +
A
$%
A '
A
,-.&
A 
*$01 2 # *, . 0%&
A   *$0334-5,-5.%&
A  $,7.%&
A +

| 
     
   
| 
3. 9:|8;<:=;8> ?@A2:8 :B:<?28?:C D2

n this category there is data transfer from the calling function to the
called function using parameters. But, there is no data transfer from
called function to the calling function.
A 9 
 
A The arguments that are supplied in to two categories
A 1. actual arguments/parameters
A 2. formal arguments/parameters
A  (
A Actual parameters are the expressions in the calling functions.
These are the parameters present in the calling statement (function
call).
A *  (
A formal parameters are the variables that are declared in the header
of the function definition. These list defines and declares that will
contain the data received by the function. These are the value
parameters, copies of the values being passed are stored in the
called functions memory area.
A :  Actual and Formal parameters must match exactly in type,
order, and number. Their names however, do not need to match.

| 
     
   
| 
_ the values of actual arguments are assigned to the formal arguments.
func1(x1, x2, x3,Ʀxn);
| | | |
func1(y1, y2, y3,Ʀyn)
_ n case, the actual arguments are more than the formal arguments , extra
actual arguments are discarded.
_ on other hand, the formal arguments are more, the extra formal arguments
are initialized to some garbage values.
_ if any mismatch found in their data type may also takes some value. No
error is reported.

| 
     
   
| 
example
((|  *
 *E  F
* 
E
    #
A X
 
!"
A #
$
-
%&
A #

$%
A '
A
-F&
A  $%&
A 
*$01 2 # * F 0%&
A   *$0334-5-5F%&
A $-F%&() 
* 
)(
A  $%&
A +
A #
$
,-
.%
A '
A
/&
A /6,7.&
A 
*$01 8634-/%&
A }

| 
     
   
| 
4. 9:|8;<:=;8> ?@A2:8 :B?28?:

C D2

n this category there is data transfer


between the calling function and called
function.

| 
     
   
| 
example
((|  *
 *E  F
* 
E
   #
A X
 
!"
A
$
-
%&
A #

$%
A '
A
-F&
A  $%&
A 
*$01 2 # * F 0%&
A   *$0334-5-5F%&
A 6$-F%&() 
* 
)(
A 
*$01 8634- %&
A  $%&
A +
A
$
,-
.%
A '
A
/&
A  ,7.&
A +

| 
     
   
| 
:  generally we are using functions with arguments
and return values (category 4) in our applications. Why
because the job of a function is to perform a well_
defined task, it carries inputs and sends result after
executed. n real world the programming teams codes
the modules (functions) according to the input
(arguments) given by the team coordinators.

| 
     
   
| 
Recursion
n c, a function calls itself is called recursion.
t is a repetitive process where function is called itself.
Ex:_
main()
{
printf(Ơrecursion\nơ);
main();
}//main
output:
recursion
.
.
.
A Any problem that can be solved recursively can also be solved iteratively
(using loop).
A Recursive functions are slow and takes a lot of memory space
compared to iterative functions.

| 
     
   
| 
2, ? 
#*  
* 

((|  *
*  

 
!
fact (n)= n(n_1)(n_2)____1
fact(5)= 5*4*3*2*1=120
#include <stdio.h
#include<conio.h
long int factorial(long int x);
main()
{
long int n,fact;
printf(Ơenter the number\nơ);
scanf(Ơ%ldơ, &n);
fact=factorial(n);
printf(Ơfactorial of a number=%ldơ,fact);
}//main
factorial(long int x)
{
long int f;
if(x==1)
return 1;
else
f= x*factorial(x_1);
return(f);
}//factorial()
| 
     
   
| 
Factorial of a number using non_recursive function (iterative method)
long int factorial(int x);
main()
{
long int n,fact;
printf(Ơenter n value\nơ);
scanf(Ơ%ldơ,&n);
fact=factorial(n);
printf(Ơfactorial value=%ldơ,fact);
}//main
factorial(int x);
{
long int f=1,i;
for(i=x;i =1;i__)
f=f*i;
return (f);
}//factorial();

Out put
enter n value
5
factorial value= 120

| 
     
   
| 
Return statement

1. When return statement encounter the control transferred back to the


calling function.
2. t returns the value present in its parenthesis to the calling function.
3. We can use any number of return statements in the functions. However
it returns only single value.
4. Return statement need not be present at end of the function.
5. Function should return only one value at a time.
return (a,b);_ invalid gives the error.

| 
     
   
| 
Scope and life time of variable in functions

Scope of the variable:_ determines what


parts of the program can have access the
variable or what parts of the program a
variable is available for use (active).
Life time of a variable (longitivity)
How long the variable retains its value
during execution of a program (alive)

| 
     
   
| 
÷   


The variables are stored in computer.


_ Basically variables are stored in computer in two locations.
_ 1. memory 2. registers.
_ Location of the variable to store is determined by the storage
classes.
_ t also tells us the following
1. where the variable would be stored.
2. what is initial value of the variable, if variable is not initialized
while declaring.
3. what is the scope of the variable; i.e in which functions the
value of the variable would be available.
4. what is the life of a variable; i.e how long variable would be
exist.

| 
     
   
| 
There are four storage classes:

1. Auto storage class


2. Static storage class
3. Extern storage class
4. Register storage class

| 
     
   
| 
u       

 

 
     


  
  
    
   

          
     
   
   
 !"

  "
      



 

   . #
FG &


&
H.*
 !

| 
     
   
| 
Features of auto variable when they declared
Storage Memory
Default initial value garbage value
Scope local to the block
in which variable
defined
Life remains within the
block in which
variable defined.

| 
     
   
| 
Example_ auto
main()
{
auto int x=10;
{
auto int x=20;
{
auto int x=30;
printf(Ơ%dơ,x);// 30
}
printf(Ơ%dơ,x);//20
}
Printf(Ơ%dơ,x);//10
}//main

| 
     
   
| 
÷ 
  

 

 "


#
u

 
  
   




$  
    
 
  
 
     



  
 

    
 
   
 
   

 
 

| 
     
   
| 
Features of static variable

_ Storage ________ Memory


_ Default initial value _____ zero
_ Scope ____________ Local to the block in
which the variable is defined.
_ Life _________ value of the variable persists
between different function calls.
_ Once memory is allocated to the static
variable is persists its value until he end of
the program
| 
     
   
| 
example
main()
{
demo1();
demo1();
demo1();
}//main
demo1()
{
static int i=1;
printf(Ơ%d\nơ,i);
i++;
}//demo1()
output:
1
2
3

| 
     
   
| 
Auto example
main()
{
demo1();
demo1();
demo1();
}//main
demo1()
{
auto int i=1;
printf(Ơ%d\nơ,i);
i++;
}//demo1()
output:
1
1
1

| 
     
   
| 
Extern (external storage classes)

Syntax
extern type identifier;
extern int i;
features
_ This variables or also called global variables.
_ These are declared outside of any function.
_ Storage ƛ memory
_ Default initial value_ 0
_ Scope_ global can access even outside of the function.
_ Life_ through out the program execution.
_ Any variable defined outside of the function by default
treated as extern variable.

| 
     
   
| 
example


#


%
"
 & ' (
#
) #
) #
* #
* #
+,,

) 
%

--#
"
 & ' 
#
+,, ) 
* 
 
% -

# .
"
 & ' 
#
+,, * 

.
-
| 
     
   
| 
Register storage class

_ Special case of the auto storage class


_ Syntax
register type identifier;
_ storage_ registers
_ default initial value_ garbage value
_ scope ƛ local to the block in which the variable
is defined.
_ life ƛ value persists within the block in which
the variable is defined.
| 
     
   
| 
  
 
#I  

Preprocessor:_ a program used to process a


source code before the compilation.
_ Preprocessor has several features called
preprocessor directives which are start
with a symbol #.
_ Preprocessor directives can placed any
where in the program.

| 
     
   
| 
Macro expansion

A 
  
    
.
A X  /  01/ 23 / 
A X       
 /   
    /  
                  
A &/       
A &/   4  
4#
X  ! .
 03
5
   6 )
  6 ! 7 7
 08 6 9 : 3
;rr

| 
     
   
| 
What is the purpose of macro

A Preprocessor replaces the every occurrence of P in the


program before compilation.
A Before compilation, preprocessor checks for the macro
name used in the directives, when it finds replace
macro name with the macro body.
A Once this process over the preprocessor handover to the
compiler.
A A constant value appears many times through out the
program, you may need to be changed later. manually
you need to change each occurrence of that constant.
however, if u define that constant using the macro ,you
only need to make one change in the macro body.

| 
     
   
| 
A t is customized to use the macro names
in capital letters.
A Macro names and macro body separated
by space.
A Space between # and define is optional.
A Macro definition never end with
semicolon.

| 
     
   
| 
_ # define directive is used to define operators.
# define PLUS +
main()
{
int c;
c= 10 PLUS 20;
}//main()
_ # define directive could be used even to replace a condition.
_ Ex:
_ #define EX a b
_ main()
_ {
_ int a=10,b=20;
_ if (EX)
_ printf(Ơa is big\nơ);
_ else
_ printf(Ơb is bigơ);
_ }//main
_
| 
     
   
| 
A Statement also replaced using macros
# define stat printf (Ơhaiơ);
main()
{
stat
}//main

| 
     
   
| 
A  E
 
A Macro can also have arguments as same as function
have.

rr        &


X  1 2
X  + 043 04743
  03
5
 6.-
 0<=  +  9 69 <+ 033
  - 
;
!
 +  .-6.--

| 
     
   
| 
A Donƞt leave space between the macro name and its arguments.
ex:_ SQUARE (n)
A Macro body should be enclosed within parenthesis.
ex:_
#define SQUARE(n) n*n
main()
{
int a;
a=64/SQUARE(4);
printf(Ơa=%dơ,a);
}//main
_ Macros can split into multiple lines, with a Ɲ\ƞ(back slash) present at the end of the
each line.
ex:
#define ABC for(i=0;i<10;i++)\
printf(Ơ%d\nơ,i);
main()
{
int i;
ABC
}//

| 
     
   
| 
macros versus functions

A n a macro name is encountered the preprocessor


replaces it with macro body without thinking, some
literal way, whereas in function call the control passed to
a called function with some arguments, it perform some
task and at end return some value to the calling
function.
A Macros make the program run faster but increase the
program size, whereas functions make the program
smaller and compact (less space).
A Passing arguments to a function and getting back the
returned value would take time and make the program
slow this is avoided in macros, since it has already been
replaced with is macro body before compilation.
A

| 
     
   
| 

You might also like