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

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

1 Parameters

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

View Full Document
2 Repetitive figures Consider the task of drawing the following figures: ************* ******* *********************************** ********** * * ********** ***** * * * * ***** The lines and figures are similar, but not exactly the same.
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?

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

View Full Document
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
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

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

View Full Document
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.
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 04/29/2009 for the course EE 142 taught by Professor Bensollimketkai during the Spring '09 term at Washington State University .

### 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
Ask a homework question - tutors are online