You are on page 1of 46

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

| 
     
   
| 
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()
| 
     
   
| 
Return statement

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


calling function.
2. t returns the value present in its parathesis 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.

| 
     
   
| 

You might also like