Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015
Learning Objectives Introduce more C Discuss the importance of style in code 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
Announcements Homework #5 due in two weeks Midterm in lab this week
Compound assignments C has many assignment operators which perform an operation and then store the results value += 5; Same as value = value + 5; value *= 10; value = value * 5;
All compound assignments += (addition) -= (subtraction) *= (multiplication) /= (division) %= (modulo division) &= (bitwise and) |= (bitwise or) ^= (bitwise xor) <<= (left shift) >>= (right shift)
Increment and decrement value++; similar to value = value + 1, increments after returning value ++value; similar to value = value + 1, increments before returning value value--; similar to value = value – 1; --value;
Programming Style The layout of program directives using white space, syntax, & punctuation to visually highlight a programmer’s intention Good Style is Consistent – A given structure always looks the same Illustrative – The intent of the code is shown by the grammar Clear – Any reasonable programmer should be able to follow the intent
Programming Style Why does it matter? Reduces bugs – the human eye is excellent at detecting patterns Reduces development time Allows others to understand how to approach your code Style is a fundamental part of code documentation
#include // Programmer: B. Burlingame // Program: Good_form.c double square( double x ); // Version: 1.0 // int main( void ) // Prints the area of a square { double side = 0.0; // Stores the length of the side double area = 0.0; // Stores the area of the square char buffer[100] = ""; // Input buffer printf( "Enter the side of a square: " ); fgets( buffer, sizeof(buffer), stdin ); sscanf( buffer, "%lf", &side ); // Obtain the length of a side area = square( side ); // Call square function printf( "The area is %.3lf.\n", area ); // Display the area to the screen return 0; // Return 0 to the OS } ////////////////////////////////////////////////////////////////////////////// double square( double x ) // Return x squared { return( x * x ); }
#include double square( double x ); int main( void ) { double side = 0.0, area = 0.0; char buffer[100] = ""; printf( "Enter the side of a square: " ); fgets( buffer, sizeof(buffer), stdin ); sscanf( buffer, "%lf", &side ); area = square( side ); printf( "The area is %.3lf.\n", area ); return 0; } double square( double x ) { return( x * x ); }
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(), sscanf(), pow() User-defined functions Your own code In combination with predefined functions
Functions - Mathematical View X Function Returned value
Flowchart notation
Functions - Definition Structure Function 'header' Return data type (if any) Name Descriptive Arguments (or parameter list) Notice: data type and name Statements Variable declaration Operations Return value (if any) type function_name (type arg1, type arg2 ) { statements; } double product(double x, double y) { double result; result = x * y; return result; } A function that calculates the product of two numbers
Functions - Example Function prototype Like a variable declaration Tells compiler that the function will be defined later Helps detect program errors Note semicolon!! Function definition See previous slide Note, NO semicolon 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 Function call main() is the 'calling function' product() is the 'called function' Control transferred to the function code Code in function definition is executed #include /* 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(int x, int y) { int result; result = x + y; return result; }
Passing Arguments into Functions How are the arguments passed into functions? 'Pass by value' function arguments are expressions In the function call: Expressions are evaluated and copies of their values are put into temporary memory locations The names of the corresponding parameters in the function definition are made to be the names of the copies The values of the expressions in the function call are not changed #include double product(double x, double y); int main() { int a = 10; 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 A, double B) { double result; result = A * B; return result; }
Identifiers and Scope Identifier The name of a variable, function, label, etc. int my_var1;/* a variable */ pow_table();/* a function */ start:/* a label */ Question: Does it make a difference where in a program an identifier is declared? YES! --> concept of ‘scope’
Scope of Identifiers Scope of a declaration of an identifier The region of the program that the declaration is active (i.e., can access the variable, function, label, etc.) Five types of scope: Program (global scope) File Function prototype Function Block (“between the { } scope”)
Scope of Identifiers - Program (Global) Scope Program (global) scope if declared outside of all functions "Visible" to all functions from point of declaration Visible to functions in other source files Use only when necessary and then very carefully!! ex. from Ch var_scope.c #include int a = 10; 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 scope Applies only to labels start: * goto start; Active from the beginning to the end of a function Ex. Statement labels in a switch selection structure Scope of Identifiers - Function Scope #include int main() { int user_sel; /* prompt user for entry */ /* get user entry */ switch( user_sel ) { case 1: printf("\n message..."); /* call game function1 here */ break; case 2: printf("\n message..."); /* call game function2 here */ break; default: printf("Error"); break; }
Scope of Identifiers - Block Scope Block (local) scope A block is a series of statements enclosed in braces { } The identifier scope is active from the point of declaration to the end of the block ( } ) Nested blocks can both declare the same variable name and not interfere ex. from Ch var_scope_block.c scope_nested_blocks.c #include double product(double x, double y); int main() { int a = 10; 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; }
Storage Duration How long the identifier exists in memory Static storage class Identifier exists when program execution begins For variables: Storage allocated and variable is initialized once Retains their values throughout the execution of the program #include void just_count(void); /* proto */ int main() { int i; for(i=0;i<10;i++) { just_count(); } return 0; } void just_count(void) { static int count_a; int count_b; count_a = count_a + 1; count_b = count_b + 1; printf("count_a== %d\n", count_a); printf("count_b== %d\n", count_b); } just_count.c
For functions: function name exists when execution begins For variables with global scope: i.e., declared outside of all functions and uses static keyword "Visible" to all functions from point of declaration in this source file only Keeps data ‘private’ to this file only Storage Duration, cont. #include static int a = 10; 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; }
References Modular Programming in C math.h /xsh/math.h.html /xsh/math.h.html e.html e.html