Department of Information Technology Computer Programming Problem Solving

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

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

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 3

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 Main program Function 1 Function 2 Function 5 Function 4 KIC/Computer Programming & Problem Solving Function 3 4

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 5

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

Math Library Functions • Math library functions – perform common mathematical calculations – #include <math. h> • Format for calling functions – Function. Name( argument ); • If multiple arguments, use comma-separated list – printf( "%. 2 f", 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 KIC/Computer Programming & Problem 7 Solving expressions

KIC/Computer Programming & Problem Solving 8

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 9

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 10

Function Definitions Data type Function_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 – return expression; 11 KIC/Computer Programming & Problem Solving

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

KIC/Computer Programming & Problem Solving 13

KIC/Computer Programming & Problem Solving 14

KIC/Computer Programming & Problem Solving 15

KIC/Computer Programming & Problem Solving 16

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 KIC/Computer & Problem – Converting to lower types. Programming can lead to errors Solving 17

Function Prototypes KIC/Computer Programming & Problem Solving 18

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

Example output program KIC/Computer Programming & Problem Solving 20
- Slides: 20