Chapter 5 Modular Programming
Chapter 5 Modular Programming
Modular
Programming
Chapter 8 : Modular Programming
2
Objectives
3
Overview
C program made of one or more function,
one and only one of them must be named
main.
Execution of program always starts and ends
with main, but it can call other function.
Function is used to repeat the same process
with a different input or output. It might
have no input or output.
A function in C can perform a particular
task, and supports the concept of modular
programming design techniques.
Chapter 8 : Modular Programming
4
Types of Functions
5
User-defined Function
Must be declared and defined.
Function declaration also called as function
prototype.
Function definition is the body itself contains
the code needed to complete the task.
Function can be called (invoked) by
specifying its name.
Chapter 8 : Modular Programming
6
User-defined Function
3 main elements in using functions:
Function declaration/prototype
Function definition
Calling a function
Chapter 8 : Modular Programming
7
Function Declaration / Prototype
Syntax :
data_type function_name (arguments);
8
Example
GLOBAL FUNCTION LOCAL FUNCTION
#include <stdio.h> #include <stdio.h>
9
Calling Function
Syntax :
function_name (arguments);
Example :
testprt(num); @
getnum = testprt(num);
Chapter 8 : Modular Programming
10
Function Definition
Syntax :
• Compulsory • Compulsory • Optional (depends on case)
• Must be • Comply with rules • MUST state types and
synchronizes of legal identifier name of variables (if any)
with return data
type
11
Structure of Function Definition
12
Example
void display(int no)
{
no = 5;
13
Example of Full Programs
#include <stdio.h>
#include <conio.h>
void main()
{
printf("\nFirst function");
display();// Calling a function
printf("\nI have finish called a function");
getch();
}
//Define a function
void display()
{
printf("\nThis is my first function");
}
Chapter 8 : Modular Programming
14
15
Exercise
16
Basic Function Designs
17
void functions with no parameters
18
void functions with parameters
21
Non-void functions without
parameters
22
Non-void functions with parameters
27
Returning Values from Function
28
Example
main()
{
float calc(int), num;
num = calc(10);
printf (“%.2f”,num);
}
return no;
}
Chapter 8 : Modular Programming
29
Exercise
30
Exercise
31
Calling A Function
The primary function that controls function
calls and their order is called a calling
function.
To call function : type its name- including
parentheses- follow it with semicolon.
Example : getSum();
A function call in C can appear as an
expression or it can appear an expression
statement.
num = calc(10); || display();
Chapter 8 : Modular Programming
32
Calling A Function ....cont
printOne(a); return;
}
a = 33;
printOne(a);
Chapter 8 : Modular Programming
33
Exercise
Calling function or called function?
void main()
{ …..
display();
}
NESTED
void display()
{ …….
}
get_sum();
FUNCTION
int get_sum()
{ ……
return sum;
}
Chapter 8 : Modular Programming
34
Passing Arguments To Functions
Variable declared in a function are local
variable.
Their scope is limited to that function and
cannot be access from another function.
The functions are independent of each
other.
To pass value from one function to another
must use arguments or parameters.
Chapter 8 : Modular Programming
35
Passing Arguments To Functions
36
Passing Arguments To Functions
37
Exercise
38
Exercise
39
Exercise
Write an appropriate main program and two
functions based on the following criteria:
Function Name: get_Age
Purpose : get an age from user
Called by : main
Receives : none
Returns : age
40
Exercise
Write an appropriate main program and two
functions based on the following criteria:
Function Name: get_Age
Purpose : get an age from user
Called by : main
Receives : none
Returns : none
41
INTER-FUNCTION
COMMUNICATION
Chapter 8 : Modular Programming
42
Passing Pointer to a Function
Passing pointer to a function is quite similar
as passing other variable.
The function declaration and definition must
have the pointer declaration in their
argument.
Pass the address of variable when the
calling function pass call the called
function.
Chapter 8 : Modular Programming
43
Example
void main()
{
void addcon(int *, int *); // prototypes
int x=6,y=7;
printf("\nx is %d, y is %d",x,y);
addcon(&x, &y);
printf("\nx is now %d, y is now %d",x,y);
getch();
}
44
Passing Array to a Function
A function can receive the address of an
array by using pointer.
The declaration and definition for passing
array arguments is the same as passing a
pointer address.
The argument send from the calling function
is the name of array.
Chapter 8 : Modular Programming
45
Example
#define n 5
void main()
{
int get_total(int *, int); // function prototype
int total, y[n] = {1,2,3,4,5};
total = get_total(y, n); // function call
printf("\nTotal = %d", total);
getch();
}
46
Passing Arguments by Value
In the example so far, the calling function
passed arguments (value of variables) to
the corresponding parameters in the called
function.
The called function then used these
parameter values to do its computation.
Any changes made to these parameters in
the called function did not have any effect
on the variables in the calling function that
passed the values since only copies or
values of the arguments were passed to the
called function.
Chapter 8 : Modular Programming
47
Passing Arguments by Value
When only copies of arguments are passed
to a function, this is known as passing
arguments by value (or passing by copy).
Chapter 8 : Modular Programming
48
Example
void modify_Number(int); // prototypes //define a function
void modify_Number (int number)
void main() {
{
int number; number=number + 1;
printf("\n\nNumber in modify_Number is
%d",number);
printf("\nEnter an integer number: ");
scanf("%d",&number);
return ;
printf("\nNumber in main is %d",number);
modify_Number(number);
}
printf("\n\nNumber in main is %d",number);
getch();
}
Chapter 8 : Modular Programming
49
Pass Arguments by Reference
Called function can change the values of
several variables in the calling function.
This can be done by passing the addressed
of variables whose value that will be
change from the called function.
When the called function changes the
value stored in these memory addresses, the
value of variables are also changed since
the variables refer to same memory
addresses.
Chapter 8 : Modular Programming
50
Pass Arguments by Reference
51
Example
void integer_swap(int* , int*); //prototypes //define a function
void integer_swap(int *x , int *y)
void main() {
{
int x=10,y=20; int temp;
temp = *x;
printf("\nInitial values of x and y are %d %d\n",x,y); *x=*y;
integer_swap(&x,&y); *y=temp;
printf("\nSwapped values of x and y are %d %d\n",x,y); }
getch();
}
Chapter 8 : Modular Programming
52
Global Variable
Global variables are variable declared
outside of any function.
Global variables do not belong to any
specific function.
Any change made in the value of a global
variable by one function will be “felt” by the
other as well.
Global variables can be used for passing
values to a function and returning values
from it.
Chapter 8 : Modular Programming
53
Global Variable
54
Built-in Function
55
Built-in Function
Header file must be include before
using the functions contained in a
header file.
Some of the standard header files are:
stdio.h – standard I/O
conio.h – screen-handling function
math.h – various math function
string.h – string handling
ctype.h – character-handling function
time.h – system time function
………
Chapter 8 : Modular Programming
56
Built-in Function
conio.h - screen-handling function
textcolor(i)
textbackground (i)
clrscr()
gotoxy()
Chapter 8 : Modular Programming
57
Built-in Function
math.h - various math function
sqrt(d)
log10 (d)
pow(d1,d2)
sin(d)
cos(d)
Chapter 8 : Modular Programming
58
Tips