C Programming Lecture 8-2 : Function (advanced). Recursive Function (recursion) A function that calls itself (in its definition) Classic example : factorial.

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
Exercise 4 1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the.
CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays.
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
Function Part II: Some ‘advanced’ concepts on functions.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
FALL 2001ICOM Lecture 21 ICOM 4015 Advanced Programming Lecture 2 Procedural Abstraction Reading: LNN Chapter 4, 14 Prof. Bienvenido Velez.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
FUNCTIONS. C function can be classified into two categories, namely, library functions and user – defined functions. main is an example of user – defined.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Return function Random function Recursion function Function in C 1.
CMPE 150: Introduction to Computing Functions. A function groups a set of related statements under a single title. You can "call" the function using its.
LECTURE 11 TYPES OF USER DEFINE FUNCTIONS ITC-414.
1 Lecture04: Function Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
C Programming Lecture 10-1 : Array & Pointer. Character Array String A sequence of characters The last character should be ‘\0’ that indicates “the end.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program.
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Functions Sujana Jyothi C++ Workshop Day 2. Functions 3 Parameter transmission modes pass by value (default) pass by reference (&) pass by const reference.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
C++ Basics II Lecture 2 Seoul National University Graphics & Media Lab.
Templates Where the TYPE is generic. Templates for functions Used when the you want to perform the same operation on different data types. The definition.
C# Programming Methods.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
C++ Programming Lecture 12 Functions – Part IV
FUNCTIONS (METHODS) Pascal C, C++ Java Scripting Languages Passing by value, reference Void and non-void return types.
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Introduction to Programming
Introduction to Programming
CSE 220 – C Programming Pointers.
C Functions -Continue…-.
FUNCTIONS In C++.
CSC113: Computer Programming (Theory = 03, Lab = 01)
9. FUNCTIONS.
Programming Fundamentals Lecture #7 Functions
CSC113: Computer Programming (Theory = 03, Lab = 01)
User-defined Functions
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
More ‘concepts’ on Function
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
A function with one argument
Pass by Reference.
Stack Frames and Functions
Simulating Reference Parameters in C
The Function Prototype
Functions.
In C Programming Language
CS148 Introduction to Programming II
Seoul National University
Introduction to Computing Lecture 08: Functions (Part I)
More ‘concepts’ on Function
Functions Chapter No. 5.
Presentation transcript:

C Programming Lecture 8-2 : Function (advanced)

Recursive Function (recursion) A function that calls itself (in its definition) Classic example : factorial

Recursion example : factorial // recursive function int factorial (int n) { // we assume n is positive integer if ( n == 0 ) return 1; else return n  factorial (n - 1); // recursive calling } // end factorial f(N) = N! f(N) = N  f(N-1)..... printf(“%d”, factorial (4));.....

int factorial ( 4 ) { if ( n == 0 ) return 1; else return 4  factorial (3); } // end factorial int factorial ( 3 ) { if ( n == 0 ) return 1; else return 3  factorial (2); } // end factorial Recursive call factorial (4); recursion

int factorial ( 2 ) { if ( n == 0 ) return 1; else return 2  factorial (1); } //end factorial int factorial ( 1 ) { if ( n == 0 ) return 1; else return 1  factorial (0); } // end factorial Recursive call recursion

int factorial ( 1 ) { if ( n == 0 ) return 1; else return 1  factorial (0); } // end factorial factorial ( 0 ) { if ( n == 0 ) return 1; else return 0  factorial (-1); } // end factorial Return 1 recursion

int factorial ( 2 ) { if ( n == 0 ) return 1; else return 2  factorial (1); } // end factorial int factorial ( 1 ) if ( n == 0 ) return 1; else return 1  factorial (0); } // end factorial 1 1  1 = 1 Return 1 recursion

int factorial ( 3 ) { if ( n == 0 ) return 1; else return 3  factorial (2); } // end factorial int factorial ( 2 ) { if ( n == 0 ) return 1; else return 2  factorial (1); } // end factorial 1 2  1 = 2 Return 2 recursion

int factorial ( 4 ) { if ( n == 0 ) return 1; else return 4  factorial (3); } // end factorial int factorial ( 3 ) { if ( n == 0 ) return 1; else return 3  factorial (2); } // end factorial 2 3  2 = 6 Return 6 recursion

int factorial ( 4 ) { if ( n == 0 ) return 1; else return 4  factorial (3) } // end factorial 6 4  6 = cout << factorial (4); //output 24 Return 24

Exercise : fibonacci numbers Code?

Call-by-Value The value of Argument variable will be copied to parameter variable The value of Argument variable is not affected during the processing of function Advantage : we can avoid(exclude) unwanted side- effects C provides call-by-value mechanism.

Example : swap function #include void swap(int a, int b) { int temp; temp=a; a=b; b=temp; } int main() { int x=3, y=2; printf(“before: x=%d, y=%d\n”,x,y); swap(x,y); printf(“after : x=%d, y=%d\n”,x,y); } #include void swap(int* a, int* b) { int temp; temp=*a; *a=*b; *b=temp; } int main() { int x=3, y=2; printf(“before: x=%d, y=%d\n”,x,y); swap(&x,&y); printf(“after : x=%d, y=%d\n”,x,y); } Output :

Macro function Effective when a function is short and simple #define min(x,y) ( (x<y) ? (x) : (y) ) #define max(x,y) ( (x>y) ? (x) : (y) ) Advantage? No overhead for function call & return

Example : MAX #include #define MAX(x, y) (x > y)? x: y int main() { int i, j; int max; printf("get two integers : "); scanf("%d %d", &i, &j); max = MAX(i, j); printf("MAX(%d, %d) = %d\n", i, j, max); return 0; }

Inline function the compiler will insert the complete body of the inline function in every place in the code where that function is used. Reduce overhead for function call & return Effective when a function is short and simple inline int cube( int n ) { return n * n * n; }

Static (additional) #include int count() { static int n = 0; return ++n; } int main() { int i; for (i = 0; i < 5; ++i) printf("count = %d\n", count()); return 0; }