lecture 5 functions pt1

21
Lecture 5: Modular Programming (functions  – part 1 BJ Furman 27FEB212

Upload: harshit-kapadia

Post on 02-Jun-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 1/21

Lecture 5: Modular

Programming (functions – part 1

BJ Furman27FEB212

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 2/21

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 structuredusing ,& and ,c files

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 3/21

Modular Programming

Brea- a large pro"lem into smaller pieces .maller pieces sometimes called /modules0 or

/su"routines0 or /procedures0 or functions

&) 3elps manage comple%it)

.maller "loc-s of code

Easier to read

Encourages re4use of code it&in a particular program or across different programs

 +llos independent de$elopment of code

Pro$ides a la)er of /a"straction0 a = sqrt(9.0);

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 4/21

Functions

&e /"uilding "loc-s0 of a ' program 6ou0$e used predefined functions alread):

main(

printf(8 scanf(8 po(

9ser4defined functions 6our on code

n com"ination it& predefined functions

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 5/21

Functions 4 Mat&ematical ;ie

32)( 2++=

 x  x  x  f 

11is)2(

113443)2(2)2()2( 2

 f  

 f  

⇒++⇒++⇒

f(2)?isWhat

)( x  f 2 11X Function

Returned

value

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 6/21

Functions 4 <efinition .tructure

Function =&eader= *eturn data t)pe

(if an) >ame

<escripti$e  +rguments (or

parameter list >otice: data t)pe and

name

.tatements

;aria"le declaration !perations *eturn $alue (if an)

type function_name (type arg1, type arg2 )

state!ents?

"

 double product(double x, double y)

 {

  double result;  result = x * y;  return result; }

 + function t&a

t calculates t&e product  of to num"ers

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 7/21

Functions 4 E%ample

Function prototype Li-e a $aria"le declaration ells 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 isreturned 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 ise%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;

 }

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 8/21

Function 4 Practice 1

rite a functionnamed =sum= sums to

integers returns t&e sum

2 min, on )ouron

.&are it&neig&"or 

#teps1. Function header 

A return data t)peA function nameA argument list it& data t)pes

2. Statements in function definition

A $aria"le declarationA operationsA return $alue

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 9/21

Function 4 sum(

 int sumint(int x, int y)

 {  int result;

  result = x - y;  return result; }

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 10/21

Functions t&at do not return a $alue

9se t&e return t)pe of $oid $oid m)fun( arglist8C

Practice rite to functions8 t&e first prints out first

name8 and t&e second prints out last name

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 11/21

Function 4 Practice 2

Program to print out to&app) : : or sad faces :(:( 'ontinuousl) prompts for

user input: for &app) face ( for sad face Duits if == or =D= entered

calls to functions &app)face( sadface(

or- in pairs Pseudocode $irst%% <i$ide tas-s of riting

t&e to functions

#teps1. Pseudocode for program logic 

2. Function headerA return data t)pe (if an)A function nameA argument list it& data t)pes (if an)

3. Statements in function definitionA $aria"le declaration (if an)A operationsA return $alue

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 12/21

Program 4 Faces logic

Pseudocode1, <eclare and initialie $aria"les

2, 3LE user input not eual to +>< not eual to D

1 .itc& on user input to

2 'ase =/:

call &app)face(?

"rea-?

5 'ase =(/:

call sadface(?

"rea-?

G 'ase /0:

7 'ase /D0:&rea';

H 'ase /0:

I <efault:

re4prompt for user input

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 13/21

Program 4 Faces code

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 14/21

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 15/21

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

.ee ': '& include 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@

Function protot)pes extern double sin(double x);

Pertinent comments

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 16/21

*e$ie

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 17/21

.tructured Programming

 +ll programs can "e ritten using t&esecontrol structures: .euence

<ecision (t&ree structures F F4EL.E .'3

*epetition (t&ree structures 3LE <!43LE F!*

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 18/21

.tructure of a ' program

E%, !ree!alld"stime.c

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 19/21

' 'ode for <K< ,15c

rogra!!ers &loc'

re*processor directive

+eclare and initialie

varia&les

-ile loop

(repetition structure)

/ain $unction (state!ents go &eteen " )

return state!ent

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 20/21

 +rit&metic it& ntegers and Mi%ed <ata )pes

 +rit&metic it& integers *esult is an integer 

11 44N 2 O2 44N 2 2O 44N 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 /upard0 in t&e sieof( sense E%ample in '&

char a = 5;

si6eo!(a);

double b=$;

si6eo!(b);

 print!(a-b == l! and needs d bytesn , a-b,si6eo!(a-b));

8/10/2019 Lecture 5 Functions Pt1

http://slidepdf.com/reader/full/lecture-5-functions-pt1 21/21

*eferences

Modular Programming in '

&ttp:,icosaedro,itc4modules,&tml

mat&,&

&ttp:,opengroup,orgonlinepu"s7

IH7II%s&mat&,&,&tml