Week 5: Modular Programming BJ Furman 21SEP2009 The Plan for Today Overview of modular programming Functions Top-down design Role of functions Function prototype Return data type Arguments Function Example Function Practice Learning Objectives Explain the concept of modular program design Explain the concept of a function in C Explain why functions are important in programming Explain the structure of a function Return data type Parameters Apply the concept of a function to a practical problem Modular Programming Break a large problem into smaller pieces Smaller pieces sometimes called ‘modules’ or ‘subroutines’ or ‘procedures’ or functions Why? Helps manage complexity Smaller blocks of code Easier to read Encourages re-use of code Within a particular program or across different programs Allows independent development of code Provides a layer of ‘abstraction’ a = sqrt(9.0); Functions The ‘building blocks’ of a C program You’ve used predefined functions already: main() printf(), scanf(), pow() User-defined functions Your own code In combination with predefined functions Functions - Mathematical Δ f ( x) = x 2 + 2 x + 3 What is f(2)? f (2) ⇒ (2) 2 + 2(2) + 3 ⇒ 4 + 4 + 3 ⇒ 11 ∴ f (2) is 11 X Function Returned value 2 f ( x) 11 Functions - Definition Structure Function 'header' Return data type (if any) Name Descriptive type function_name (type arg1, type arg1, ) { statements; } Arguments (or parameter list) Statements Variable declaration Operations Return value (if any) double product(double x, double y) { double result; result = x * y; return result; } Functions - Example Function prototype Like a variable declaration Function definition See previous slide Function call Tells compiler that the function will be defined later Helps detect program errors Note semicolon!! main() is the 'calling function' product() is the 'called function' Control transferred to the function code Code in function definition is executed Function return return statement terminates execution of the current function Control returns to the calling function if return expression; then value of expression is returned as the value of the function call Only one value can be returned this way #include <stdio.h> /* function prototype */ double product(double x, double y); int main() { double var1 = 3.0, var2 = 5.0; double ans; ans = product(var1, var2); printf("var1 = %.2f\n" "var2 = %.2f\n",var1,var2); printf("var1*var2 = %g\n", ans); } /* function definition */ double product(double x, double y) { double result; result = x * y; return result; } Function - Practice 1 Write a function named 'sum' sums two integers returns the sum 2 min. on your own Share with neighbor Steps 1. Function header • return data type • function name • argument list with data types 2. Statements in function definition • variable declaration • operations • return value Function - sum() int sum(int x, int y) { int result; result = x + y; return result; } Function - Practice 2 Program to print out two happy :) :) or sad faces :( :( Continuously prompts for user input: calls two functions ) for happy face ( for sad face Quits if 'q' or 'Q' entered happy_face() sad_face() Work in pairs Pseudocode first!! Divide tasks of writing the two functions Steps 1. Pseudocode for program logic 2. Function header • return data type (if any) • function name • argument list with data types (if any) 3. Statements in function definition • variable declaration (if any) • operations • return value Review References