Senem Kumova Metin // CS115 // 2008-20091 FUNCTIONS continues CHAPTER 5.

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.
Modular Programming With Functions
By Senem Kumova Metin 1 DATA TYPES. by Senem Kumova Metin 2 DATA TYPE? …… x; // DECLARATION OF VARIABLE X printf(“Do you want to go on? \n”) printf(“Please.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Chapter 7: User-Defined Functions II
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.
1 Review of Class on Oct Outline  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Proof Techniques and Recursion. Proof Techniques Proof by induction –Step 1: Prove the base case –Step 2: Inductive hypothesis, assume theorem is true.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
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.
Storage Classes.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Modules and Scope Gabriel Hugh Elkaim Spring 2013.
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.
CSCI 130 Scope of Variables Chapter 6. What is scope Parts of program which can access a variable –accessibility –visibility How long variable takes up.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
M180: Data Structures & Algorithms in Java
Lecture 10 Recursion CSE225: Data Structures. 2 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
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.
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
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.
Functions. Flow of Control Review while for do while goto break & continue switch Relational operators short circuits.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
Senem Kumova Metin // CS115 // FUNCTIONS CHAPTER 5.
A First Book of ANSI C Fourth Edition
Introduction to C Lecture 4: Functions Alireza Masoum Urmia University Of Technology 2007
Program Development and Design Using C++, Third Edition
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objectives  Function Templates  Recursion Functions.
CS212: Data Structures and Algorithms
C Functions -Continue…-.
ㅎㅎ Fourth step for Learning C++ Programming Namespace Function
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Functions.
Programmazione I a.a. 2017/2018.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Chapter 6 - Functions Outline 5.1 Introduction
Storage class.
Recursion.
In C Programming Language
CS148 Introduction to Programming II
STORAGE CLASS.
C Programming Lecture-17 Storage Classes
Storage classes in C In C language, each variable has a storage class which decides the following things: scope i.e where the value of the variable would.
Functions.
Functions Chapter No. 5.
Storage Classes.
Presentation transcript:

Senem Kumova Metin // CS115 // FUNCTIONS continues CHAPTER 5

Senem Kumova Metin // CS115 // CALL BY VALUE 2.DEVELOPING A LARGE PROGRAM 3.SCOPE RULES 4.STORAGE CLASSES 5.RECURSION

Senem Kumova Metin // CS115 // CALL BY VALUE If a variable is passed to a function, the stored value in the calling environment will not be changed!!! EXAMPLE: #include int my_sum(int n); void main(void) {int n=9; printf(“%d\n”,n); printf(“%d\n”,my_sum(n)); // call function my_sum() by value printf(“%d\n”,n); } int my_sum(int n) { n=n+2; // stored value of n in my_sum() is changed return n; } OUTPUT ??

Senem Kumova Metin // CS115 // Developing a large program read 5.8 (pg 209) /* my_program.c */ #include”my_header.h” void main(void) { int a=3, b=4, result=0; result=my_sum(a,b); printf(“%d\n”,result); result=my_subtract(a,b); printf(“%d\n”,result); printf(“%f\n”,PI); } /* my_header.h */ #include #define PI 3.14 int my_sum(int x,int y) { x++; y++; return x+y; } int my_subtract(int x, int y) {return x-y-1; }

Senem Kumova Metin // CS115 // SCOPE RULES Each identifier is accessible only within the block in which it has been declared!!! EXAMPLE: #include void func_1 (int a) {int b, c; } void func_2 (int a, double b, float d) { char c; } void main () { int a,b,d; char c; } func1() variables abc abdc func2() variables abcd main() variables

Senem Kumova Metin // CS115 // SCOPE RULES : EXAMPLE # include void main (void) {int a=2; float b=3.2; // outer block a printf("%d\n",a); {int a =5; // inner block a printf("%d\n", a); printf("%f\n", a+b); { int b=4; printf("%d\n“, a+b); } printf("%d\n",++a); } OUTPUT:

Senem Kumova Metin // CS115 // STORAGE CLASSES  EACH VARIABLE HAS A TYPE AND A STORAGE CLASS  STORAGE CLASSES 1.auto /* variables declared within function bodies are automatic by default */ 2.extern /* look for it elsewhere either in this file or in some other file */ 3.register 4.static  EXAMPLE extern int a=1; /* type of variable a is int, storage class of a is extern */ auto float f; float z; /* storage class is auto and data type is float */

Senem Kumova Metin // CS115 // STORAGE CLASSES 2. EXTERN The keyword extern is used to tell the compiler to "look for it elsewhere, either in this file or in some other file" External variables never disappear. Because they exist throughout the execution life of the program, they can be used to transmit values across functions ( GLOBAL)

Senem Kumova Metin // CS115 // STORAGE CLASSES 2. EXTERN /* file1.h */ int f(void) { extern int a; /* look for it elsewhere */ int b, c; // local b and c a = b = c = 4; // global b anc c are masked return (a + b + c); } /* prog1.c*/ #include #include ”file1.h” int a = 1, b = 2, c = 3; /* global variables */ int f(void); int main(void) { printf("%3d%3d%3d\n", a, b, c); printf("%3d\n", f() ); printf("%3d%3d%3d\n", a, b, c); return 0; } OUTPUT:

