Lecture 5 Functions Pt1
Lecture 5 Functions Pt1
Programming (functions
– part 1
BJ Furman
27FEB2012
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
Explain how larger C programs should be structured
using .h and .c files
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
User-defined functions
Your own code
In combination with predefined functions
Functions - Mathematical View
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
Returned
X Function value
2 f ( x) 11
Functions - Definition Structure
Function 'header' type function_name (type arg1, type arg2 )
Return data type
(if any) {
Name statements;
Descriptive }
Arguments (or
parameter list)
Notice: data type and A function that calculates the product of two numbers
name
double product(double x, double y)
Statements
{
Variable declaration double result;
Operations
result = x * y;
Return value (if any)
return result;
}
Functions - Example
Function prototype #include <stdio.h>
Like a variable declaration
Tells compiler that the function will /* function prototype */
be defined later
double product(double x, double y);
Helps detect program errors
Note semicolon!!
int main()
Function definition {
See previous slide
Note, NO semicolon
double var1 = 3.0, var2 = 5.0;
Function return double ans;
return statement terminates ans = product(var1, var2);
execution of the current function
printf("var1 = %.2f\n"
Control returns to the calling
function "var2 = %.2f\n",var1,var2);
if return expression; printf("var1*var2 = %g\n", ans);
then value of expression is }
returned as the value of the
function call /* function definition */
Only one value can be returned this
way double product(double x, double y)
Function call {
main() is the 'calling function' double result;
product() is the 'called function'
Control transferred to the function
result = x * y;
code return result;
Code in function definition is }
executed
Function - Practice 1
Steps
Write a function 1. Function header
named 'sum' • return data type
• function name
sums two • argument list with data types
2. Statements in function definition
integers • variable declaration
returns the sum • operations
• return value
2 min. on your
own
Share with
neighbor
Function - sum()
int sum_int(int x, int y)
{
int result;
result = x + y;
return result;
}
Functions that do not return a value
Use the return type of void
void my_fun( arg_list,…)
Practice
Write two functions, the first prints out first
name, and the second prints out last name
Function - Practice 2
Steps
Program to print out two
1. Pseudocode for program logic
happy :) :) or sad faces : 2. Function header
( :( • return data type (if any)
Continuously prompts for • function name
user input: • argument list with data types (if any)
) for happy face 3. Statements in function definition
( for sad face • variable declaration (if any)
• operations
Quits if 'q' or 'Q' entered
• return value
calls two functions
happy_face()
sad_face()
Work in pairs
Pseudocode first!!
Divide tasks of writing
the two functions
Program - Faces logic
Pseudocode
1. Declare and initialize variables
2. WHILE user input not equal to q AND not equal to Q
1) Switch on user input to
2) Case ')‘:
call happy_face();
break;
5) Case '(‘:
call sad_face();
break;
6) Case ‘q’:
7) Case ‘Q’:
break;
8) Case ‘0’:
9) Default:
re-prompt for user input
Program - Faces code
Structuring C Programs
Modularization
Breaking a program up into smaller pieces:
Instead of:
one_big_program.c
break into groupings of header files (.h) and source code (.c) files:
module_1.h
module_1.c
etc.
Rationale
separates the user-interface description (.h) from the nitty-
gritty details of implementation (.c)
The Application Programming Interface (API), the .h file, is
distinct from the implementation, the .c file (which may already
be compiled and not readily viewed)
Example: math.h from Ch
can construct and test modules independently
promotes re-use of code
Example: math.h used in Ch
See C:/ Ch / include / math.h
Declaration of constants
#define M_PI 3.14159265358979323846
Declaration of macro subsitutions
#define isgreater(x, y) ((x)>(y))
Declaration of global variables (caution!)
Function prototypes
extern double sin(double x);
Pertinent comments
Review
Structured Programming
All programs can be written using these
control structures:
Sequence
Decision (three structures)
IF
IF-ELSE
SWITCH
FOR
Structure of a C program
Ex. free_fall_d_vs_time.c
C Code for D&D 3.15c
Programmer’s block
Pre-processor directive
While loop
(repetition structure)
return statement
Arithmetic with Integers and Mixed Data Types