Functions Lecture 5: C++
Document Details
Tags
Summary
This lecture introduces functions in the C++ programming language. It describes how functions are used to divide and conquer programming tasks, and how they are a crucial part of organizing and managing complex algorithms.
Full Transcript
Functions 3 Introduction Divide and conquer – Construct a program from smaller pieces or components – Each piece more manageable than the original program 3.2 Program Components in C++ Programs written by – combining new functions with “prepackaged” functions...
Functions 3 Introduction Divide and conquer – Construct a program from smaller pieces or components – Each piece more manageable than the original program 3.2 Program Components in C++ Programs written by – combining new functions with “prepackaged” functions in the C++ standard library. – new classes with “prepackaged” classes. The standard library provides a rich collection of functions. Functions are invoked by a function call – A function call specifies the function name and provides information (as arguments) that the called function needs – Boss to worker analogy: A boss (the calling function or caller) asks a worker (the called function) to perform a task and return (i.e., report back) the results when the task is done. 3.2 Program Components in C++ Function definitions – Only written once – These statements are hidden from other functions. – Boss to worker analogy: The boss does not know how the worker gets the job done; he just wants it done 3.3 Math Library Functions Math library functions – Allow the programmer to perform common mathematical calculations – Are used by including the header file Functions called by writing functionName (argument) Example cout c; 17 18 // a, b and c below are arguments to 19 // the maximum function call 20 cout