Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Programming in C++ Lecture Notes 9 Functions (Returning Values) Andreas Savva.

Similar presentations


Presentation on theme: "1 Programming in C++ Lecture Notes 9 Functions (Returning Values) Andreas Savva."— Presentation transcript:

1 1 Programming in C++ Lecture Notes 9 Functions (Returning Values) Andreas Savva

2 2 Functions in Mathematics f(x) = x 2 Parameters f(2) = f(-2) = f(4) = f(x,y) = x 2 +y 4 4 16 f(2,3) = f(-2,-3) = 7 1 f = 3

3 3 Functions Function None or many inputparameters Exactly one return value

4 4 Functions that we know abs(-6)= 6 sqrt(16)= 4 sin(3.14159/2)= 1 int(45.876)= 45 absx|x| F(x) = |x|

5 5 Function Structure ( ) {..... return ; } bool IsBigger (int a, int b) { return a > b; }

6 6 Example int MySqr (int x) { return x * x; }Name Formal parameter Data type of return value Return value Functions are executed when we call them: cout << MySqr(6); y = 1 + MySqr(3-1); n = 3 * MySqr(abs(sqrt(9)-5));

7 7 Function - Example #include #include using namespace std; int cube(int x) { return x * x * x; return x * x * x;} void main() { int n, x; int n, x; cout << ”Give a number: ”; cout << ”Give a number: ”; cin >> n; cin >> n; x = cube(n); x = cube(n); cout << ”The cube of ” << n cout << ”The cube of ” << n << ” is ” << x; << ” is ” << x;} Start Cube(n) Read n x = Cube(n) Display x Stopmain() Entrance return x * x * x Exit Function Cube(x)

8 8 #include using namespace std; char First (int a, int b; float c) {... return ; } void main() { int a = 1, b = 3, c = 7;... char ch = First (5, c, a); } Actual parameters Formal parameters Parameters (Arguments) Formal Formal Actual Actual

9 9 Function Example #include using namespace std; int max(int a, int b); // Function prototype int num; // Global variable void main() { cout << max(4,7); num = max(2*4-1, Sqrt(81)); cout << num; cout << max(max(4,5),8); cout << max(max(4,2),max(3,max(6,1))); } int max (int a, int b) { if (a > b) return a; else return b; } Result Result 7 9 8 6

10 10 Return return exits the function immediately and returns a value. return exits the function immediately and returns a value. int addone (int a) { return 1; cout << a + 1; a++; return a; cout << a; } Statements below this line will never be executed. Always return 1 int max(int a, int b) { if (a > b) return a; else return b; } int max(int a, int b) { if (a > b) return a; return b; } same

11 11 Be Careful int max(int a, int b) { int large; if (a > b) large = a; else large = b; return large; } int max(int a, int b) { int large; if (a > b) large = a; large = b; return large; } NOTthesame

12 12 Procedure Vs Function #include using namespace std; int num; Display void Display( ) { cout << ”I like college”; } MySqr int MySqr (int x) { return x * x; } void main( ) Display( ) Display( ); MySqr cout << MySqr(3); MySqr num = 1 + 6 * MySqr(4); } Does not return a value Returns

