Object Oriented Programming Spring - 2012 COMSATS Institute of Information Technology Functions OOP in C++ by Robert Lafore - Chapter#5 Kaleem Ullah

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

Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Overview of Previous Lesson(s) Over View  C++  KeywordsReserved words  IdentifiersProgrammers defined variables  Variables A named storage location.
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.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Introduction to C++ Programming Introduction to C++ l C is a programming language developed in the 1970's alongside the UNIX operating system. l C provides.
1 Chapter 9 Scope, Lifetime, and More on Functions.
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.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
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.
Recursion. Definition Recursion is a function calling on itself over and over until reaching an end state. One such example is factorial. 10! = 10 * 9.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Chapters 1-5 Review C++ Class. Chapter 1 – the big picture Objects Class Inheritance Reusability Polymorphism and Overloading.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
1 CS161 Introduction to Computer Science Topic #9.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 6 Functions.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
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)
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Functions BICSE-6A Mr. Naeem Khalid Lecturer, Dept. of Computing.
C++ Programming Lecture 12 Functions – Part IV
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
Functions  Simple Functions  Passing Arguments to Functions  Returning Values from Functions  Reference Arguments  Overloaded Functions  Recursion.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Program Development and Design Using C++, Third Edition
Topic 6 Recursion.
Chapter 15 Recursion.
IS Program Design and Software Tools Introduction to C++ Programming
Chapter 15 Recursion.
Java 4/4/2017 Recursion.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 5 Conclusion CIS 61.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 9 Scope, Lifetime, and More on Functions
FUNCTIONS& FUNCTIONS OVERLOADING
Stacks & Recursion.
Object Oriented Programming Using C++
Computing Fundamentals
Module 1-10: Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
CS1201: Programming Language 2
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
Functions Chapter No. 5.
Presentation transcript:

Object Oriented Programming Spring COMSATS Institute of Information Technology Functions OOP in C++ by Robert Lafore - Chapter#5 Kaleem Ullah

Overloaded functions int main() { repchar(); repchar(‘=’); repchar(‘+’, 30); return 0; } 2

Overloaded functions 3

Overloaded functions: Different kinds of arguments struct Distance { int feet; float inches; }; Void disp( Distance ); //declarations Void disp( float ); 4

Overloaded functions: Different kinds of arguments struct Distance { int feet; float inches; }; Void disp( Distance ); //declarations Void disp( float ); 5

6 Properties of recursive functions 1.Base Case(s): condition for terminating the recursive process 2.Recursive Case(s): set of rules to break the problem into smaller sub-problems to reach base case a)Divide the problem into smaller sub- problems b)Solve the sub-problems c)Combine results to get answer Sub-problems solved as a recursive call to the same function

7 int loop(int x) { + return (1 + loop(x)) } infinite loop – no termination Trace Table with x=5 1 + loop 5 Problem not being divided into smaller problems – no termination 1 + loop 5 … loop 5 Need of Base Case and Recursive Case Recursive function with –no base case –not a valid recursive case

8 Power function Lets figure out a recursive function for calculating the Powers of a given number 2 nd power function Square of x = x*x 3 rd power function Cube of x = x*x*x 4th power function Fourth Power of x = x*x*x*x

9 Power function x 4 = x*x*x*x = x*( x*x*x ) = x*x 3 x 5 = x*x*x*x*x = x*(x*x*x*x ) = x*x 4 x 6 = x*x*x*x*x*x = x*(x*x*x*x*x ) = x*x 5 In general x n = x*x n-1 Int power (int x, int n) { return x * power (x, n-1) }

10 Power Function When does it stop ? Int power (int x, int n) { return x * power (x, n-1) } Step no. Calc 2 3 : Power (2,3) x=2, n=3 1 2* power(2,2) 2 2* 2* power(2,1) 3 2*2* 2*power(2,0) 4 2*2*2* 2*power(2,-1) x * power (x, n-1) We need to stop here We know 2 0 =1 Base case: if n==0 return 1 Trace table

11 Revised Power Function Int power (int x, int n) { If (n==0) return 1; else return x * power (x, n-1) } Result: 8 Base case Recursive case sub-problems must be “smaller” than the original problem otherwise the recursion never terminates. Trace table: Calc 2 3 : x=2, n=3 Power (2,3) 2* power(2,2) 2* 2* power(2,1) 2*2* 2*power(2,0) =8

