 2007 Pearson Education, Inc. All rights reserved. 1 5 5 C Functions -Continue…-

Slides:



Advertisements
Similar presentations
 2006 Pearson Education, Inc. All rights reserved Functions.
Advertisements

Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
1 Storage Classes, Scope, and Recursion Lecture 6.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
 Pearson Education, Inc. All rights reserved Recursion.
 2008 Pearson Education, Inc. All rights reserved Function Call Stack and Activation Records Data structure: collection of related data items.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
C++ Programming Lecture 12 Functions – Part IV
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
A First Book of ANSI C Fourth Edition
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 2 Functions September.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2008 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
5 C Functions.
Functions Course conducted by: Md.Raihan ul Masood
5.13 Recursion Recursive functions Functions that call themselves
5 C Functions.
IS Program Design and Software Tools Introduction to C++ Programming
RECURSION.
C Functions -Continue…-.
5 C Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
6 Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Chapter 6 - Functions Outline 5.1 Introduction
Functions Recursion CSCI 230
5 C Functions.
5 C Functions.
Function.
1-6 Midterm Review.
Programming Fundamentals Lecture #7 Functions
Function.
Presentation transcript:

 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-

 2007 Pearson Education, Inc. All rights reserved Storage Classes  Storage class specifiers – Storage duration – how long an object exists in memory – Scope – where object can be referenced in program – Linkage – specifies the files in which an identifier is known  Automatic storage – Object created and destroyed within its block – auto: default for local variables auto double x, y; – register: tries to put variable into high-speed registers - Can only be used for automatic variables register int counter = 1;

 2007 Pearson Education, Inc. All rights reserved. 3 Performance Tip 5.1 Automatic storage is a means of conserving memory, because automatic variables exist only when they are needed. They are created when the function in which they are defined is entered and they are destroyed when the function is exited.

 2007 Pearson Education, Inc. All rights reserved. 4 Software Engineering Observation 5.10 Automatic storage is an example of the principle of least privilege—allowing access to data only when it is absolutely needed. Why have variables stored in memory and accessible when in fact they are not needed?

 2007 Pearson Education, Inc. All rights reserved. 5 Performance Tip 5.2 The storage-class specifier register can be placed before an automatic variable declaration to suggest that the compiler maintain the variable in one of the computer’s high-speed hardware registers. If intensely used variables such as counters or totals can be maintained in hardware registers, the overhead of repeatedly loading the variables from memory into the registers and storing the results back into memory can be eliminated.

 2007 Pearson Education, Inc. All rights reserved. 6 Performance Tip 5.3 Often, register declarations are unnecessary. Today’s optimizing compilers are capable of recognizing frequently used variables and can decide to place them in registers without the need for a register declaration.

 2007 Pearson Education, Inc. All rights reserved Storage Classes  Static storage – Variables exist for entire program execution – Default value of zero – static: local variables defined in functions. - Keep value after function ends - Only known in their own function – extern: default for global variables and functions - Known in any function

 2007 Pearson Education, Inc. All rights reserved. 8 Software Engineering Observation 5.11 Defining a variable as global rather than local allows unintended side effects to occur when a function that does not need access to the variable accidentally or maliciously modifies it. In general, use of global variables should be avoided except in certain situa­tions with unique performance requirements.

 2007 Pearson Education, Inc. All rights reserved. 9 Software Engineering Observation 5.12 Variables used only in a particular function should be defined as local variables in that function rather than as external variables.

 2007 Pearson Education, Inc. All rights reserved Scope Rules  File scope – Identifier defined outside function, known in all functions – Used for global variables, function definitions, function prototypes  Function scope – Can only be referenced inside a function body – Used only for labels ( start:, case:, etc.)

 2007 Pearson Education, Inc. All rights reserved Scope Rules  Block scope – Identifier declared inside a block - Block scope begins at definition, ends at right brace – Used for variables, function parameters (local variables of function) – Outer blocks "hidden" from inner blocks if there is a variable with the same name in the inner block  Function prototype scope – Used for identifiers in parameter list

 2007 Pearson Education, Inc. All rights reserved. 12 Common Programming Error 5.14 Accidentally using the same name for an identifier in an inner block as is used for an identifier in an outer block, when in fact you want the identifier in the outer block to be active for the duration of the inner block.

 2007 Pearson Education, Inc. All rights reserved. 13 Error-Prevention Tip 5.2 Avoid variable names that hide names in outer scopes. This can be accomplished simply by avoiding the use of duplicate identifiers in a program.

 2007 Pearson Education, Inc. All rights reserved. 14 Outline fig05_12.c (1 of 4 ) Global variable with file scope Variable with block scope

 2007 Pearson Education, Inc. All rights reserved. 15 Outline fig05_12.c (2 of 4 ) Variable with block scope

 2007 Pearson Education, Inc. All rights reserved. 16 Outline fig05_12.c (3 of 4 ) Static variable with block scope Global variable

 2007 Pearson Education, Inc. All rights reserved. 17 Outline fig05_12.c (4 of 4 )

 2007 Pearson Education, Inc. All rights reserved. 18 Variable Scope int A; void main() {A = 1; myProc(); printf ( "A = %d\n", A); } void myProc() { int A = 2; while( A==2 ) { int A = 3; printf ( "A = %d\n", A); break; } printf ( "A = %d\n", A); }... Printout: A = 3 A = 2 A = 1 University of Pittsburgh-Computer Science Department- Y. Khalifa

 2007 Pearson Education, Inc. All rights reserved Recursion  Recursive functions – Functions that call themselves – Can only solve a base case – Divide a problem up into - What it can do - What it cannot do What it cannot do resembles original problem The function launches a new copy of itself (recursion step) to solve what it cannot do – Eventually base case gets solved - Gets plugged in, works its way up and solves whole problem

 2007 Pearson Education, Inc. All rights reserved Recursion  Example: factorials – 5! = 5 * 4 * 3 * 2 * 1 – Notice that - 5! = 5 * 4! - 4! = 4 * 3!... – Can compute factorials recursively – Solve base case ( 1! = 0! = 1 ) then plug in - 2! = 2 * 1! = 2 * 1 = 2; - 3! = 3 * 2! = 3 * 2 = 6;

 2007 Pearson Education, Inc. All rights reserved. 21 Fig | Recursive evaluation of 5!.

 2007 Pearson Education, Inc. All rights reserved. 22 Outline fig05_14.c (1 of 2 )

 2007 Pearson Education, Inc. All rights reserved. 23 Outline fig05_14.c (2 of 2 )

 2007 Pearson Education, Inc. All rights reserved. 24 Common Programming Error 5.15 Forgetting to return a value from a recursive function when one is needed.

 2007 Pearson Education, Inc. All rights reserved. 25 Common Programming Error 5.16 Either omitting the base case, or writing the recursion step incorrectly so that it does not converge on the base case, will cause infinite recursion, eventually exhausting memory. This is analogous to the problem of an infinite loop in an iterative (nonrecursive) solution. Infinite recursion can also be caused by providing an unexpected input.

 2007 Pearson Education, Inc. All rights reserved Example Using Recursion: Fibonacci Series  Fibonacci series: 0, 1, 1, 2, 3, 5, 8... – Each number is the sum of the previous two – Can be solved recursively: - fib( n ) = fib( n - 1 ) + fib( n – 2 ) – Code for the fibonacci function long fibonacci( long n ) { if (n == 0 || n == 1) // base case return n; else return fibonacci( n - 1) + fibonacci( n – 2 ); }

 2007 Pearson Education, Inc. All rights reserved. 27 Outline fig05_15.c (1 of 4 )

 2007 Pearson Education, Inc. All rights reserved. 28 Outline fig05_15.c (2 of 4 )

 2007 Pearson Education, Inc. All rights reserved. 29 Outline fig05_15.c (3 of 4 )

 2007 Pearson Education, Inc. All rights reserved. 30 Outline fig05_15.c (4 of 4 )

 2007 Pearson Education, Inc. All rights reserved. 31 Fig | Set of recursive calls for fibonacci(3).

 2007 Pearson Education, Inc. All rights reserved. 32 Common Programming Error 5.17 Writing programs that depend on the order of evaluation of the operands of operators other than &&, ||, ?:, and the comma (, ) operator can lead to errors because compilers may not necessarily evaluate the operands in the order you expect.

 2007 Pearson Education, Inc. All rights reserved. 33 Portability Tip 5.2 Programs that depend on the order of evaluation of the operands of operators other than &&, ||, ?:, and the comma (, ) operator can function differently on systems with different compilers.

 2007 Pearson Education, Inc. All rights reserved. 34 Performance Tip 5.4 Avoid Fibonacci-style recursive programs which result in an exponential “explosion” of calls.

 2007 Pearson Education, Inc. All rights reserved Recursion vs. Iteration  Repetition – Iteration: explicit loop – Recursion: repeated function calls  Termination – Iteration: loop condition fails – Recursion: base case recognized  Both can have infinite loops  Balance – Choice between performance (iteration) and good software engineering (recursion)

 2007 Pearson Education, Inc. All rights reserved. 36 Software Engineering Observation 5.13 Any problem that can be solved recursively can also be solved iteratively (nonrecursively). A recursive approach is normally chosen in preference to an iterative approach when the recursive approach more naturally mirrors the problem and results in a program that is easier to understand and debug. Another reason to choose a recursive solution is that an iterative solution may not be apparent.

 2007 Pearson Education, Inc. All rights reserved. 37 Performance Tip 5.5 Avoid using recursion in performance situations. Recursive calls take time and consume additional memory.

 2007 Pearson Education, Inc. All rights reserved. 38 Common Programming Error 5.18 Accidentally having a nonrecursive function call itself either directly, or indirectly through another function.

 2007 Pearson Education, Inc. All rights reserved. 39 Performance Tip 5.6 Functionalizing programs in a neat, hierarchical manner promotes good software engineering. But it has a price. A heavily functionalized program—as compared to a monolithic (i.e., one-piece) program without functions—makes potentially large numbers of function calls, and these consume execution time on a computer’s processor(s). So, although monolithic programs may perform better, they are more difficult to program, test, debug, maintain, and evolve.