day04 - Parameters 1 Repetitive figures Consider the task...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
    1 Parameters
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
    2 Repetitive figures Consider the task of drawing the following figures: ************* ******* *********************************** ********** * * ********** ***** * * * * ***** The lines and figures are similar, but not exactly the same.
Background image of page 2
    3 A solution? void DrawLineOf13Stars() { for (int i = 1; i <= 13 ; i++) { printf("*"); } printf("\n"); } void DrawLineOf7Stars() { for (int i = 1; i <= 7 ; i++) { printf("*"); } printf("\n"); } void DrawLineOf35Stars() { for (int i = 1; i <= 35 ; i++) { printf("*"); } printf("\n"); } ... int main() { DrawLineOf13Stars(); DrawLineOf7Stars(); DrawLineOf35Stars(); Draw10x3Box(); Draw5x4Box(); } Observation : Functions are redundant. Would constants help us solve this problem?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
    4 Generalizing functions What if we had the following? DrawLine - A function to draw a line of any number of stars. DrawBox - A function to draw a box of any size. main DrawLine ******* 7 DrawLine ************* 13
Background image of page 4
    5 Parameterization parameterized function : A function that is given extra information (e.g. number of stars to draw) when it is called. parameter : A value passed to a function by its caller. To use a parameterized function: define it Write a function that accepts parameters call it Pass the parameter values desired to the function
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
    6 Defining parameterized functions Parameterized function definition syntax: void <name> ( <type> <name> ) { <statement(s)> ; } The scope of the parameter is the entire function. Example: void PrintSpaces( int count ) { for (int i = 1; i <= count ; i++) { printf(" "); } } Whenever PrintSpaces is called, the caller must specify how many spaces to print.
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 21

day04 - Parameters 1 Repetitive figures Consider the task...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online