Upload
mary-snyder
View
226
Download
3
Tags:
Embed Size (px)
Citation preview
Unit 7
Modular ProgrammingUsing Functions
Introduction toC Programming
Top-Down Design
Unit 7: Modular Programming
Top-Down DesignBreak Problem down into sub-problemsUse available solutions for any sub-problems
already solvedCreate modules that solve recurring sub-problemsC library functions provide pre-written solutionsLarge real-world problems are large and complex
Problem must be broken down to manage complexitySoftware reuse saves time and increases reliability
C language provides the function as unit of modularityBlack-box concept, defined inputs and outputs
C Library FunctionsContain pre-written and pre-tested solutionsBasic functions applicable to wide variety of
common problemsInput and OutputMathematical OperationsCharacter ManipulationString ManipulationSorting and SearchingTime and DateOther Utility Functions
Functions
Unit 7: Modular Programming
C Language Functions - HeaderFunction Header
Return Type (or void if no return value)Function Name - C identifierParameter List
int f1(void) …
double f2 (int p1, double p2) …
void f3 (int p3, char p4) …
C Language Functions - BodyFunction Body
Compound Statement (delimited by brace characters)
Last statement: return statement - if value is returned
double f2 (int p1, double p2)
{
/* Function body */
return (value);
}
Types of FunctionsNo inputs or outputs - Perform an action
Use keyword void for return and parameter listNo output, one or more inputs - Perform an action
with dataUse keyword void for returnParameter list specifies inputs
One output only - Retrieve informationSpecify return type, use keyword void for parameter
listOne output, one or more inputs - Typical, many uses
Specify return type, input parameter listMultiple outputs - Special circumstances
scanf() is an exampleThe return statement can only represent one outputMultiple outputs require output parameters
Use of a Function - SyntaxSyntax: Name of function followed by
argument listParenthesized list, arguments separated by
commasIf function has no inputs, parenthesized list is
empty
f1()
If function has inputs, appear in argument listf1(a1, a2, a3)
Must match the parameter list in number and orderArgument values are copied to parameter list
Use of a Function - Function CallUse of function is termed a "function call"
A function can be a statement by itselff1(a1, a2);
A function with a return value can be part of an expressionx = y + f1(a1, a2*4) - 63;
Placement of Function Definitions
One or more function definitions in a single fileFunctions must be placed one after anotherCannot have a function definition inside another
functionA function must be declared before it is used
Function declarations accomplished with prototypes
Function prototypes should be placed at top of fileWhen creating a large multi-file program, use
include filePrototype is like function header, with semicolon
instead of body
Function Call: Control of ExecutionWhen a function call appears in code:
The arguments are evaluated and passed to function
Execution transfers to the first statement in functionThe function executes until a return statement (or it
finishes)Execution transfers back to the point of the callNote that the calling function may pause mid-
statementA function may call one or more additional
functionsThe program stack is used to manage nesting
function calls
Variable Scope
Unit 7: Modular Programming
Variable ScopeScope determines lifetime of variable
How long variable uses memoryHow long the data in the variable is valid
A variable declared at the top of a function body is local scopeLifetime only while function is activeStatic keyword modifies this behavior
A variable declared outside of a function is file scopeFile scope is also called "global" scopeLifetime is for entire time program is executing
A variable declared inside a compound statement is block scopeLifetime is only while execution is inside the block
Function Output Parameters
Unit 7: Modular Programming
PointersEvery variable in memory has a memory
addressA pointer is a variable that holds a memory
addressPointers are used in C for multiple reasons
This unit introduces pointers to explain output parameters
The pointer can be used to change memory indirectly
PointersPointers are declared using the "*" characterint * ip;
The address of a variable is obtained with the "&" characterint x, y;
ip = &x;
To modify a variable using the pointer, the "*" is used*ip = 5; /* Stores 5 in "x" */
ip = &y; /* Change to point to y */
*ip = 12; /* Stores 12 in "y" */
Output ParametersA function with output parameters uses pointers
The output parameter is declared as a pointer using "*"
The argument must evaluate to a variable's address
Internally, the function changes the variable via the pointer
int f1(int * p1, int * p2) {
*p1 = 0;
*p2 = 27;
return (55);
}