Presentation is loading. Please wait.

Presentation is loading. Please wait.

User Defined Functions

Similar presentations


Presentation on theme: "User Defined Functions"— Presentation transcript:

1 User Defined Functions
Rehab Duwairi

2 Default arguments: If the driver does not send a value to an argument (when calling a function) then that function will use a default value for that argument.

3 #include <iostream> using namespace std; double sum (double =5, double =10, double =15); void main () { double a, b, c; cout<<"please enter a, b, c\n"; cin>>a>>b>>c; cout<<sum(a, b, c)<<endl; cout<<sum(a, b)<<endl; cout<<sum(a)<<endl; cout<<sum()<<endl; } double sum (double x, double y, double z) { return x+y+z;}

4 if the user provides 10 for a, 20 for b and 30 for c, then the previous program will print the following values: 60 (program sets x=10, y=20 and z=30) 45 (program sets x=10, y=20, and z=15 [default]) 35 (program sets x=10, y=10 [default], and z=15 [default]) 30 (program sets x=5 [default], y=10 [default], and z=15 [default])

5 Overloaded functions All functions that agree on the name of the function but do not have to agree on number of parameters and on types of parameters. Sometimes we may want to carry out the same task on different number of parameters (which might be of different data types). Instead using different names for the functions, we can use the same name via overloading technique. This should make our programs easy to read.

6 #include <iostream>
using namespace std; int min(int, int, int); double min(double, double, double); char min(char, char, char); bool min(bool, bool); void main () { cout<<min(4, 5, -1)<<endl; cout<<min(-4.5, 5.2, -1.1)<<endl; cout<<min('a', 'c', 'z')<<endl; cout<<min(true, false)<<endl; }

7 int min (int a, int b, int c) { int z = a; if(b < z) z = b; if(c < z) z = c; return z; } double min (double a, double b, double c) { double z = a;

8 char min (char a, char b, char c) { char z = a; if(b < z) z = b; if(c < z) z = c; return z; } bool min (bool a, bool b) { if (a < b ) return a; else return b; } From the type and number of arguments, the compiler will figure out the version of the function to call.

9 inline functions: The overhead associated with function calls may exceed the benefit of having functions. This is typically true for small functions that calculate expressions. C++ allows programmers to write the keyword inline before the function return type so that the compiler will assess the cost of calling that function. If the cost is little then the compiler will call that function; otherwise the compiler will replace the function call by its definition. Reason for this is that compilers want to execute code fast (efficiency).

10 #include <iostream>
using namespace std; inline int sum (int a, int b, int c) { return a+b+c;} void main () { int x, y, z; cin>>x>>y>>z; cout<<sum(x, y, z)<<endl; } From a user point of view, the behavior of the above program will be similar to the case where inline is not used.

11 Recursive functions A recursive function is a function that calls itself. Recursion has a base case and a general case. In the base case the function return a constant, in the general case, the function calls itself one or more times. Recursion relies on if statements.

12 A recursive function that calculates xy. Note that xy= x
A recursive function that calculates xy. Note that xy= x * xy-1 and x0=1 and x1=x int power (int x, int y) { if ((y == 1) return x; else if (y == 0) return 1; else return x * power(x, y-1); }

13 What can be achieved by recursion can be achieved by iteration.
xy= x * x * … * x (y times). The same power function with iteration: int power (int x, int y) { int s=1; for (int i=1; i<=y; i++) s=s*x; return s; }

14 Fibonacci numbers are defined as Fib(0)=1, fib(1) = 1, fib(2)=2, fib(3)=3, fib(4) = 5, fib(5) = 8. In general fib(n) = fib(n-1) + fib(n-2). //A recursive fib function int fib(int n) { if ((n == 1) || (n == 0)) return 1; else return fib(n-1) + fib (n-2); } Iterative fib (was solved in a previous lecture) Recursive functions are easier to code than iterative functions.

15 What is the output of the following program
What is the output of the following program? #include <iostream> using namespace std; void my_function (int &, int); void main() { int a =5; int b=10; my_function(a, b); cout<<" a ="<< a <<endl; cout<<" b = "<< b <<endl; } void my_function(int & m, int n) { m++; n--; a =6 b = 10 Press any key to continue

16 What is the output of the following program
What is the output of the following program? #include <iostream> using namespace std; int sum (int a, int b) {return a+b;} bool sum(bool a, bool b) {return a || b;} void main () { cout<<sum(2, 3)<<endl; cout<<sum(true, false)<<endl; } 5 1 Press any key to continue

17 #include <iostream> using namespace std; int sum (int n) { if (n == 1) return 1; else return n + sum(n-1); } void main () { cout<< sum(4) << endl; Answer is 10.

18 What will be printed by the following program
What will be printed by the following program? #include <iostream> using namespace std; void fun1(int &, int &, int); void main () { int a = 10, b=20, c=30; fun1(a, b, c); cout<<"a = "<<a<<endl; cout<<"b = "<<b<<endl; cout<<"c = "<<c<<endl; } void fun1(int &a, int &b, int c) { a = 50; b=60; c=70;}

19 // tower of Hanoi recursively #include <iostream> using namespace std; void solveHanoi (int n, char pegFrom='x', char temp='y', char pegTo='z’); int main() { int number; cout<<"type number of disks \n"; cin>>number; solveHanoi(number); return 0; } void solveHanoi(int n , char from , char temp, char to) if (n==1) cout << from << " > " << to << endl; else solveHanoi (n-1, from, to, temp); solveHanoi (n-1, temp, from, to); } //end function

20 1) Storage classes of varaibles
scope has to do with "where the variable is seen" storage class has to with "how long the variable exists": auto static

21 #include <iostream> using namespace std; void fun(); void main () { fun(); } void fun() { static int b=1; b++; cout<<b<<endl; The output is 2 3 4

22 Variable scopes (contexts) Variables in C++ have 4 scopes: a) File scope: these are variables defined outside any function. They are known (and therefore can be accessed) in any function that follows their definitions. These global variables (which have file scope) are suitable for global values such as PI or e. b) Block scope: these are defined inside blocks inside functions. They are known to the block in which they are defined. c) Function scope: these are variables that are defined inside functions such as main or user defined function. They are known and accessed inside the function from the point they are defined and onwards. d) Function prototype scope: these are defined inside function prototypes and are seen only by the function prototype.

