Download presentation
Presentation is loading. Please wait.
Published byMeghan Norman Modified over 8 years ago
1
April 11, 2005 More about Functions
2
1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function header? 3.At will the output of the following program be if the user enters 10?
3
#include using namespace std; void func1() { cout << “Able was I.” << endl; } void func2() { cout << “I saw Elba.” << endl; } int main() { int input; cout << “Enter a number:” << endl; cin >> input; if (input < 10) { func1(); func2(); } else { func2(); func1(); } return 0; }
4
4) To qualify for a credit card at a certain bank you need: 1.To have been working at your current job for at least two years. And 2.To make at least $50,000. The following code skeleton determines if a person qualifies for a credit card. There are two functions that are unfinished 1.qualify() and 2.noQualify(); Write the definition of these two methods qualify should explain that the applicant qualifies and that the annual interest is 12% noQualify should explain that the applicant does not qualify and give a general explanation why.
5
#include using namespace std; int main() { float salary; int years; cout << “This program will determine if you qualify” << endl; cout << “for our credit card.” << endl; cout << “What is your annual salary?” << endl; cin >> salary; cout << “How many years have you worked at your current job?” << endl; cin >> years; if (salary >= 50,000 && years >= 2) qualify(); else noQualify(); return 0; }
6
5) Which of the following is the function prototype, the function header, and the function call. void showNum(float num) void showNum(float); showNum(45.67); 6)Write a function called timesTen that takes an int parameter and displays the number times times ten on the screen.
7
7)Write a function prototype for the timesTen() function. 8) What is the output of the following program? #include using namespace std; void showDouble(int); int main() { int num; for (num=0; num<10; num++) showDouble(num); return 0; } void showDouble(int value) { cout << value << “\t” << (value * 2) << endl; }
8
9) What is the output of the following program? #include using namespace std; void func1(float, int); int main() { int x = 0; float y = 1.5; cout << x << “ “ << y << endl; func1(y,x); cout << x << “ “ << y << endl; return 0; } void func1(float a, int b) { cout << a << “ ” << b << endl; a = 0.0; b=10; cout << a << “ ” << b << endl; }
9
10)How many return values may a function have? 11) Write a header for a function named distance. The function should return a float and have two float parameters, rate and time. 12) Write a header for a function named days. The function should return an int and have three int parameters, years, months (with a default value of 12) and weeks. 13) Write a header for a function called getKey. The function should return a char and use no parameters. 14) Whats the difference between a static local variable and a global variable?
10
15) What is the output of the following program? #include using namespace std; void myFunc(); int main() { int var = 100; cout << var << endl; myFunc(); cout << var << endl; return 0; } void myFunc() { int var = 50; cout << var << endl; }
11
16) What is the output of the following program? #include using namespace std; Void showVar(); int main() { for (int count=0; count < 10; count++) showVar(); return 0; } void showVar() { static int var = 10; cout << var << endl; var++; }
12
17) Write the prototype and header for a function called compute. The function should have three parameters: an int a float and a long (not necessarily in that order). The int parameter should have a default value of 5, and the long parameter should have a default value of 65536. The float parameter should not have a default value.
13
18)What is the output of the following code? #include using namespace std; void test(int=2, int=4, int=6); int main() { test(); test(6); test(3,9); test(1,5,7); return 0; } void test(int first, int second, int third) { first +=3; second+=6; third+=9; cout << first << “ “ << second << “ “ << third << endl; }
14
Looping Review C++ has the following looping looping constructs. for loop – This is good when you know the exact number of iterations ahead of time. while loop – a pretest loop. This is good for cases where you don’t want the loop to run if the condition is false from the beginning. do-while loop – a posttest loop. This is good for cases where you want the loop always to run the first time.
15
for loop for (int i=0; i<100; i++) { cout << i*i << endl; } You can also do this: int i=0; for (i=0; i<100; i++) { cout << i*i << endl; }
16
for loop You can also do this: int i=0; for (; i<100; i++) { cout << i*i << endl; } You can also do this: for (int i=0; i<100; i++) cout << i*i << endl;
17
Nested for loops for (int i=0; i<100; i++) { for (int j=0; j<10; j++) { cout << i*j << endl; }
18
while loop int i=0; while (i<10) { cout << i << endl; i++; }
19
while loop int i=0; while (i>0) { cout << i << endl; i++; }
20
do-while loop int i=0; do { cout << i << endl; i++; } while (i>0);
21
do-while loop int i=-1; do { cout << i << endl; i++; } while (i>0);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.