English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
在本教程中,您将学习使用函数来解决单个问题的不同方法。
为了更好地理解参数和函数返回,可以将用户定义的函数归类为:
考虑一种情况,您必须检查素数。通过以上述4种不同的方式制作用户定义的函数,可以解决以下问题。
#include <iostream> using namespace std; void prime(); int main() { // 没有参数传递给prime() prime(); return 0; } // 函数的返回类型为void,因为未返回值。 void prime(); { int num, i, flag = 0; cout << "Enter a positive integer for checking: "; cin >> num; for(i = 2; i <= num/2; ++i) { if (num % i == 0) { flag = 1; break; } } if (flag == 1) { cout << num << " is not a prime number."; } else { cout << num << " 是质数。"; } }
在上面的程序中,在main()中调用prime(),但没有参数。
prime() 函数用来从用户处获取输入的正数,并检查该数字是否为质数。
由于prime()返回类型为void,因此该函数不返回任何值。
#include <iostream> using namespace std; int prime(); int main() { int num, i, flag = 0; // 没有参数被传递给prime() num = prime(); for (i = 2; i <= num/2; ++i) { if (num % i == 0) { flag = 1; break; } } if (flag == 1) { cout << num << " 不是质数。"; } else { cout << num << " 是质数。"; } return 0; } // The return type of the function is int int prime() { int n; printf("Enter a positive integer for checking: "); cin >> n; return n; }
In the above program, the prime() function is called from main() without any parameters.
The prime() function gets a positive integer from the user. Since the return type of the function is int, it returns the user's input number to the main() function.
Then, check whether the number is prime and print it to the screen in the main() itself.
#include <iostream> using namespace std; void prime(int n); int main() { int num; cout << "Enter a positive integer for checking: "; cin >> num; // The parameter num is passed to the function prime() prime(num); return 0; } // The function call does not return any value. Therefore, the return type of the function is void. */ void prime(int n) { int i, flag = 0; for (i = 2; i <= n/2; ++i) { if (n % i == 0) { flag = 1; break; } } if (flag == 1) { cout << n << " is not a prime number."; } else { cout << n << " is a prime number."; } }
In the above program, the user is first asked to input a positive number, which is stored in the variable num.
Then, num is passed to the prime() function, where it checks and prints whether the number is prime or not.
Because, the return type of prime() is void, prime() function does not return any value.
#include <iostream> using namespace std; int prime(int n); int main() { int num, flag = 0; cout << "Enter a positive integer for checking: "; cin >> num; // The parameter num is passed to the check() function flag = prime(num); if(flag == 1) cout << num << " is not a prime number."; else cout << num << " is a prime number."; return 0; } /* This function returns an integer value. */ int prime(int n) { int i; for(i = 2; i <= n/2; ++i) { if(n % i == 0) return 1; } return 0; }
In the above program, a positive integer is asked from the user and stored in the variable num.
Then, pass num to the function prime(), where it checks whether the number is a prime number.
Since the return type of prime() is int, therefore the1or 0 is returned to the main() calling function. If the number is a prime number, then return1. If not, return 0.
In the main() function, the returned1or 0 is stored in the variable flag, and the corresponding text is printed to the screen.
All four programs above produce the same output and are technically correct programs.
There is no strict specification for which method to choose.
Choose a specific method based on the specific situation and how you want to solve the problem.