Chapter 5 Modular Programming
Chapter 5 Modular Programming
Modular Programming
Objectives
The need for functions How to write functions How to call and return from functions
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.
Types of Functions
already existed in the standard library of C. Used to carry out a number of commonly used operations @ calculations e.g : printf();scanf();
User-defined function
Programmers
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.
User-defined Function
Syntax :
data_type function_name (arguments);
Example
GLOBAL FUNCTION #include <stdio.h> void testprt (int); main() { } LOCAL FUNCTION #include <stdio.h> main() { void testprt (int);
...statements
...statements
Calling Function
Syntax :
function_name (arguments);
Compulsory Comply with rules of legal identifier Optional (depends on case) MUST consists of constant, value of variable or address of variable
Example :
testprt(num); @
getnum = testprt(num);
10
Function Definition
Syntax :
Compulsory Comply with rules of legal identifier Optional (depends on case) MUST state types and name of variables (if any)
11
12
Example
void display(int no) { no = 5; printf("\nValue for num is %d, no); }
13
14
15
Exercise
Write a function name number that read an integer number from user and display it on the screen. Write a function name getSum that read two integer numbers from users and display the sum on the screen.
16
3.
4.
Functions that return a value but have no parameters Functions that return a value and have parameters.
17
18
19
20
21
22
PROGRAM 4-3
23
PROGRAM 4-3
24
PROGRAM 4-3
25
PROGRAM 4-3
26
27
28
Example
main() { float calc(int), num; num = calc(10);
printf (%.2f,num); } float calc(int no) { float dfloat = 5; no *= dfloat; return no; }
29
Exercise
Write a function name get_Sum that read two integer number from user and return the sum of these two number to the main. Display the sum at the main function.
30
Exercise
Called by
Receives Returns
: main
: none : none
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();
32
#include <stdio.h> void printOne(int x); void printOne(int x){ int main(){ int a = 5; printOne(a); } printf(%d\n,x); return; return 0; }
a = 33; printOne(a);
Exercise
33
void main()
NESTED FUNCTION
34
35
36
The number and type of arguments passed to the called function must agree with the number and type of parameters in the called function.
function(arg1, arg2, arg3)
function(par1, par2, par3)
Calling function
Called function
37
Exercise
Write a function name get_Sum that receive two integer number from main. get_Sum will add these two number and return the sum of these two number back to the main. Display the sum at main.
38
Exercise
Write a function name display_Age that receive age from main. display_Age will display age and return nothing to main function.
Exercise
39
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
Exercise
40
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
42
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(); } void addcon(int *px, int *py) { *px=*px + 10;
*py=*py + 10;
}
44
45
Example
#define n 5
void main() { int get_total(int *, int); // function prototype int total, y[n] = {1,2,3,4,5};
// function call
int get_total(int *ptr , int x) // function definition { int i, total=0; for (i=0;i<x; i++) { total = total + *(ptr + i); } return total; }
46
47
When only copies of arguments are passed to a function, this is known as passing arguments by value (or passing by copy).
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); printf("\nNumber in main is %d",number); modify_Number(number); printf("\n\nNumber in main is %d",number); getch(); }
return ;
49
50
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); integer_swap(&x,&y); printf("\nSwapped values of x and y are %d %d\n",x,y); getch(); } *x=*y; *y=temp; } {
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.
53
Global Variable
It is not recommended to use global variable because the reader of the program does not immediately know which values are being received by the called function and which values are being returned by it.
54
Built-in Function
C provides a set of commonly used function that programmers can use without having to write any code for them. These function are called built-in, predefined or standard library function. The built-in function are stored in header file. Each header file (file with extension .h) contains functions that are related to a particular application.
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
56
Built-in Function
textcolor
57
Built-in Function
sqrt
58
Tips
Writing a C program, it is best NOT to sit down at the keyboard and start typing. THINK first about the program & what it is supposed to do. The BEST ways: start with the overall goal & divide this goal into several smaller tasks.