1 / 20

Department of Information Technology

Department of Information Technology. Computer Programming & Problem Solving Ch 3 Functions. Outline. Introduction Program Modules in C Math Library Functions Functions Function Definitions Function Prototypes . Objectives. In this chapter, you will learn:

beulah
Télécharger la présentation

Department of Information Technology

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Department of Information Technology Computer Programming & Problem Solving Ch 3 Functions KIC/Computer Programming & Problem Solving

  2. Outline • Introduction • Program Modules in C • Math Library Functions • Functions • Function Definitions • Function Prototypes KIC/Computer Programming & Problem Solving

  3. Objectives • In this chapter, you will learn: • To understand how to construct programs modularly from small pieces called functions.. • To introduce the common math functions available in the C standard library. • To be able to create new functions. • To understand the mechanisms used to pass information between functions. KIC/Computer Programming & Problem Solving

  4. Main program Function 1 Function 2 Function 3 Function 5 Function 4 Introduction Divide and Conquer • Construct a program from smaller pieces or components • These smaller pieces are called modules • Each piece more manageable than the original program KIC/Computer Programming & Problem Solving

  5. Program Modules in Ca • Functions • Modules in C • Programs combine user-defined functions with library functions • C standard library has a wide variety of functions • Function calls • Invoking functions • Provide function name and arguments (data) • Function performs operations or manipulations • Function returns results • Function call analogy: • Boss asks worker to complete task • Worker gets information, does task, returns result • Information hiding: boss does not know details KIC/Computer Programming & Problem Solving

  6. Program Modules in C Fig. 5.1 Hierarchical boss function/worker function relationship. KIC/Computer Programming & Problem Solving

  7. Math Library Functions • Math library functions • perform common mathematical calculations • #include <math.h> • Format for calling functions • FunctionName( argument ); • If multiple arguments, use comma-separated list • printf( "%.2f", sqrt( 900.0 ) ); • Calls function sqrt, which returns the square root of its argument • All math functions return data type double • Arguments may be constants, variables, or expressions KIC/Computer Programming & Problem Solving

  8. KIC/Computer Programming & Problem Solving

  9. Functions • Functions • Modularize a program • All variables defined inside functions are local variables • Known only in function defined • Parameters • Communicate information between functions • Local variables • Benefits of functions • Divide and conquer • Manageable program development • Software reusability • Use existing functions as building blocks for new programs • Abstraction - hide internal details (library functions) • Avoid code repetition KIC/Computer Programming & Problem Solving

  10. Function Definitions Function definition format return-value-type function-name( parameter-list ){ declarations and statements} • Function-name: any valid identifier • Return-value-type: data type of the result (default int) • void– indicates that the function returns nothing • Parameter-list: comma separated list, declares parameters • A type must be listed explicitly for each parameter unless, the parameter is of type int KIC/Computer Programming & Problem Solving

  11. Function Definitions Data typeFunction_name (Passing_Data); • Definitions and statements: function body (block) • Variables can be defined inside blocks (can be nested) • Functions can not be defined inside other functions • Returning control • If nothing returned • return; • or, until reaches right brace • If something returned • returnexpression; KIC/Computer Programming & Problem Solving

  12. Function Definitions Return_Data_TypeFunction_Name(Passing_Data) { Statment 1; Statment 2; Statment 3; return Val; } void print() { printf(“Build_Functions”); }

  13. KIC/Computer Programming & Problem Solving

  14. KIC/Computer Programming & Problem Solving

  15. KIC/Computer Programming & Problem Solving

  16. KIC/Computer Programming & Problem Solving

  17. Function Prototypes • Function prototype • Function name • Parameters – what the function takes in • Return type – data type function returns (default int) • Used to validate functions • Prototype only needed if function definition comes after use in program • The function with the prototype int maximum( int x, int y, int z ); • Takes in 3 ints • Returns an int • Promotion rules and conversions • Converting to lower types can lead to errors KIC/Computer Programming & Problem Solving

  18. Function Prototypes KIC/Computer Programming & Problem Solving

  19. ExampleWrite a program that prints the following consider output numbers resulting from the mathematical relationship KIC/Computer Programming & Problem Solving

  20. Exampleoutput program KIC/Computer Programming & Problem Solving

More Related