13 13 Constant Reference Parameters Value formal parameters copy in a new memory location the value of the actual parameter. Value formal parameters copy in a new memory location the value of the actual parameter. When we have large structures it is better to use a reference formal parameter since copying could be time-consuming and we also waist additional memory. When we have large structures it is better to use a reference formal parameter since copying could be time-consuming and we also waist additional memory. We can declare a reference formal parameter as a constant which will not allow as to change its value (for safety). We can declare a reference formal parameter as a constant which will not allow as to change its value (for safety). void invalid(const int &x) { x = 5; // Syntax ERROR }

14 14 Formal Parameters void test(int a, int &b, int *c) { a = 12; b = 23; *c = 34; } void main() { int x = 2, y = 5, z = 7; test(x, y, &z); cout << x << endl << y << endl << z; } 22334

15 15 main() is also a Function #include // standard system definitions library #include using namespace std; int main() { int x,y; cout << ”Please enter two numbers: ”; cin >> x >> y; int sum = x + y; cout << ”Their sum is ” << sum << endl; return EXIT_SUCCESS; }

16 16 main() can also take Parameters #include using namespace std; void main(int argc, char **argv) { if (argc > 1) if (strcmp(argv[1],”nicosia”)) cout << ”Not a valid password”; else cout << ”Logged in as administrator”; else cout << ”Regular user”; }

17 17 Exercise 1 Write a program that will ask the price of a product and display the discount. The discount should be returned by a function, called “Discount”, that will take the price as a formal parameter and return the discount which is 15%. Write a program that will ask the price of a product and display the discount. The discount should be returned by a function, called “Discount”, that will take the price as a formal parameter and return the discount which is 15%.

18 18 Exercise 2 Write a program to ask the base and height of a right-angle triangle and display its area. The area should be calculated and returned by a function, called “Area”, that will take the base and height as formal parameters. Write a program to ask the base and height of a right-angle triangle and display its area. The area should be calculated and returned by a function, called “Area”, that will take the base and height as formal parameters. Area = (Base x Height) / 2

19 19 Exercise 3 Write a function “Subtract” that will take two real parameters and return their difference. Also write the program that will read the numbers, call the function and display the result. Write a function “Subtract” that will take two real parameters and return their difference. Also write the program that will read the numbers, call the function and display the result.

20 20 Exercise 4 Write a function “Calculator” that will take two numbers a and b and a character, and if the character is: Write a function “Calculator” that will take two numbers a and b and a character, and if the character is: ’+’ to return a + b ’–’ to return a – b ’*’ to return a * b ’/’ to return a / b

21 21 Exercise 5 Write a function “Sum” that will take two integer numbers n and m and return the sum of all the numbers from n to m. Write a function “Sum” that will take two integer numbers n and m and return the sum of all the numbers from n to m.i.e. Sum(1,4) = 1 + 2 + 3 + 4 = 10 Sum(1,4) = 1 + 2 + 3 + 4 = 10 Sum(4,9) = 4 + 5 + 6 + 7 + 8 + 9 = 39 Sum(4,9) = 4 + 5 + 6 + 7 + 8 + 9 = 39 Sum(7,7) = 7 Sum(7,7) = 7 Sum(7,2) = 0 Sum(7,2) = 0

22 22 Exercise 6 Write a function “Month” that will take the month-number and return the month name. Write a function “Month” that will take the month-number and return the month name. i.e. Month(1) = “January” Month(1) = “January” Month(4) = “April” Month(4) = “April” Month(11) = “November” Month(11) = “November”

23 23 Exercise 7 Write a function “Teenager” that will take the age of a person and return true if is a teenager and false if not. A teenager is someone who is between 12 and 18 years old. Write a function “Teenager” that will take the age of a person and return true if is a teenager and false if not. A teenager is someone who is between 12 and 18 years old.

24 24 Exercise 8 Write a function “PI” that will return the value of π which is 3.14159. Write a function “PI” that will return the value of π which is 3.14159.

25 25 Exercise 9 Write a function “Decimal” that will return the decimal part of a number. Write a function “Decimal” that will return the decimal part of a number.Example: Given the number 13.46 the function will return the value 0.46. Given the number 13.46 the function will return the value 0.46. Hint: 13.46 - 13 = 0.46 Hint: 13.46 - 13 = 0.46 num int(num)

26 26 Exercise 10 1. Write a function “Power” to calculate the power of a given number. i.e. Power(2,3) = 2 3 = 8 Power(4,2) = 4 2 = 16 2. Write a function “Equation” to calculate the equation 3x 3 9x 5. x is a value formal parameter. 3. Using the function “Equation” write a program to calculate and display the equation 3  6 3  9  6 5.

27 27 What is the output of the following program? What is the output of the following program? Exercise 11 #include using namespace std; int StopAt(int i, int m, int n) { if (2*m-1 <= n) return i – 1; else return n – i; } void display(int n, char c) { int i, j; for (i = 1; i <= n; i++) { for (j = 1; j <= StopAt(i,i,n); j++) cout << ' '; cout << c; if (i*2-1 != n) { for (j = 1; j <= StopAt((2*i)%(n+1),n-i+1,n); j++) cout << ' '; cout << c; } cout << endl; } void main() { display(5,'+'); display(7,'?'); display(6,'0'); }


Download ppt "1 Programming in C++ Lecture Notes 9 Functions (Returning Values) Andreas Savva."

Similar presentations


Ads by Google