lec04-program_design_and_functions

lec04-program_design_and_functions - CSC 1111 Introduction...

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

View Full Document Right Arrow Icon
1 CSC 1111 Introduction to Computing using C++ Program Design and Functions
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 Questions to be answered … We have seen how we can define a function in C++, but … Why do we need functions? What are the advantages of using functions? How to use functions to help organizing codes in a program? How to utilize functions in program design?
Background image of page 2
3 Why functions? Reusability Speedup software development Modularity Make codes more organized Make codes easy to maintain Make codes easy to debug
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 void foo() { statement_A; statement_B; statement_A; statement_B; statement_A; statement_B; } void bar() { statement_A; statement_B; } void foo() { bar(); bar(); bar(); } Code Reuse: Copy/Paste vs Functions Functions (Pro) Reduce the amount of codes in a program Ease of maintenance e.g.: If we want to change statement_B , we only need to make one change in the function. Functions (Con) Overhead of defining functions
Background image of page 4
5 Functions with Parameters (Same block of codes but different data) class Date { public : int dd, mm, yy; }; int main () { Date date1 = { 1 , 7 , 1997 }, date2 = { 1 , 1 , 2000 }; // Print date as "dd-mm-yy" cout << date1 .dd << "-" << date1 .mm << "-" << date1 .yy << endl; cout << date2 .dd << "-" << date2 .mm << "-" << date2 .yy << endl; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // Print value of d in this // format: "dd-mm-yy" void printDate(Date d) { cout << d.dd << "-" << d.mm << "-" << d.yy; } int main () { Date date1 = { 1 , 7 , 1997 }, date2 = { 1 , 1 , 2000 }; printDate( date1 ); cout << endl; printDate( date2 ); cout << endl;
Background image of page 5

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

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

This note was uploaded on 12/28/2010 for the course CSC CSC1110 taught by Professor Cjyuan during the Fall '06 term at CUHK.

Page1 / 18

lec04-program_design_and_functions - CSC 1111 Introduction...

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

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