23 #include <iostream.h> int a = 5; //global or file scope
Example 1: //variable scopes #include <iostream.h> int a = 5; //global or file scope void fun(int a); //function prototype scope void main () { int a = 10; //local variable to main. function scope cout<< "main a "<<a<<endl; { int a=15; //local variable to block: block scope cout<<"block a "<<a<<endl; cout<<"global a"<<::a<<endl; } cout<<"main a " << a<<endl; cout<<"global a "<< ::a<<endl; fun(a); //function call } //end main //function definition void fun (int a) { //function scope cout<<"function a "<<a<<endl; a = 20; cout<<::a<<endl;

24 The output:

25 The following diagram shows the nesting that we have in the above example:

26 Example 2: #include <iostream> Using namespace std; int x=5; //x has a file scope void fun(int a, int b); // a and b have function prototype scope void main() { int a=10; //a has function scope. Local to main int c = a+x; //c has function scope local to main // can see and use global variable x. cout<<"c = "<<c<<endl;// c will be 15 { int m =x+a; //m has block scope; block can see both global x and //variable a cout<<"m = "<<m<<endl; // m will be 15 } fun(a,x); //function call void fun(int a, int b){//a and b have function scope cout<<a+b+x<<endl; //values of local variables a and b are set by function //call in main () // also function fun() can see and use global variable x // value printed will be 20.

27 Static variables: Users can define static variables. Static variables live throughout program execution and are initialized to zero by the system. Example 1: //static variables #include <iostream> using namespace std; void temp() { static int y; //initialization is done by system cout<<y<<endl; } void main () { temp(); } The output of the previous program is 0

28 #include <iostream> using namespace std; void temp() {
static int y=1; y = y + 5; cout<<y<<endl; } void main () { temp(); } The output of the previous program is 6 11 16 The idea is that static variables live (or exist) throughout program execution and retain (keep or save) their values between function calls.

29 addone1(a); //call addone1
1) call value and call reference void addone1(int a) { a++; } void addone2 (int &a) {a++;} void main () { int a=5; cout<<"the value of a before calling any function \n"<< a <<end; addone1(a); //call addone1 cout<<"the value of a after calling addone1 \n"<<a << endl; addone2(a); // call addone2 cout<<"the value of a after calling addone2 \n" <<a << endl; }

30 #include <iostream> using namespace std; void main () { int count = 1; int &R = count; int X = count; R++; //count++; cout<<"count = "<<count <<endl; }

31 #include <iostream> using namespace std;
design a function that will return more than one value. input: length and width of a rectangle output: area, circumference #include <iostream> using namespace std; void area_cir(int x, int y, int &area, int &cir) { area= x*y; cir = 2*(x+y); } void main () { int a, b; cout<<"type in length \"; cin>>a; cout<<"type in width \n"; cin>>b; int area, cir; area_cir(a, b, area, cir); cout<< "Area = "<< area <<endl; cout<<"Circumference = "<<cir <<endl;


Download ppt "User Defined Functions"

Similar presentations


Ads by Google