290 likes | 425 Views
Andreas Savva. Programming in C++. Lecture Notes 9 Functions (Returning Values). f(2) = f(-2) = f(4) =. f(2,3) = f(-2,-3) =. Functions in Mathematics. f(x) = x 2. 4 4 16. Parameters. f(x,y) = x 2 +y. 7 1. f = 3. Function. None or many input parameters. Exactly one
E N D
Andreas Savva Programming inC++ Lecture Notes 9 Functions (Returning Values)
f(2) = f(-2) = f(4) = f(2,3) = f(-2,-3) = Functions in Mathematics f(x) = x2 4 4 16 Parameters f(x,y) = x2+y 7 1 f = 3
Function None or many input parameters Exactly one return value Functions
x |x| abs F(x) = |x| Functions that we know abs(-6) = 6 sqrt(16) = 4 sin(3.14159/2) = 1 int(45.876) = 45
Function Structure <Data-type of return value> <Function Name>(<Formal parameters>) { . . . . . return<expression>; } bool IsBigger (int a, int b) { return a > b; }
Data type of return value Formal parameter Name Example int MySqr (int x) { return x * x; } Return value • Functions are executed when we call them: • cout << MySqr(6); • y = 1 + MySqr(3-1); • n = 3 * MySqr(abs(sqrt(9)-5));
main() Start Read n Function Cube(x) Cube(n) Entrance x = Cube(n) return x * x * x Display x Exit Stop Function - Example #include <iostream> using namespace std; int cube(int x) { return x * x * x; } void main() { int n, x; cout << ”Give a number: ”; cin >> n; x = cube(n); cout << ”The cube of ” << n << ” is ” << x; }
Formal parameters Actual parameters #include <iostream> using namespace std; char First (int a , int b; float c) { . . . return <expression>; } void main() { int a = 1, b = 3, c = 7; . . . char ch = First (5 , c , a); } Parameters (Arguments) • Formal • Actual
Function Example #include <iostream> 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 7 9 8 6
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 Return Statements below this line will never be executed. Always return 1 • return exits the function immediately and returns a value. int addone (int a) { return 1; cout << a + 1; a++; return a; cout << a; }
int max(int a,int b) { int large; if (a > b) large = a; elselarge = b; return large; } int max(int a,int b) { int large; if (a > b) large = a; large = b; return large; } NOT the same Be Careful
Procedure Vs Function #include <iostream> using namespace std; int num; voidDisplay( ) { cout << ”I like college”; } intMySqr (int x) { return x * x; } void main( ) Display( ); cout << MySqr(3); num = 1 + 6 * MySqr(4); } Does not return a value Returns a value
Constant Reference Parameters • 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. • 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 }
2 23 34 Formal Parameters void test(inta, 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; }
main() is also a Function #include <cstdlib>// standard system definitions library #include <iostream> 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; returnEXIT_SUCCESS; }
main() can also take Parameters #include <iostream> 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”; }
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%.
Exercise2 • 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
Exercise3 • 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.
Exercise4 • Write a function“Calculator”that will take two numbersaandband a character, and if the character is: ’+’ to returna + b ’–’ to returna – b ’*’ to returna * b ’/’ to returna / b
Exercise5 • Write a function “Sum”that will take two integer numbersnandmand return the sum of all the numbers from ntom. i.e. • Sum(1,4) = 1 + 2 + 3 + 4 = 10 • Sum(4,9) = 4 + 5 + 6 + 7 + 8 + 9 = 39 • Sum(7,7) = 7 • Sum(7,2) = 0
Exercise6 • Write a function“Month”that will take the month-number and return the month name. i.e. • Month(1) = “January” • Month(4) = “April” • Month(11) = “November”
Exercise7 • 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.
Exercise8 • Write a function “PI” that will return the value ofπwhich is 3.14159.
num int(num) Exercise 9 • 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. • Hint:13.46 - 13 = 0.46
Exercise 10 • Write a function “Power” to calculate the power of a given number. i.e. Power(2,3) = 23 = 8 Power(4,2) = 42 = 16 • Write a function “Equation” to calculate the equation 3x39x5. x is a value formal parameter. • Using the function “Equation” write a program to calculate and display the equation 363965.
Exercise 11 #include <iostream> using namespace std; int StopAt(int i, int m, int n) { if (2*m-1 <= n) return i – 1; elsereturn 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'); } • What is the output of the following program?