Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 7 Modular Programming Using Functions Introduction to C Programming.

Similar presentations

Presentation on theme: "Unit 7 Modular Programming Using Functions Introduction to C Programming."— Presentation transcript:

1 Unit 7 Modular Programming Using Functions Introduction to C Programming

2 Top-Down Design Unit 7: Modular Programming

3 Top-Down Design Break Problem down into sub-problems Use available solutions for any sub-problems already solved Create modules that solve recurring sub-problems C library functions provide pre-written solutions Large real-world problems are large and complex Problem must be broken down to manage complexity Software reuse saves time and increases reliability C language provides the function as unit of modularity Black-box concept, defined inputs and outputs

4 C Library Functions Contain pre-written and pre-tested solutions Basic functions applicable to wide variety of common problems Input and Output Mathematical Operations Character Manipulation String Manipulation Sorting and Searching Time and Date Other Utility Functions

5 Functions Unit 7: Modular Programming

6 C Language Functions - Header Function Header Return Type (or void if no return value) Function Name - C identifier Parameter List int f1(void) … double f2 (int p1, double p2) … void f3 (int p3, char p4) …

7 C Language Functions - Body Function 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); }

8 Types of Functions No inputs or outputs - Perform an action Use keyword void for return and parameter list No output, one or more inputs - Perform an action with data Use keyword void for return Parameter list specifies inputs One output only - Retrieve information Specify return type, use keyword void for parameter list One output, one or more inputs - Typical, many uses Specify return type, input parameter list Multiple outputs - Special circumstances scanf() is an example The return statement can only represent one output Multiple outputs require output parameters

9 Use of a Function - Syntax Syntax: Name of function followed by argument list Parenthesized list, arguments separated by commas If function has no inputs, parenthesized list is empty f1() If function has inputs, appear in argument list f1(a1, a2, a3) Must match the parameter list in number and order Argument values are copied to parameter list

10 Use of a Function - Function Call Use of function is termed a "function call" A function can be a statement by itself f1(a1, a2); A function with a return value can be part of an expression x = y + f1(a1, a2*4) - 63;

11 Placement of Function Definitions One or more function definitions in a single file Functions must be placed one after another Cannot have a function definition inside another function A function must be declared before it is used Function declarations accomplished with prototypes Function prototypes should be placed at top of file When creating a large multi-file program, use include file Prototype is like function header, with semicolon instead of body

12 Function Call: Control of Execution When a function call appears in code: The arguments are evaluated and passed to function Execution transfers to the first statement in function The function executes until a return statement (or it finishes) Execution transfers back to the point of the call Note that the calling function may pause mid-statement A function may call one or more additional functions The program stack is used to manage nesting function calls

13 Variable Scope Unit 7: Modular Programming

14 Variable Scope Scope determines lifetime of variable How long variable uses memory How long the data in the variable is valid A variable declared at the top of a function body is local scope Lifetime only while function is active Static keyword modifies this behavior A variable declared outside of a function is file scope File scope is also called "global" scope Lifetime is for entire time program is executing A variable declared inside a compound statement is block scope Lifetime is only while execution is inside the block

15 Function Output Parameters Unit 7: Modular Programming

16 Pointers Every variable in memory has a memory address A pointer is a variable that holds a memory address Pointers are used in C for multiple reasons This unit introduces pointers to explain output parameters The pointer can be used to change memory indirectly

17 Pointers Pointers are declared using the "*" character int * ip; The address of a variable is obtained with the "&" character int 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" */

18 Output Parameters A 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); }

Download ppt "Unit 7 Modular Programming Using Functions Introduction to C Programming."

Similar presentations

Ads by Google