12-Functions.pptx - CMPSC 201 Lecture 12 Programmer-defined...

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

View Full Document Right Arrow Icon
CMPSC 201 Lecture 12 Programmer-defined functions Book 5.2
Image of page 1

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

View Full Document Right Arrow Icon
Objectives Be able to understand function protocols, function calls, and function definition. Be able to use function calls correctly. Be able to tell the difference between functions that return a value and those that do not. Martin K.-C. Yeh 2
Image of page 2
Functions Functions are a collection of statements that perform a specific task. C ++ has built-in functions, e.g.: pow( ) , sqrt() , abs( ) , sin( ) C++ also allows programmer-defined functions. Using functions helps organize code, make it easier to port from program to program – modular programming . Can call the same function from many places in the program. Martin K.-C. Yeh 3
Image of page 3

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

View Full Document Right Arrow Icon
Top Down Design To write a program Develop the algorithm that the program will use Translate the algorithm into the programming language Top down design (also called stepwise refinement) Break the algorithm into subtasks Break each subtask into smaller subtasks Eventually the smaller subtasks are trivial to implement in the programming language Martin K.-C. Yeh 4
Image of page 4
Benefits of Top Down Design Subtasks, or functions in C++, make programs: Easier to understand when using meaningful function names Easier to change Easier to write Easier to test Easier to debug Easier for teams to develop Martin K.-C. Yeh 5
Image of page 5

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

View Full Document Right Arrow Icon
Predefined Functions C++ comes with libraries of predefined functions Example: sqrt function theRoot = sqrt(9.0); returns , or computes, the square root of a number The number, 9, is called the argument theRoot will contain 3.0 Martin K.-C. Yeh 6
Image of page 6
Employing Functions To use functions, you will need to have 3 items: Function Declaration ( Prototype ) – identifies the function name and requirements. Occurs before main() so the name is globally defined. Function Call – statement that directs control of the program to that function. Function Definition – code for the specific task. (In this course function definitions will occur after main) Martin K.-C. Yeh 7
Image of page 7

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

View Full Document Right Arrow Icon
Function Definitions The function definition specifies what the function does
Image of page 8
Image of page 9
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