> weight; moon(weight); jupiter(weight); return 0; }"> > weight; moon(weight); jupiter(weight); return 0; }">
Download presentation
Presentation is loading. Please wait.
Published byJoanna Patterson Modified over 8 years ago
1
Passing Value Passing by Value (or by Copy) main() { int i = 5; pr_it(i); return 0; } pr_it(int i) { cout << i; return; } 5 5
2
//Filename: test1.cpp //Calculates the user's weight in multiple functions #include voidmoon( int earth_weight) { int moon_weight; moon_weight = earth_weight / 6; cout << "You weight only " << moon_weight << " pounds on the moon!\n"; return; } voidjupiter( int earth_weight) { float jupiter_weight; jupiter_weight = earth_weight * 2.64; cout << "You weight " << jupiter_weight << " pounds on Jupiter!\n"; return; } 98
3
main() { int weight; cout << "How many pounds do you weight? "; cin >> weight; moon(weight); jupiter(weight); return 0; }
4
//Filename: Test2.cpp //Gets grade information for a student #include void check_grade( char lgrade, float average, int tests) { switch(tests) { case 0: cout << "You will get your current grade of " << lgrade << "\n"; break; case 1: cout << "You still have time to "; cout << "bring your average "; cout << "of " << average << " up."; cout << "\nStudy hard!\n"; break; default: cout << "Relax - You still have "; cout << "plenty of time. \n"; break; } return; } 99
5
main() { char lgrade; int tests; float average; cout << "What letter grade do you want? "; cin >> lgrade; cout << "What is your current test average(1-4)? "; cin >> tests; check_grade(lgrade, average, tests); return 0; }
6
Passing by Address or Passing by Reference //Filename: Test3.cpp //Program that passes by address #include void change_it(char c[4]) { cout << c << "\n"; strcpy(c,"USA"); return; } main() { char name[4] = "ABC"; change_it(name); cout << name << "\n"; return 0; }
7
Program example of passing array //Filename: test4.cpp //Gets a name in an array and then prints it, //using separate functions #include void get_name(char name[25]) { cout << "What is your first name? "; cin >> name; return; } 102
8
void print_name(char name[25]) { cout << "\n\n Here it is : " << name << "\n"; return; } main() { char name[25]; get_name(name); print_name(name); return 0; }
9
Passing Non-arrays by Address //Filename:Test5.cpp //Demonstrates passing nonarrays by address #include void do_fun( int *amt) { *amt = 85; cout << "In do_fun(), amt is " << *amt << "\n"; return; } main() { int amt; amt = 100; cout << "In main(), amt is " << amt << "\n"; do_fun(&amt); cout << "After return, amt is " << amt << " in main()\n"; return 0; } 104
10
C++ References (Reference Passing) //Filename:Test6.cpp //Gets user's weight into a passed reference variable #include void get_weight( int &weight) { cout << "How much do you weight ? "; cin >> weight; return; } main() { int user_weight; get_weight(user_weight); cout << "\nYou said that you weigh" << user_weight << " pounds. \n"; return 0; } 106
11
Function Return Values รูปแบบ return (return value);
12
//Filename:Test7.cpp //Doubles the user's number #include int doub(int num) { int d_num; d_num = num * 2; return (d_num); } main() { int number; int d_number; cout << "What number do you want to doubles? "; cin >> number; d_number = doub(number); cout << number << " doubled is " << d_number << "\n"; return 0; } 108
13
//Filename: Test8.cpp //Finds minimum and maximum values in functions #include int maximum(int num1, int num2) { int max; max = (num1 > num2) ? (num1) : (num2); return (max); } int minimum(int num1, int num2) { int min; min = (num1 < num2) ? (num1) : (num2); return (min); } 109
14
main() { int num1, num2; int min, max; cout << "Please type two numbers (such as 46, 75)\n"; cout << "Please Enter after each number.\n"; cout "; cin >> num1; cout "; cin >> num2; max = maximum(num1, num2); min = minimum(num1, num2); cout << "\n The minimum number is " << min << "\n"; cout << "\n The maximum number is " << max << "\n"; return 0; }
15
Function Prototypes The word prototype is sometimes defined as a model. In C++, a function prototype models the actual function. Before completing your study of functions, parameters, and return values, you must understand how to prototype each function in a program. You should prototype all functions in your programs. By prototyping them, you inform C++ of the function’s parameter types and its return value, if any. You do not always need to prototype functions, but it is always recommended. Sometimes a prototype is mandatory before your functions will work properly. If you run the following program, the Visual C++ compiler refuses to compile it. Yet this program seems like many others you have seen. The primary difference is that you referred to the function before it occurred in your code. In other words, the function has not been yet prototyped. C++ requires that each function be prototyped before you can refer to it.
16
//Filename:Test9.cpp //Converts the user's Celsius to Fahrenheit #include main() { float c_temp; float f_temp; cout << "What is the Celsius temparature to convert? "; cin >> c_temp; f_temp = convert(c_temp); cout << "The Fahrenheit equivalent is " << setprecision(2) << f_temp << "\n"; return 0; } float convert(float c_temp) { float f_temp; f_temp = c_temp * (9.0 / 5.0) + 32.0; return (f_temp); } 110
17
//Filename:Test9.cpp //Converts the user's Celsius to Fahrenheit #include float convert(float c_type); main() { float c_temp; float f_temp; cout << "What is the Celsius temparature to convert? "; cin >> c_temp; f_temp = convert(c_temp); cout << "The Fahrenheit equivalent is " << setprecision(2) << f_temp << "\n"; return 0; } float convert(float c_temp) { float f_temp; f_temp = c_temp * (9.0 / 5.0) + 32.0; return (f_temp); }
18
//Filename:Test10.cpp //Prints the ASCII character of the user's number //Prototype follow #include char ascii(int num); int main(void) { int num; char asc_char; cout << "What is an ASCII number? "; cin >> num; asc_char = ascii(num); cout << "The ASCII character for " << num << " is " << asc_char << "\n"; return 0; } char ascii(int num) { char asc_char; asc_char = (char)num; return (asc_char); } 112
19
จากการบ้าน ฟังก์ชัน ครั้งที่ 5 ทำการเปลี่ยนโปรแกรม ให้เป็นโปรแกรมฟังก์ชันการส่ง ค่าแบบต่าง ๆ - by value - by Reference - passing array - by Address - Reference Passing
20
การบ้าน การส่งค่า ฟังก์ชัน 1. จงเขียน โปรแกรมเพื่อรับค่าเลขจำนวนเต็มของ matrix A และ matrix B ขนาด 2*2 และ สร้างฟังก์ชันคำนวณ A*B, A+B, A-B 2. จงทำการบ้านในหนังสือ บทที่ 6 ข้อที่ 1( หน้า 114- 115)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.