Extra Functions Function Prototype Syntax returntype functionname type

  • Slides: 18
Download presentation
Extra

Extra

Functions Function Prototype Syntax return_type function_name ( [type [parameter. Name]]. . . ); Function

Functions Function Prototype Syntax return_type function_name ( [type [parameter. Name]]. . . ); Function Definition Syntax return_type function_name ( [type parameter. Name]. . . ) { statements; //function body }

#include <iostream> using namespace std; double Find. Area(double length, double width); //function prototype void

#include <iostream> using namespace std; double Find. Area(double length, double width); //function prototype void main() { double length. Of. Yard; double width. Of. Yard; double area. Of. Yard; cout << "n. How wide is your yard? "; cin >> width. Of. Yard; cout << "n. How long is your yard? "; cin >> length. Of. Yard; area. Of. Yard= Find. Area(length. Of. Yard, width. Of. Yard); cout<< "n. Your yard is " <<area. Of. Yard<< " square meternn"; } double Find. Area(double l, double w) { return l * w; }

#include <iostream> using namespace std ; void my. Func(); int x = 6; void

#include <iostream> using namespace std ; void my. Func(); int x = 6; void main() { cout << "n In main x is: " << x; { int x = 5; cout << "n In main x is: " << x; my. Func(); cout << "n Back in main, x is: " << x; } cout << "n In main x is: " << x; } void my. Func() { int x = 8; cout << "n In my. Func, local x: " << x << endl; { cout << "n In block in my. Func, x is: " << x; int x = 9; cout << "n. Very local x: " << x; } cout << "n. Out of block, in my. Func, x: " << x << endl; }

Exercises • Write the prototype for a function named Perimeter(), which returns int and

Exercises • Write the prototype for a function named Perimeter(), which returns int and that takes two parameters, both ints. • Write the definition of the function Perimeter() The two parameters represent the length and width of a rectangle. Have the function return the perimeter (twice the length plus twice the width).

Overloading Functions C++ enables you to create more than one function with the same

Overloading Functions C++ enables you to create more than one function with the same name. This is called function overloading. The functions must differ in their parameter list, with a different type of parameter, a different number of parameters, or both. Here's an example: int my. Function (int x, int y); int my. Function (long x, long y); int my. Function (long z); The return types can be the same or different on overloaded functions. You should note that two functions with the same name and parameter list, but different return types, generate a compiler error. int my. Function (int x, int y); long my. Function (int x, int y);

Exercises What is wrong with the following code? #include <iostream> using namespace std ;

Exercises What is wrong with the following code? #include <iostream> using namespace std ; void my. Func(int x); int main() { int x, y; y = my. Func(6); cout << "x: " << x << " y: " << y << "n"; } void my. Func(int x) { return (4*x); }

Exercises What is wrong with the following code? #include <iostream> using namespace std ;

Exercises What is wrong with the following code? #include <iostream> using namespace std ; int my. Func( int x); int main() { int x, y; y = my. Func(x); cout << "x: " << x << " y: " << y << "n"; } int my. Func(int x); { return (4*x); }

Exercises Write a programme contain a function that takes two integer arguments and returns

Exercises Write a programme contain a function that takes two integer arguments and returns the result of dividing the first by the second. Do not do the division if the second number is zero, but do return -1. In main asks the user for two numbers and calls the function Print the answer, or print an error message if you get -1.

call by reference The call by reference method of passing arguments to a function,

call by reference The call by reference method of passing arguments to a function, copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument. void Add. One(int &y) { y = y + 1; }

#include <iostream> using namespace std; // function declaration void swap(int &x, int &y); void

#include <iostream> using namespace std; // function declaration void swap(int &x, int &y); void main () { // local variable declaration: int a = 100; int b = 200; cout << "Before swap, value of a : " << a << endl; cout << "Before swap, value of b : " << b << endl; swap(a, b); cout << "After swap, value of a : " << a << endl; cout << "After swap, value of b : " << b << endl; } void swap(int &x, int &y) { int temp; temp = x; x = y; y = temp; }

Trace the program. class Pet{public: void speak(){cout << "Growl" << endl; }}; class Rat:

Trace the program. class Pet{public: void speak(){cout << "Growl" << endl; }}; class Rat: public Pet {public: void speak(){cout << "Rat noise" << endl; }}; class Cat: public Pet {Public: void speak(){cout << "Meow" << endl; }}; void chorus(Pet pt, Pet *pet. Ptr, Pet &pet. Ref) { pt. speak(); pet. Ptr->speak(); pet. Ref. speak(); } void main() { Pet *ptr; //Pointer to base class ptr = new Pet; chorus(*ptr, *ptr); //////////////////// ptr = new Rat; chorus(*ptr, *ptr); ///////////////////// ptr = new Cat; chorus(*ptr, *ptr); } delete ptr;

 • Convert Speak() function to virtual function then trace the program again. class

• Convert Speak() function to virtual function then trace the program again. class Pet {public: virtual void speak(){cout << "Growl" << endl; }};

 • Change class pet to be an abstract pet. class Pet{Public: virtual void

• Change class pet to be an abstract pet. class Pet{Public: virtual void speak()=0; }; What will you change in the main ? ?