Top Banner
Unit 7 Modular Programming Using Functions Introduction to C Programming
18

Unit 7 Modular Programming Using Functions Introduction to C Programming.

Mar 27, 2015

Download

Documents

Mary Snyder
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Unit 7 Modular Programming Using Functions Introduction to C Programming.

Unit 7

Modular ProgrammingUsing Functions

Introduction toC Programming

Page 2: Unit 7 Modular Programming Using Functions Introduction to C Programming.

Top-Down Design

Unit 7: Modular Programming

Page 3: Unit 7 Modular Programming Using Functions Introduction to C 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

Page 4: Unit 7 Modular Programming Using Functions Introduction to C Programming.

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

Page 5: Unit 7 Modular Programming Using Functions Introduction to C Programming.

Functions

Unit 7: Modular Programming

Page 6: Unit 7 Modular Programming Using Functions Introduction to C 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) …

Page 7: Unit 7 Modular Programming Using Functions Introduction to C Programming.

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);

}

Page 8: Unit 7 Modular Programming Using Functions Introduction to C Programming.

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

Page 9: Unit 7 Modular Programming Using Functions Introduction to C Programming.

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

Page 10: Unit 7 Modular Programming Using Functions Introduction to C Programming.

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;

Page 11: Unit 7 Modular Programming Using Functions Introduction to C Programming.

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

Page 12: Unit 7 Modular Programming Using Functions Introduction to C Programming.

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

Page 13: Unit 7 Modular Programming Using Functions Introduction to C Programming.

Variable Scope

Unit 7: Modular Programming

Page 14: Unit 7 Modular Programming Using Functions Introduction to C 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

Page 15: Unit 7 Modular Programming Using Functions Introduction to C Programming.

Function Output Parameters

Unit 7: Modular Programming

Page 16: Unit 7 Modular Programming Using Functions Introduction to C 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

Page 17: Unit 7 Modular Programming Using Functions Introduction to C Programming.

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" */

Page 18: Unit 7 Modular Programming Using Functions Introduction to C Programming.

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);

}