①
APS105 Lecture II
Last lare : for loops & Nested loops
Today : I nested loops example and functions
Nested Hops example
How about this pattern ?
239
-
-
I Istrow 3 I star "
spaces
:
:
2 Indrow [
:
spaces 2 stars : ↑
3
** * 3rd now :
/ spaces 3 stars = 4
↑ * * * * 4th row :
Ospaces 4 stars = 4
I W W
↓
this line is row + col = 4 4-row row #
so if col is 4-row print space
else print
"
*
"
for (int row = 1
;
row c = 4 ; now ++ )&
for(int col = 1 ; col = 4
; col
+ + )
if (col <= -row) !
printf("");
Jelses
print (" * ") ;
3
3
②
Functions : Break soare into
manageable pieces
-
Want to build a complex product ? It is difficult
to handle all at once .
It is better to break into pieces and solve assemble
pieces ikea
e
g.
.
In software , separate pieces are called functions
(or subroutines , modules
, procedures) -
Modularity
We can divide the work different
among
developers ,
each will create separate
a piece/function
e .
g. rand() , printf() ,
scanfl) , vint()
~ No need to it!
implement print every time
you use
~ Can test each function alone
↓ Avoid repetition of code >
- make less errors.
① Print Stars
function variable type input
name
imput variable name
↑ ↑ -
output
voidPinstant
sta Sta ;
col ++ )5
type ("
3
print * ") ;
printf("In") ;
3
③
-DEMO Let's call functions , recall pattern printing :
A
* A
* * *
* * AX
We decomposed the problem into 2 sources of
repetition 0 lines
:
Printing -
bigger problem
② Printing stars -
small problem
Let's create a function for each problem :
Begin with smaller problems -
:
print stars
function variable type input
name
input variable name
↑ ↑ -
printstas(t
void
I
sta esta col
>
-
①
·
output -
↓
++ )5
type ("
3
print * ") ;
Printf("In") ;
3
Bigger problem :
print lines
① - void print Pattern (int num Of Rows) [
for (int row = 1
;
row <= nunOf Rows ; now ++ )5
Order
of print Stars ( 1;
implementation 3
mains
before
int (void) &
function % main
matters print Pattern (4) ; main calls
4
point Pattern
to
and
Of Rows
return O ;
passes num
3
Order of exation :
① Main is
always called 1st
② Functions can be
(i) implement before main as in previous example OR
(ii) fruction written
of
prototype
-
before main
,
but implemented
after
↓ main
(i) retur
type , (ii) function name
, (iii) in put types
# include < staio h . >
print Stars (int)
I Prototypes
void ; Function
void print Pattern (int) ;
int main (void) [
print Pattern (4) ;
return 0 ;
void printstars (int numOfstas)[
I
for (int col = 1 col = numOfStars ; 201 ++ )3
;
Function printf(" * ") ;
Implementation 3 printf("In") ;
void print Pattern (int num Of Rows)
for (int row = 1
; row <= numotRows ; vow ++ )3
print Stars (row) ;
3
3
⑤
When call function call by passing the
·
you a
,
variable name (not type)
·
parameter passed to a function will take name
of parameter in function header
of to function
· Order parameters passed a
matters.
Return non-vord type : int
,
double
,
bool , char
E. factorial ! nx(n 1) * ( 2) 3x2x/
g. n
: =
-
- ...
Write a function that takes ; a int and returns
it factorial .
① Toy example : 4= 4 * 3 *2 * )
② Think :
repeatedly multiply an
increasing
num
by
a
product .
③ Steps : ① Product =I
② set num I
③
⑨
product
Increase
=
product
num
by
*
1
num
] loop
⑤ Repeat 3-4 till num = n- dixed of
iterations
Use for loop !