12 Factorial function Factorial0! = 1 1! = 1 2! = 2 * 1 = 2 3! = 3 * 2 * 1 = 6 4! = 4 * 3 * 2 * 1 = 24

13 Factorial function 0! = 1 1! = 1 2! = 2 * 1 = 2 3! = 3 * 2 * 1 = 6 4! = 4 * 3 * 2 * 1 = 24 4*3! 3*2! 2*1! 1*0! …… In general: n!=n*(n-1)! Recursive case: Factorial(n)=n*factorial(n-1) Base case: 0!=1 i.e; if (n==0) return 1

14 Factorial function Int factorial (int n) { If (n==0) return 1; else return n * factorial (n-1) } Trace table: Calc 4! here n=4 factorial (4) 4* factorial (3) 4* 3* factorial (2) 4*3* 2* factorial (1) 4*3*2* 1* factorial (0) =24

15 Factorial function Version Action Argument or Return Value 1 Call 5 2 Call 4 3 Call 3 4 Call 2 5 Call 1 5 Return 1 4 Return 2 3 Return 6 2 Return 24 1 Return 120

16 Fibonacci sequence The first ten terms in the sequence are: 1,1,2,3,5,8,13,21,34,55 Each value, except for first two, is sum of last two values Simply saying: Fib(n)= fib(n-1)+fib(n-2) except for when n=0 and n=1 Base case: if (n==0 or n==1) Return 1 Recursive case: Fib(n)= fib(n-1)+fib(n-2)

17 Function for fibonacci sequence Int fib (int n) { If (n==0 or n==1) return 1; else return fib(n-1) +fib (n-2); }

18 Trace of Fibonacci(5) fib 5 fib 4 + fib 3 fib 3 + fib 2 fib 2 + fib 1 fib 1 + fib 0 = 8 If (n==0 or n==1) return 1; else return fib(n-1) +fib (n-2);

19 Why recursion? Recursion makes the program faster? Recursion uses less memory? Recursion makes the code much simpler and Easy to read

In-Line functions Functions save memory space because call to function cause the same code to be executed When a function is called, jump to function is made. After the call, jump back to instruction following the call takes some extra time for jump to function  Slows down the program To save execution time for short functions having one or two statements)  Make them inline 20

In-Line functions 21

In-Line functions: Example #include using namespace std; inline float lbstokg(float pounds) // converts pounds to kilograms { return * pounds; } int main() { float lbs; cout << “\nEnter your weight in pounds: “; cin >> lbs; cout << “Your weight in kilograms is “ << lbstokg(lbs) << endl; return 0; } 22

In-Line functions: Example inline float lbstokg(float pounds) // inline function Sometimes the compiler will ignore the request and compile the function as a normal function. It might decide the function is too long to be inline, for instance. 23

Default arguments In OVERLOAD we used three different functions with the same name to handle different numbers of arguments. Achieve the same effect in a different way!! // demonstrates missing and default arguments #include using namespace std; void repchar(char=’*’, int=45); //declaration with default arguments 24

Default arguments: Example Achieve the same effect in a different way!! int main() { repchar(); //prints 45 asterisks repchar(‘=’); //prints 45 equal signs repchar(‘+’, 30); //prints 30 plus signs return 0; } 25

Default arguments: Example void repchar(char ch, int n) { for(int j=0; j<n; j++) //loops n times cout << ch; //prints ch cout << endl; } 26

Default arguments: Example void repchar(char=’*’, int=45); //declaration with default arguments The default argument follows an equal sign Working in case of missing arguments?  One missing?  Two missing?  Remember that missing arguments must be those at the end of the argument list!! Can’t miss first argument and provide second 27

Static Variable A static variable in kind of local variable (inside a function)  lifetime is the same as that of a global variable Does not exist until the first call to the function containing it is made remains in existence for the life of the program 28

Static Variable: Example void teststatic() { static int count; //automatically initialized to 0 count++; cout<<"I am called "<<count<<" times"<<endl; } void main() { teststatic(); } 29

Static Variable: Example 30

Non-Static Variable: Example void teststatic() { int count=0; // initialized to 0, not a static variable count++; cout<<"I am called "<<count<<" times"<<endl; } int main() { teststatic(); } 31

Non-Static Variable: Example 32

Const function arguments To assure that the function cannot change the value passed void testconst(int a, const int b ) { a=10; //OK b=10; //error } int main() { int x,y; testconst(x,y); } 33