CSC113: Computer Programming (Theory = 03, Lab = 01)

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. 1 Recursion Recursive functions –Functions that call themselves –Can only solve a base case If not base.
Advertisements

 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
 2003 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
 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.
 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.
Functions. 3Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece more manageable than the original program.
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.
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.
© 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.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
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.
© 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…-
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
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.
UniMAP SemII-09/10EKT120: Computer Programming1 Week 6 – Functions (2)
1 Chapter 6 - Functions Outline 6.1Introduction 6.2Program Components in C++ 6.6Math Library Functions 6.4Functions 6.5Function Definitions 6.6Function.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions 3.4Functions 3.5Function Definitions 3.6Function.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
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.
 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.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Functions.
Functions Course conducted by: Md.Raihan ul Masood
Topic 6 Recursion.
Dr. Shady Yehia Elmashad
IS Program Design and Software Tools Introduction to C++ Programming
RECURSION.
C Functions -Continue…-.
Chapter 3 - Functions Outline 3.1 Introduction
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.
6.11 Function Call Stack and Activation Records
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 6 - Functions Outline 5.1 Introduction
Functions Recursion CSCI 230
Variables have attributes
Functions Imran Rashid CTO at ManiWeber Technologies.
Recursive Algorithms 1 Building a Ruler: drawRuler()
Programming Fundamentals Lecture #7 Functions
Chapter 3 - Functions Outline 3.1 Introduction
Presentation transcript:

CSC113: Computer Programming (Theory = 03, Lab = 01) Computer Science Department Bahria University, Islamabad

User Defined functions Week # 9

User defined functions Function definitions Function prototypes Function calls

Create customized functions to Function Definitions Create customized functions to Take in data Perform operations Return the result Format for function definition: return-value-type function-name( parameter-list ) { declarations and statements } Example: int square( int y) { return y * y; }

Function Prototypes Function prototype Example: Function name Parameters Information the function takes in Return type Type of information the function passes back to caller (default int) void signifies the function returns nothing Only needed if function definition comes after the function call in the program Example: int maximum( int, int, int ); Takes in 3 ints Returns an int

Example of user defined functions int square ( int y ) { return y*y; }

Self practice Create a function to: Add two variables of type int Add three variables of type float Multiply two variables of type double

examples int add ( int x, int y ) { return x+y; } ************************ float add (float x, float y, float z ) return x+y+z; double multiply ( double x, double y ) return x*y;

Test drivers Whenever you create your own function, you should immediately test it with a simple program. Such programs are called “test drivers”. Temporary or adhoc programs without comments or prompts etc. “Quick and dirty”

1. Function prototype 2. Loop Function definition Function call 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 int square( int ); 9 10 int main() 11 { 12 for ( int x = 1; x <= 10; x++ ) 13 cout << square( x ) << " "; 14 15 cout << endl; 16 return 0; 17 } 18 19 20 int square( int y ) 21 { 22 return y * y; 23 } 1. Function prototype 2. Loop Function definition Function call Program Output 1 4 9 16 25 36 49 64 81 100

Self practice Create test drivers for the programs created previously i.e.: int add (int , int); float add (float, float, float); double multiply (double, double);

Function categories Week # 9

Functions Library functions User defined functions Recursive functions Inline functions Functions with empty parameter list

Recursion Recursive functions functions that directly or indirectly call themselves Can only solve a base case If not base case, the function breaks the problem into a slightly smaller, slightly simpler, problem that resembles the original problem and Launches a new copy of itself to work on the smaller problem, slowly converging towards the base case Makes a call to itself inside the return statement Eventually the base case gets solved and then that value works its way back up to solve the whole problem

Examples of Recursion Example: factorial n! = n * ( n – 1 ) * ( n – 2 ) * … * 1 Recursive relationship ( n! = n * ( n – 1 )! ) 5! = 5 * 4! 4! = 4 * 3!… Base case (1! = 0! = 1)

Factorial function - an example of recursion //recursive factorial function #include<iostream> #include<conio.h> using namespace std; unsigned long factorial (unsigned long); void main( ) { for( int i=0; i<=10; i++) cout<<factorial(i)<<endl; getch( ); } unsigned long factorial (unsigned long number) if(number <= 1) return 1; else return number * factorial(number-1);

Examples of Recursion Fibonacci series: 0, 1, 1, 2, 3, 5, 8... Each number sum of two previous ones Example of a recursive formula: fib(n) = fib(n-1) + fib(n-2)  

Fibonacci function - an example of recursion //recursive fibonacci series function #include<iostream> #include<conio.h> using namespace std; unsigned long fibonacci (unsigned long); void main( ) { unsigned long result, number; cout<<“Enter an integer:”; cin>>number; result=fibonacci (number); cout<<“Fibonacci(“ <<number<< “)”<<result<<endl; getch( ); } unsigned long fibonacci (unsigned long n) if(n== 0 || n==1) return n; else return fibonacci(n-1) + fibonacci(n-2);

Fibonacci function - an example of recursion Diagram of Fibonnaci function f( 3 ) f( 1 ) f( 2 ) f( 0 ) return 1 return 0 return +

Program Output Enter an integer: 0 Fibonacci(0) = 0   Enter an integer: 6 Fibonacci(6) = 8 Enter an integer: 20 Fibonacci(20) = 6765 Enter an integer: 30 Fibonacci(30) = 832040   Enter an integer: 35 Fibonacci(35) = 9227465

Recursion vs. Iteration Uses selection structure Terminates when base case is recognized Repeatedly calls itself Easier to understand and debug Computationally heavy in terms of memory and processing Uses repetition structure Terminates when loop condition fails Mostly uses counter controlled loop Not so apparent Computationally less heavy

Functions with Empty Parameter Lists Either writing void or leaving a parameter list empty indicates that the function takes no arguments void print(); or void print( void ); Function print takes no arguments and returns no value

Notice the two ways of declaring no arguments. 1 // Fig. 3.18: fig03_18.cpp 2 // Functions that take no arguments 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 void function1(); 9 void function2( void ); 10 11 int main() 12 { 13 function1(); 14 function2(); 15 16 return 0; 17 } 18 19 void function1() 20 { 21 cout << "function1 takes no arguments" << endl; 22 } 23 24 void function2( void ) 25 { 26 cout << "function2 also takes no arguments" << endl; 27 } 1. Function prototypes (take no arguments) 2. Call the functions Function definitions Program Output Notice the two ways of declaring no arguments. function1 takes no arguments function2 also takes no arguments

Inline Functions inline functions Reduce function-call overhead Asks the compiler to copy code into program instead of using a function call Compiler can ignore inline Should be used with small, often-used functions

Fibonacci function - an example of recursion //inline function #include<iostream> #include<conio.h> using namespace std; inline float cube(const float s) {return s*s*s;} void main( ) { cout<<“Enter the side length of your cube:” ; float side; cin>>side; cout<<“Volume of cube with side”<<side<<“ is” <<cube(side)<<endl; getch( ); }