Senem Kumova Metin // CS115 // STORAGE CLASSES 3. REGISTER Forces compiler that these variables should be stored in high speed memory registers Compiler may or may not store them in registers Reading data from registers is faster comparing to reading from memory EXAMPLES: register char c; register int a=1; /* IS EQUIVALENT TO */ register a=1; // default data type is int

Senem Kumova Metin // CS115 // STORAGE CLASSES 4. STATIC Memory opened for the static variable will not be closed after the execution of the function Initialization for static variables is done just once in the first execution of the function EXAMPLE: #include int ver(); main() {printf(“1. value=%d\n”, ver() ); printf(“2. value=%d\n”, ver() ); printf(“3. value=%d\n”, ver() ); } int ver() {static int k =0; k=k+5; return k; } OUTPUT 1. value=5 2. value=10 3. value=15

Senem Kumova Metin // CS115 // STORAGE CLASSES STATIC EXTERNAL VARIABLES Can be seen below the prototype/declaration but can not be seen in any other file Scope restricted Cannot be used with functions defined earlier or in any other files Program security !! EXAMPLE : #include void f(void) { } // v is not available static int v; // static external variable void g(void) {..... } // you can use v

Senem Kumova Metin // CS115 // RECURSION An algorithmic technique where a function, in order to accomplish a task, calls itself with some part of the task If a function calls itself, then it is called recursive !!! There are two parts to the definition of a recursive solution: –Base case: Simple, non-recursive solution –General method: Uses simpler version of the problem to solve harder problem

Senem Kumova Metin // CS115 // RECURSION : EXAMPLES void main(void) { printf(“I am calling myself!\n”); main(); } /* NO BASE CASE SO IT WILL NEVER STOP */ #include void rev( ) { char ch; ch=getchar(); if (ch != '\n') rev(); putchar(ch);} // BASE CASE ch=‘\n’ int main( ) { rev();}

Senem Kumova Metin // CS115 // RECURSION : EXAMPLES // ITERATION int sum(int N) { int i; int SUM =0; for (i=1; i<=N; i++) SUM=SUM+1; return SUM; } // RECURSION int sum(int N) { if(N==1) return 1; else return(N+sum(N-1));} /* BASE CASE : N==1  1 METHOD : sum(N) = N + sum(N-1) if BASE CASE else METHOD */

Senem Kumova Metin // CS115 // RECURSION : EXAMPLES BASE CASE  N=1 return 1 METHOD  sum(N)= sum(N-1)+1/N double sum(int N) {if(N==1) return 1; else return (sum(N-1)+1/N); }

Senem Kumova Metin // CS115 // RECURSION & ITERATION EXAMPLE : FACTORIAL /*iterative version n! = 1*2*3* … * n = n*(n-1)*(n-2) * …*3*2*1 */ int factorial (int n) { int product=1; for(;n>1;--n) product=product*n; return product;} /* recursive version n! = n * (n-1)! */ int factorial(int n) { if(n<=1) return 1; else return ( n*factorial(n-1) ); } /* BASE CASE : n<=1  1 METHOD : n!=n*(n-1)! */ factorial(3) = 3 * factorial(2) = 3 * ( 2 * factorial(1) ) = 3 * ( 2 * ( 1 * factorial(0) )) = 3 * ( 2 * ( 1 * 1 ) )) = 6

Senem Kumova Metin // CS115 // RECURSION & ITERATION EXAMPLE : POWER /*iterative version x n = x*x*x* … *x */ /* Assume that n is always positive*/ int power (int x, int n) { int result=1; int i; for(i=1; i<=abs(n); i++) result=result*x; } /* recursive version x n = x n-1 * x */ /* Assume that n is always positive */ int power(int x, int n) { if(n==1) return x; else return ( x* power(x,n-1) ); } /* BASE : n==1  1 METHOD : power(x,n)= power(x,n-1)*x */ power(3,4) = 3 * power(3,3) = 3 * ( 3 * power(3,2) ) = 3 * ( 3 * ( 3 * power(3,1) )) = 3 * ( 3 * ( 3 * 3 ) ) = 81

Senem Kumova Metin // CS115 // RECURSION EXAMPLE : FIBONACCI In mathematics, the Fibonacci numbers form a sequence definedmathematicssequence recursivelyrecursively by for n=0  fibonacci(0) = 0 fibonacci (n) = for n=1  fibonacci(1) = 1 for n >1  fibonacci(n) = fibonacci(n-1) + fibonacci(n-2) int fibonacci(int n) { if(n<=1) return 1; else return ( fibonacci(n-1)+fibonacci(n-2) );} /* BASE CASE : n<=1  1 METHOD : fibonacci(n)=fibonacci(n-1)+ fibonacci(n-2)*/

Senem Kumova Metin // CS115 // YOUR HOMEWORK TOWERS OF HANOI