Unit 3
Unit 3
Modular programming
• Breaking down of a program into a group of files.
• The different files of the program are integrated using a main
program file.
• Each file consists of a program that can be executed
independently.
• The problem is divided into different independent but related
task.
• They are not reusable.
• Example C, COBOL, Pascal
• It is a process-centric or procedural.
FUNCTION
• It is a self-contained program.
• It is a set of instructions to perform a particular task.
• Classification of Function:
• Two types:
• Pre-defined Function(library function/built in function)
• User defined Function
Pre defined Function:
A library function needs to be declared before it is called.
It is available in header files.
It is used to access the functions.
• Ex:
• Printf is available in stdio.h
Syntax:
#include<filename.h>
Use of library function:
using a function call operator(())
Ex strcpy();
Library function
To find the square root of a number
#include<stdio.h>
#include<conio.h>
#include<math.h>
Void main()
{
Int a,b;
Printf(“enter a number”);
Scanf(“%d”,&a);
b=sqrt(a);
Printf(“%d:”,b);
getch();
}
User defined function/Programmer
defined function
• Function that are defined by user at the time of Writing a program.
• Functions are used to break down a large program into a number of smaller
functions.
Benefits:
Easy to locate and debug an error
length of the program can be reduced.
avoid Coding of repeated programming.
Three aspect:
function Declaration/Function Prototype
function Definition
Function use(function call/function invocation)
Function Declaration
• Syntax
return type function_name(parameter list or Parameter_type_list);
Ex:
int add(int ,int);
int sub(int x,int y);
Function Definition
• Composing a function
• It is the process of specifying and establishing the user defined function by
specifying all of its elements and characteristic.
• Two parts
• header of the function
• body of the function
Header of the function:
return type function name( parameter list)
Body of the function
Consists of a set of statement enclosed within braces.
Int add(int x,int y)
{
Int z;
z=x+y;
return(z);
}
return type function_name(parameter list)
Parameter declaration
{
Local variable declaration;
.
.
Body of the function;
,
,return (expression)
}
Function Use/Function Call
• The function can be called by simple specifying the name of the
function,return value and parameter if present.
• Syntax
function_name();
function_name(parameters);
return value=functionname(parameters);
Ex:
Add()
Add(a,b)
C=add(a,b)