Function Part 1 PDF
Document Details
Uploaded by BestKnownHeather
Tags
Summary
This document provides a lecture on functions in C++. It covers learning outcomes, modular programming, different types of functions, function calls, and local/global variables.
Full Transcript
Learning Outcomes Modular Programming Complex problems can be broken down into sub tasks, each of...
Learning Outcomes Modular Programming Complex problems can be broken down into sub tasks, each of which is easy to implement in C++. At the end of this lecture, you should be able to: Modular programming: breaking a program up into smaller, explain the benefits of modular programming manageable functions or modules. Function describe the concept of a function Function: a block of statements in a program to perform a specific task. identify types of function Part 1 define function, declare function and call function Motivation for modular programming: Multiple programmers : each sub task, is easy to be implemented use actual parameter and formal parameter by a programmer. differentiate function that returns a value and Easier readability, testing and maintenance : program is read, function that does not return a value tested and maintained based on its specific function. identify local and global variables and their scope Reduce duplication of code : functions can be called/used repeatedly in a program, simplifies the process of writing programs. 2 LECTURE 6 3 LECTURE 6 4 LECTURE 6 Function Concept Function Concept Types of Function Functions in a C++ program A function is to : There are 2 types of function : ✓ receive zero or more data Standard Library (or Pre-defined) Function Programmer defined Function ✓ operate on the data; or just produce side effect (output); or both In C++, a program contains numerous functions : ✓ return at most one data value. main() : is a programmer defined function; it is a must and Function definition: statements in program that describe can only be one in a program. how a function will do its task. main() often call/use standard library functions. Function call: statement in program that causes a function main() can also call/use other programmer defined to execute, in order to get its task done. functions. A programmer defined function, can also call other programmer defined functions. 5 LECTURE 6 6 LECTURE 6 7 LECTURE 6 8 LECTURE 6 Standard library / Pre-defined function Standard Library Function Standard Library Function Some functions defined in : Pre-defined Functions – Program eg. Example : some of the pre-defined mathematical Function Explanation Example #include Its function definition has been coded and kept in a file functions in are: log( x ) Natural logarithm of x (base log( 2.718282 ) is 1.0 #include called header file. of e) ✓ The power function, pow(x,y) using namespace std; Programmer just need to know: ✓ The square root function, sqrt(x) log10( x ) Logarithm for x (based of 10) log10( 100.0 ) is 2.0 int main() ✓ what the pre-defined function will do, ✓ The floor function, floor(x) pow( x, y ) x raised to the power y pow(2.0,7) is 128 { pow(9, 0.5) is 3 double angle; ✓ how to call it, To make use of the function, programmer has to include the sin( x ) Trigonometric value of sine sin(0.0) is 0 cout > angle; example : #include sqrt( x ) Non-negative square root of x sqrt(400.00) is 20.0 cout