You will discover various types of user-defined functions in C to tackle the same problem in this article.
The four programs listed below determine whether or not a user-inputted integer is a prime number.
Contents
Different Types of User-defined Functions in C
- No-argument, no-return-value functions
- Function with a return value and no parameters
- Function with no return value and no arguments
- Function with no return value and no arguments
All of these categories will be covered here, along with examples. Think of a scenario when you need to check a larger figure.
The following solution to this problem involves creating user-defined functions in the four different ways outlined above.
The result of each of the programs listed below is the same, and for each example, we’ve written a user-defined function. However, each case uses a distinct strategy that we have employed.
Example 1: No Argument Passed and No Return Value( Types of User-defined Functions in C )
Any function that we create without any parameters or a return value neither receives any data from the caller function nor produces anything.
To put it another way, we can say that the called function provides no benefit to the calling function.
#include <stdio.h> void checkPrimeNumber(); int main() { checkPrimeNumber(); // argument is not passed return 0; } // return type is void meaning doesn't return any value void checkPrimeNumber() { int n, i, flag = 0; printf("Enter a positive integer: "); scanf("%d",&n); // 0 and 1 are not prime numbers if (n == 0 || n == 1) flag = 1; 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 user enters a number, the check prime number() method determines if the number is a prime number or not, and then displays the result on the screen.
There is no argument supplied to the method at the time check prime number(); is called inside the main() function.
The function’s return type is void. Therefore, the function returns nothing.
Example 2: No Arguments Passed But Returns a Value( Types of User-defined Functions in C )
The calling function passes arguments to the called function in this type of function, but the called function just returns a value.
#include <stdio.h> int getInteger(); int main() { int n, i, flag = 0; // no argument is passed n = getInteger(); // 0 and 1 are not prime numbers if (n == 0 || n == 1) flag = 1; 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 integer entered by the user int getInteger() { int n; printf("Enter a positive integer: "); scanf("%d",&n); return n; }
The absence of an argument in the function call n = get integer(); is indicated by the empty parenthesis. Additionally, n is given the function’s return value.
The user’s input is received by the get integer() function, which then returns it. The main() function contains the logic to determine whether a given integer is a prime or not.
Example 3: Argument Passed But No Return Value( Types of User-defined Functions in C )
This kind of function does not return any value but instead passes arguments from the caller function to the called function.
#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; } // return type is void meaning doesn't return any value void checkPrimeAndDisplay(int n) { int i, flag = 0; // 0 and 1 are not prime numbers if (n == 0 || n == 1) flag = 1; 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 checkPrimeAndDisplay() function receives the integer value that the user input.
The checkPrimeAndDisplay() function in this case determines whether or not the parameter supplied is a prime integer and displays the relevant message.
Example 4: Argument Passed and Returns a Value
This is the best kind since only the logic is defined inside the function body, making the function completely independent of inputs and outputs.
#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; } // int is returned from the function int checkPrimeNumber(int n) { // 0 and 1 are not prime numbers if (n == 0 || n == 1) return 1; int i; for(i=2; i <= n/2; ++i) { if(n%i == 0) return 1; } return 0; }
The checkPrimeNumber() function receives the user’s input.
The check prime number() function determines whether or not the argument passed in is a prime number.
The function returns 0 if the provided input is a prime number. The function returns 1 if the provided input is a non-prime number. The flag variable receives the return value as its value.
Depending on whether the flag is 0 or 1, the main() method prints the relevant message.
Which strategy is superior?
The issue you’re seeking to address will determine how to proceed. It is preferable in this situation to supply an argument and have the function return a value (example 4).
A function should do a particular task. The check prime number() function does not ask the user for input and does not show the necessary message. It solely determines if an integer is prime.
This Compiler Is Available For Implementation: Compiler