English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn to use functions to solve the same problem in different ways.
Below this4A program checks if the integer input by the user is a prime number.
The output of all these programs is the same, and in each example, we have created a user-defined function. However, the methods we use in each example are different.
#include <stdio.h> void checkPrimeNumber(); int main() { checkPrimeNumber(); //No parameters are passed return 0; } //The return type is void, indicating that no value is returned void checkPrimeNumber() { int n, i, flag = 0; printf("Enter a positive integer: "); scanf("%d", &n); for(i=2; i <= n/2; ++i) { if(n % i == 0) { flag = 1; } } if (flag == 1) printf("%d is not a prime number.", n); else printf("%d is a prime number.", n); }
The checkPrimeNumber() function receives the user's input, checks if it is a prime number, and displays it on the screen.
The empty parentheses in the brackets of checkPrimeNumber() inside the main() function indicate that no parameters are passed to the function.
The return type of the function is void. Therefore, the function does not return any value.
#include <stdio.h> int getInteger(); int main() { int n, i, flag = 0; //No parameters are passed n = getInteger(); for(i=2; i <= n/2; ++i) { if(n % i == 0){ flag = 1; break; } } if (flag == 1) printf("%d is not a prime number.", n); else printf("%d is a prime number.", n); return 0; } //Returns the integer input by the user int getInteger() { int n; printf("Enter a positive integer: "); scanf("%d", &n); return n; }
n = getInteger(); The empty parentheses in the statement indicate that no parameters are passed to the function. And, the value returned by the function will be assigned to n.
Here, the getInteger() function gets input from the user and returns it. The main() function contains code to check if the number is a prime number.
#include <stdio.h> void checkPrimeAndDisplay(int n); int main() { int n; printf("Enter a positive integer: "); scanf("%d", &n); // n is passed to the function checkPrimeAndDisplay(n); return 0; } //The return type is void, indicating that no value is returned void checkPrimeAndDisplay(int n) { int i, flag = 0; for(i=2; i <= n/2; ++i) { if(n % i == 0){ flag = 1; break; } } if (flag == 1) printf("%d is not a prime number.", n); else printf("%d is a prime number.", n); }
The integer value entered by the user will be passed to the checkPrimeAndDisplay() function.
Here, the checkPrimeAndDisplay() function checks whether the passed parameter is a prime number and displays an appropriate message.
#include <stdio.h> int checkPrimeNumber(int n); int main() { int n, flag; printf("Enter a positive integer: "); scanf("%d", &n); //n is passed to the checkPrimeNumber() function //The returned value is assigned to the flag variable flag = checkPrimeNumber(n); if (flag == 1) printf("%d is not a prime number.", n); else printf("%d is a prime number.", n); return 0; } //returns int from the function int checkPrimeNumber(int n) { int i; for(i=2; i <= n/2; ++i) { if(n % i == 0) return 1; } return 0; }
The input from the user will be passed to the checkPrimeNumber() function.
The checkPrimeNumber() function checks whether the passed parameter is a prime number.
If the passed parameter is a prime number, the function returns 0. If the passed parameter is not a prime number, the function returns1. The returned value will be assigned to the flag variable.
depending on whether flag is 0 or1The main() function will print an appropriate message.
Well, it depends on the problem you want to solve. In this case, it is better to pass parameters and return values from the function (example4)。
The function should perform a specific task. The checkPrimeNumber() function does not accept user input nor display appropriate messages. It only checks whether a number is a prime number.