show_functions

# show_functions - CHAPTER 5 Programming with Functions...

This preview shows pages 1–8. Sign up to view the full content.

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CHAPTER 5 Programming with Functions March 28, 2008 Motivation Why Functions? ◮ Provides re-usability of common or often used algorithms (such as prompting a user for input). ◮ Let’s you maintain “algorithm focus” — don’t clutter up any function (specifically main() ) with “little bits of logic” that aren’t intrinsic to the task at hand. ◮ Allows you to provide a single, explicit interface to data or algorithms specific to your application. For instance, an interface for calculating specialized equations, access to a laboratory device, or access to a database of collected data, . . . Motivation f ( x ) ∼ double f( double x ) ◮ Conceptual idea similar to the f ( x ) you know. ◮ Functional composition: f ( sin ( y ) , cos ( y )) ◮ Recursion: a n = a n- 2 + a n- 1 . Function Anatomy Types of Functions User-Defined Function A function you have written. Library Function Function you use but didn’t write. Library functions come in many different forms: commercial libraries, FOSS libraries, system libraries, your partner’s library. main() is a user-defined function provided for every application. Function Anatomy Parts of a Function 1 / ** 2 * Main f u n c t i o n comment d e s c r i b e s what t h e f u n c t i o n does , s p e c i a l p r o p e r t i e s 3 * o f i t s arguments , what i t r e t u r n s , and i t s r e t u r n i n t e r p r e t a t i o n . 4 * / 5 r e t u r n _ t y p e f u n ct i o n _ n am e ( a r g 1 _ t y p e arg1_name , a r g 2 _ t y p e arg2_name ) 6 { 7 v a r i a b l e d e c l a r a t i o n s ; 8 9 f u n c t i o n s t a t e m e n t s ; 10 11 r e t u r n v a l u e ; 12 } ◮ All _name s must be valid identifiers ( A-Za-z_1-90 ). ◮ Each _type is a variable or class type ( int , double , ifstream ). ◮ function definition = The whole thing. ◮ function body = { variable declarations → return value; } ◮ function header = function definition- function body Function Anatomy Parts of a Function 2 / ** 3 * C a l c u l a t e t h e sq u a re r o o t o f a number . 4 * / 5 d o u b l e s q r t ( d o u b l e x ) 6 { 7 d o u b l e answer = _ _ m a c h i n e _ s p e c i f i c _ c o d e _ _ ; 8 r e t u r n answer ; 9 } ◮ The type of value returned matches the return_type . ◮ The variable x is a formal parameter of the sqrt function. ◮ Everything between the () of the function header is called the parameter declarations or argument list . ◮ sqrt : System library functions are used to insulate programmers from machine dependencies. Function Anatomy Parts of a Function 4 / ** 5 * P r i n t a " Hel l o World " message t o c o u t . 6 * / 7 v o i d s a y _ h e l l o _ w o r l d ( v o i d ) 8 { 9 c o u t << "Hello world , I’m a function!" << e n d l ; 10 r e t u r n ; 11 } ◮ The void as a return type indicates no return value....
View Full Document

{[ snackBarMessage ]}

### Page1 / 34

show_functions - CHAPTER 5 Programming with Functions...

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

View Full Document
Ask a homework question - tutors are online