In this tutorial, you will learn about different ways to solve a problem using a function in C programming .
Here these 4 programs check whether the input number by the user is a prime number or not.
The output of all these programs is the same and a function is defined in each example. However the approach of each example is different.
Note: In these programs, the input number is assumed by the user to be a positive number, because the first number is a natural number greater than one that has no divisor other than itself and the number 1. Of course, the condition of inequality with 1 has been examined in the programs.

Example 1: No submitted argument and no return value
#include <stdio.h>
void checkPrimeNumber ();
int main ()
{
checkPrimeNumber (); // No arguments submitted
return 0;
}
// indicates that this function has no return value void
void checkPrimeNumber ()
{
int n, i, flag = 0;
printf (“Enter a positive integer:”);
scanf (“% d”, & n);
if (n == 1) {
flag = 1;
}
else {
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);
}
In the check Prime Number () function, input is taken from the user, then it checks whether the number is prime or not, and finally gives a message on the screen.
Blank parentheses when calling the function; check Prime Number () in the main () function indicate that no argument is passed to the function.
The recursive type of the function is also void. This means that this function does not return any value.
Read more here:
Example 2: No argument sent but with return value
#include <stdio.h>
int getInteger ();
int main ()
{
int n, i, flag = 0;
// No arguments submitted
n = getInteger ();
if (n == 1) {
flag = 1;
}
else {
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
int getInteger ()
{
int n;
printf (“Enter a positive integer:”);
scanf (“% d”, & n);
return n;
}
Empty parentheses in the function call
n = getInteger ();
Indicates that no argument is passed to the function. The return value of the function is stored in the variable n.
Here the getInteger () function takes a number from the user and returns the same. The main () function checks whether the number is prime or not.
Example 3: With a submitted argument but no return value
#include <stdio.h>
void checkPrimeAndDisplay (int n);
int main ()
{
int n;
printf (“Enter a positive integer:”);
scanf (“% d”, & n);
// The value n is passed to the function as an argument
checkPrimeAndDisplay (n);
return 0;
}
// indicates that this function has no return value void
void checkPrimeAndDisplay (int n)
{
int i, flag = 0;
if (n == 1) {
flag = 1;
}
else {
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 correct input value is sent by the user to the checkPrimeAndDisplay () function.
Here the checkPrimeAndDisplay () function checks whether the passed argument is a prime number and displays a message at the end.
Example 4: with a submitted argument and a return value
#include <stdio.h>
int checkPrimeNumber (int n);
int main ()
{
int n, flag;
printf (“Enter a positive integer:”);
scanf (“% d”, & n);
// The value n is passed to the function as an argument
// stored flag The return value of the function in the 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 the data type int from the function
int checkPrimeNumber (int n)
{
int i;
if (n == 1)
return 1;
for (i = 2; i <= n / 2; ++ i)
{
if (n% i == 0)
return 1;
}
return 0;
}
The input number is passed from the user to the checkPrimeNumber () function. This function checks whether the submitted argument is a prime number. If it is a prime number, the function returns 0, otherwise it returns 1. The return value of the function is stored in the flag variable. Depending on whether the value of the flag variable is 0 or 1, an appropriate message is printed in the main () function.
Which method is better?
It depends on the problem. In this case, it is better to send the argument and return the value than the function (Example 4).
Each function must do a specific task. The check Prime Number () function does not take input from the user or display a message. Rather, this function only checks whether it is a prime number or not.
The difference between our store tutorials and free tutorials : First, the packs teach the latest version of the software with much more functionality. Secondly, the packs have been prepared by experts, in a completely project-oriented manner, by solving the challenges that you face in the course of practical and professional work, and by learning them, you will be fully prepared for the labor market! Free tutorials or paid tutorials in our website have only been dubbed or equivalent by non-experts!
One thought on “Types of functions in programming C”