You are on page 1of 14

Introduction of Function in C Programming Language

Paramita Mitra

Dept. of Information Technology Institute of Engineering & Management.

Outline
What is function. Types of function.

Necessity of function.
Advantages of function. Key points of multi function program.

What is a Function ?
Function is a self contained block of statements that perform a coherent task of some kind. Every C program can be thought of to be a collection of functions.

Types of Functions

Library functionsThese are the in built functions in c library. These are already defined in header files. e.g. printf( ); is a function which is used to print an output. It is defined in stdio.h file . User defined functionsProgrammer can create their own function in C to perform specific task.

Need for user defined function


Reusability and reduction of code size. Easier Debugging Easy for Testing Easy for Maintenance

Advantages
Top down modular programming. Length of the source code can be reduced.(reduction of code size) Easy for debugging Function sharing

Flow of control in User defined function


Example:#include<stdio.h> main( ) { message( ); Printf(I m in main); } message( ) { printf(I m in message); }

o/p-

I m in message I m in main

Summarization
main( ) itself is a function from which we are calling the function message( ) A function gets called when its name is followed by an opening and a closing parenthesis and then with a semicolon. The activity of main( ) is temporarily suspended and message( ) function goes to work. When the message( ) function runs out of any statement to execute, the control returns to main( ),where it let off.

Flow of control in multi-function program


#include<stdio.h> main( ) { printf(I am in main); poly( ); engg( ); agri( ); } poly( ) { printf(I am in polytechnic); } engg( ) { printf(I am in engineering); } agri( ) { printf(I am in agri); }

o/pI am in main I am in polytechnic I am in engineering I am in agri

Summarization Contd
C Program is a collection of one or more functions. Any C Program must contain at least one function. If program contains only one function it must be main( ). There is no limit on the number of functions present in a C program. After each function has done its job, control returns to calling function, where it let off.

Any function can be called by another function


main( ) { printf(I m in main); fun1( ); printf(I m again back in main); } fun1( ) { printf(I m in fun1); fun2( ); printf(I m back in fun1); } fun2( ) { printf(I m in fun2); }

o/pI m in main I m in fun1 I m in fun2 I m back in fun1 I m again back in main

Summarization Contd

A function can be called from another function but can not be defined in another function. It should be defined outside of the main.

Conclusion Writing function avoids rewriting the same code again and again. Using functions it becomes easier to write programs and keep track of what are they doing. Cram the entire logic in one function is not suggestive. Break a program into small units and write functions. These functions perform some logically isolated task.

THANK YOU

You might also like