C Functions -Continue…-.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
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.
 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.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 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.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
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.
Storage Classes.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
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.
© 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.
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
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.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
 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.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
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.
Chapter 6: Function Introduction to function Standard functions User defined functions –function prototype –function definition Function call Storage classes.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Dale Roberts CSCI 230 Functions Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Senem Kumova Metin // CS115 // FUNCTIONS CHAPTER 5.
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
 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.
Function – I What is a function Why we need functions?
5 C Functions.
Functions Course conducted by: Md.Raihan ul Masood
Introduction to Programming
5 C Functions.
IS Program Design and Software Tools Introduction to C++ Programming
ㅎㅎ Fourth step for Learning C++ Programming Namespace Function
5 C Functions.
CSC113: Computer Programming (Theory = 03, Lab = 01)
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
EKT120: Computer Programming
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
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.
Storage class.
5 C Functions.
Scope Rules Of Variables
1-6 Midterm Review.
C Programming Lecture-17 Storage Classes
Chapter 3 - Functions Outline 3.1 Introduction
Scope Rules.
Presentation transcript:

C Functions -Continue…-

Storage Classes Storage class specifiers Automatic storage 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;

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

Scope Rules File scope Function 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.)

Scope Rules Block scope Function prototype 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

Outline Global variable with file scope Variable with block scope

Outline Variable with block scope

Outline Static variable with block scope Global variable

Outline

Printout: -------------- A = 3 A = 2 A = 1 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; break; . . . Printout: -------------- A = 3 A = 2 A = 1 University of Pittsburgh-Computer Science Department- Y. Khalifa

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

Recursion Example: factorials Notice that 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;

Recursive evaluation of 5!.

Outline

Outline

Example Using Recursion: Fibonacci Series 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 ); }

Outline

Outline

Outline

Outline

Set of recursive calls for fibonacci(3).