CP1_7a_Functions1_1112

CP1_7a_Functions1_1112 - Functions Functions 1 Intro and...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Functions 1 Intro and Predefined Functions
Image of page 1

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

View Full Document Right Arrow Icon
Breaking a task into subtasks So far, we've written our programs as a group of statements in main() {…} . If we structured large programs like this, their code would be very difficult to understand, test, debug, maintain… We might also have to duplicate sections of code at many points in the program where we needed the same or similar f ti lit functionality. In reality, we don't structure our programs like this. We use a simple but powerful approach to problem solving, which is to break a large problem into smaller sub-problems and then write small sub-programs to solve those. The sub-programs are then combined to solve the larger bl problem. 2 CSIS1117B Computer Programming 1 2010-2011
Image of page 2
The sub-programs The sub-programs we write to deal with some small part of the overall problem are combined by calling them from the main control paths of the program or from other sub- programs. A good sub-program has a single responsibility . It does one thi d d th t thi ll E thi l i k t t thing and does that thing well. Everything else is kept out of that sub-program. Almost all programming languages support this form of structuring. In C++, we write these building blocks in the form of functions . 3 CSIS1117B Computer Programming 1 2010-2011
Image of page 3

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

View Full Document Right Arrow Icon
What is a function in C++? A function is a small program which accomplishes a specific task. Code can call functions to help complete its own task For functions to help complete its own task. For example our code may pass input to a function and then use the output passed back (returned) when the function completes its task completes its task. As a first example, we can examine functions available in the C++ library These are predefined functions that are available C++ library. These are predefined functions that are available for use in our own programs and which make our life as programmers much easier.
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern