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

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

View Full Document Right Arrow Icon
1 Parameters 2 Repetitive figures s Consider the task of drawing the following figures: ************* ******* *********************************** ********** * * ********** ***** * * * * ***** s 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(); } s Observation : Functions are redundant. s Would constants help us solve this problem? 4 Generalizing functions s What if we had the following? b DrawLine - A function to draw a line of any number of stars. b DrawBox - A function to draw a box of any size. main DrawLine ******* 7 DrawLine ************* 13 5 Parameterization s parameterized function : A function that is given extra information (e.g. number of stars to draw) when it is called. s parameter : A value passed to a function by its caller. s To use a parameterized function: b define it s Write a function that accepts parameters b call it s Pass the parameter values desired to the function 6 Defining parameterized functions s Parameterized function definition syntax: void <name> ( <type> <name> ) { <statement(s)> ; } s The scope of the parameter is the entire function. s Example: void PrintSpaces( int count ) { for (int i = 1; i <= count ; i++) { printf(" "); } } b Whenever PrintSpaces is called, the caller must specify how many spaces to print. count
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/05/2010 for the course CSE cse142 taught by Professor Benson during the Spring '10 term at Punjab Engineering College.

Page1 / 4

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

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

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