Sie sind auf Seite 1von 35

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.
_ A c program consists of one or more functions, one and only one of
which must be named main.
_ 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

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

| 
     
   
| 
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 fucntion
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$     )   ,

| 
     
   
| 
!u$     )   ,

| 
     
   
| 
!u$     )   ,

| 
     
   
| 
!u$     )   ,

| 
     
   
| 
!  - !   "   

| 
     
   
| 

Das könnte Ihnen auch gefallen