Вы находитесь на странице: 1из 21

Lecture 5: Modular

Programming (functions
part 1
BJ Furman
27FEB212
Learning !"#ecti$es

E%plain t&e concept of modular program design

E%plain t&e concept of a function in '

E%plain (&) functions are important in programming

E%plain t&e structure of a function

*eturn data t)pe

Parameters

+ppl) t&e concept of a function to a practical pro"lem

E%plain &o( larger ' programs s&ould "e structured


using ,& and ,c files
Modular Programming

Brea- a large pro"lem into smaller pieces

.maller pieces sometimes called /modules0 or


/su"routines0 or /procedures0 or functions

1&)2

3elps manage comple%it)

.maller "loc-s of code

Easier to read

Encourages re4use of code

1it&in a particular program or across different programs

+llo(s independent de$elopment of code

Pro$ides a la)er of /a"straction0


a = sqrt(9.0);
Functions

5&e /"uilding "loc-s0 of a ' program

6ou0$e used predefined functions alread):

main(7

printf(78 scanf(78 po((7

9ser4defined functions

6our o(n code

:n com"ination (it& predefined functions


Functions 4 Mat&ematical ;ie(
3 2 ) (
2
+ + =

x x x f
11 is ) 2 (
11 3 4 4 3 ) 2 ( 2 ) 2 ( ) 2 (
2
f
f

+ + + +
f(2)? is What
) ( x f
2 11
X
Function
Returned
value
Functions 4 <efinition .tructure

Function =&eader=

*eturn data t)pe


(if an)7

>ame

<escripti$e

+rguments (or
parameter list7

>otice: data t)pe and


name

.tatements

;aria"le declaration

!perations

*eturn $alue (if an)7


type function_name (type arg1, type arg2 )

state!ents?
"
double product(double x, double y)
{
double result;
result = x * y;
return result;
}
+ function t&at calculates t&e product of t(o num"ers
Functions 4 E%ample

Function prototype

Li-e a $aria"le declaration

5ells compiler t&at t&e function (ill


"e defined later

3elps detect program errors

>ote semicolon@@

Function definition

.ee pre$ious slide

>ote8 >! semicolon

Function return

return statement terminates


e%ecution of t&e current function

'ontrol returns to t&e calling


function

if return expression;

t&en $alue of expression is


returned as t&e $alue of t&e function
call

!nl) one $alue can "e returned t&is


(a)

Function call

main() is t&e =calling function=

product() is t&e =called function=

'ontrol transferred to t&e function


code

'ode in function definition is


e%ecuted
#include <stdio.h>
* !unction prototype *
double product(double x, double y);
int main()
{
double "ar# = $.%, "ar& = '.%;
double ans;
ans = product("ar#, "ar&);
print!(("ar# = ).&!*n(
("ar& = ).&!*n(,"ar#,"ar&);
print!(("ar#*"ar& = )+*n(, ans);
}
* !unction de!inition *
double product(double x, double y)
{
double result;
result = x * y;
return result;
}
Function 4 Practice 1

1rite a function
named =sum=

sums t(o
integers

returns t&e sum

2 min, on )our
o(n

.&are (it&
neig&"or
#teps
1. Function header
A return data t)pe
A function name
A argument list (it& data t)pes
2. Statements in function definition
A $aria"le declaration
A operations
A return $alue
Function 4 sum(7
int sum,int(int x, int y)
{
int result;
result = x - y;
return result;
}
Functions t&at do not return a $alue

9se t&e return t)pe of $oid

$oid m)Bfun( argBlist8C7

Practice

1rite t(o functions8 t&e first prints out first


name8 and t&e second prints out last name
Function 4 Practice 2

Program to print out t(o


&app) :7 :7 or sad faces :(
:(

'ontinuousl) prompts for


user input:

7 for &app) face

( for sad face

Duits if =E= or =D= entered

calls t(o functions

&app)Bface(7

sadBface(7

1or- in pairs

Pseudocode $irst%%

<i$ide tas-s of (riting


t&e t(o functions
#teps
1. Pseudocode for program logic
2. Function header
A return data t)pe (if an)7
A function name
A argument list (it& data t)pes (if an)7
3. Statements in function definition
A $aria"le declaration (if an)7
A operations
A return $alue
Program 4 Faces logic

Pseudocode
1, <eclare and initialiFe $aria"les
2, 13:LE user input not eEual to E +>< not eEual to D
17 .(itc& on user input to
27 'ase =7/:
call &app)Bface(7?
"rea-?
57 'ase =(/:
call sadBface(7?
"rea-?
G7 'ase /E0:
77 'ase /D0:
&rea';
H7 'ase /0:
I7 <efault:
re4prompt for user input
Program 4 Faces code
.tructuring ' Programs

ModulariFation

Brea-ing a program up into smaller pieces:

:nstead of:

oneB"igBprogram,c

"rea- into groupings of &eader files (,&7 and source code (,c7 files:

module,#.h

module,#.c

etc.

*ationale

separates t&e user-interface description (,&7 from t&e nitt)4


gritt) details of implementation (,c7

5&e +pplication Programming :nterface (+P:78 t&e ,& file8 is


distinct from t&e implementation8 t&e ,c file ((&ic& ma) alread)
"e compiled and not readil) $ie(ed7

E%ample: mat&,& from '&

can construct and test modules independentl)

promotes re4use of code


E%ample: mat&,& used in '&

.ee ':J '& J include J mat&,&

<eclaration of constants

#de!ine .,/0 $.#1#'2&3'$'4252$&$413

<eclaration of macro su"situtions

#de!ine is+reater(x, y) ((x)>(y))

<eclaration of glo"al $aria"les (caution@7

Function protot)pes

extern double sin(double x);

Pertinent comments
*e$ie(
.tructured Programming

+ll programs can "e (ritten using t&ese


control structures:

.eEuence

<ecision (t&ree structures7

:F

:F4EL.E

.1:5'3

*epetition (t&ree structures7

13:LE

<!413:LE

F!*
.tructure of a ' program

E%, !ree,!all,d,"s,time.c
' 'ode for <K< L,15c
(rogra!!er)s &loc'
(re*processor directive
+eclare and initiali,e
varia&les
-.ile loop
(repetition structure)
/ain $unction (state!ents go &et0een " )
return state!ent
+rit&metic (it& :ntegers and Mi%ed <ata 5)pes

+rit&metic (it& integers

*esult is an integer

1M1 44N 2

OJ2 44N 2

2JO 44N 2 BE '+*EF9L@@@

+rit&metic (it& mi%ed data t)pes

+utomatic con$ersion of operand so t&at data t)pes matc&

'on$ersion is /up(ard0 in t&e siFeof(7 sense

E%ample in '&
char a = 5;
si6eo!(a);
double b=$;
si6eo!(b);
print!((a-b == )l! and needs )d bytes*n (, a-b,
si6eo!(a-b));
*eferences

Modular Programming in '


&ttp:JJ(((,icosaedro,itJc4modules,&tml

mat&,&
&ttp:JJ(((,opengroup,orgJonlinepu"sJ7
IH7IIJ%s&Jmat&,&,&tml

Вам также может понравиться