Functions and an Introduction to Recursion

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
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.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
Chapter 6: User-Defined Functions I
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Programmer-defined functions Development of simple functions using value parameters.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 6: User-Defined Functions I
Function Part II: Some ‘advanced’ concepts on functions.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
1 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006.
Functions Modules in C++ are called functions and classes
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
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.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
CPS120: Introduction to Computer Science Functions.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
Learners Support Publications Functions in C++
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
FUNCTIONS - What Is A Function? - Advantages Function Declaration
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
Programming Fundamentals Enumerations and Functions.
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.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Functions.
Chapter 6: User-Defined Functions I
Chapter 10: Void Functions
Chapter 5 Function Basics
School of EECS, Peking University
Chapter 5 Functions.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Pointers and Pointer-Based Strings
CSC113: Computer Programming (Theory = 03, Lab = 01)
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
User Defined Functions
Chapter 5 Function Basics
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Name: Rubaisha Rajpoot
More ‘concepts’ on Function
Chapter 6: User-Defined Functions I
Functions and an Introduction to Recursion
CHAPTER 2 Arrays and Vectors.
In C Programming Language
Pointers and Pointer-Based Strings
COMS 261 Computer Science I
CHAPTER 2 Arrays and Vectors.
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
CS1201: Programming Language 2
Intro to Programming Week # 8 Functions II Lecture # 13
More ‘concepts’ on Function
Functions Chapter No. 5.
Presentation transcript:

Functions and an Introduction to Recursion CHAPTER 1 Functions and an Introduction to Recursion

Function Definition To develop a large program effectively, it is divided into smaller pieces or modules called as functions. Function is defined to do a particular task. Function is defined by one or more statements. In C++ main() is also a function.

Uses of Function Functions are used for: Divide and conquer A large program is divided into smaller pieces to build the program effectively. Code reusability Avoid repeating code in the program. Use existing functions as building block to create new program.

Implementing Function Function is implemented in 2 steps: 1. Defining the function A function is defined by one or more statements to perform a task. Format: Return type function_name(formal argument 1, formal argument 2, …, formal argument n) { statement 1; statement 2; … } 2. Calling the function A function is invoked by calling the function and when the called function completes its take it returns a value. function_name(actual argument 1, actual argument 2, …, actual argument n)

Function Definition Example float CircleArea(float r) { const float Pi = 3.1415; return Pi * r * r; } Formal parameter Function name Return type Local object definition Function body Return statement

Function Calling Example cout << CircleArea(MyRadius) << endl; Actual parameter Function call

Function Prototype Also called as function declaration. Function prototype tells the compiler the name of a function, the return-type, the number of arguments the function receive and the types of those arguments. Format: Return type function_name(formal argument 1, formal argument 2, …, formal argument n)

Example program #include <iostream> using namespace std; float CircleArea(float r); int main() { cout << "Enter radius: "; float MyRadius; cin >> MyRadius; float Area = CircleArea(MyRadius); cout << "Circle has area " << Area; return 0; } float CircleArea(float r) { const float Pi = 3.1415; return Pi * r * r; Function Prototype Function Calling Function Definition

Functions with Empty Parameter Lists This type of function does not take any arguments and does not return a value. In C++, an empty parameter list is specified by writing void or nothing at all in the parentheses. Example: void function_name(void); void function_name();

Example program #include <iostream> using namespace std; void function1(void); void function2(); int main() { function1(); function2(); return 0; } void function1(void) { cout<<“I wrote void in parentheses”; void function2() cout<<“I wrote nothing in parentheses”; Function Prototype Function Calling Function Definition

Inline function If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time. To inline a function, place the keyword inline before the function name and define the function before any calls are made to the function. Inline functions can reduce execution time but may increase program size.

Example program //To find maximum of two numbers #include <iostream> using namespace std; inline int max(int x, int y) { return(x > y)? x : y ; } int main() cout << “max(20,10):” << max(20,10) << endl; cout << “max(0,200):” << max(0,200) << endl; cout << “max(100,1010):” << max(100,1010) << endl; return 0; Inline function

References and Reference Parameters Arguments can by passed by 2 ways: 1. Pass by value It means making a copy of content of actual parameter in the memory. Example: int incrementByValue(int number) 2. Pass by reference(or pass by address) Copy of the address of the actual parameter is stored. int incrementByReference(int &number)

Example program #include <iostream> using namespace std; int incrementByValue(int); int incrementByReference(&int); int main() { int x=2; int y=8; cout<<squareByValue(x); cout<<squareByReference(y); return 0; } int incrementByValue(int a) { return a++; int incrementByReference(int &b) { return b++; Function Prototype Display the value 3 Display the value 9

Default Arguments In C++ programming, you can provide default values for function parameters. If a function is called by passing argument/s, those arguments are used by the function. But if all argument/s are not passed while invoking a function then, the default value passed to arguments are used. Default value/s are passed to argument/s in function prototype. 

Function Overloading In C++ programming, two functions can have same identifier(name) if either number of arguments or type of arguments passed to functions are different. These types of functions having similar name are called overloaded functions.

Example of function overloading int test() { } int test(int a){ } int test(double a){ } int test(int a, double b){ } All 4 functions mentioned above are overloaded function. Overloaded function may or may not have different return type but it should have different argument(either type of argument or numbers of argument passed).

Program for function overloading #include <iostream> using namespace std; void test(int); void test(float); void test(int, float); int main() { int a = 5; float b = 5.5; test(a); test(b); test(a, b); return 0; } void test(int var) { cout<<"Integer number: "<<var<<endl; } void test(float var) cout<<"Float number: "<<var<<endl; void test(int var1, float var2) cout<<"Integer number: "<<var1; cout<<" And float number:"<<var2;

Recursion In many programming languages including C++, it is possible to call a function from a same function. This function is known as recursive function and this programming technique is known as recursion. In recursion, a function calls itself but you shouldn't assume these two functions are same function. They are different functions although they have same name.

Program for Recursion #include <iostream> using namespace std; int factorial(int); int main() { int n; cout<<"Enter a number to find factorial: "; cin>>n; cout<<"Factorial of "<<n<<" = "<<factorial(n); return 0; } int factorial(int n) { if (n>1) return n*factorial(n-1); } else return 1;

Recursion Visualization