Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions and an Introduction to Recursion

Similar presentations


Presentation on theme: "Functions and an Introduction to Recursion"— Presentation transcript:

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

2 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.

3 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.

4 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)

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

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

7 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)

8 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 = ; return Pi * r * r; Function Prototype Function Calling Function Definition

9

10 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();

11 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

12 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.

13 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

14 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)

15 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

16 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. 

17

18 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.

19 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).

20 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;

21 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.

22 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;

23 Recursion Visualization


Download ppt "Functions and an Introduction to Recursion"

Similar presentations


Ads by Google