in , ,

Types of User-defined Functions in C Programming

Types of User-defined Functions in C Programming
Types of User-defined Functions in C Programming

Types of User-defined Functions in C Programming

In this tutorial, you will find out about various approaches you can take to solve some issues using functions.

These 4 problems beneath check whether the integer entered by the user is a prime number or not.

The output of every one of these projects beneath is the equivalent, and we have made a user-defined function in each example. However, the approach we have taken in every model is unique.


Example 1: No arguments passed and no return value

#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);

    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() work takes contribution from the user, checks whether it is a prime number or not, and shows it on the screen.

The vacant enclosures in checkPrimeNumber(); explanation inside the fundamental() work demonstrates that no contention is passed to the capacity.

The arrival sort of the function is void. Thus, no worth comes back from the function.


Example 2: No arguments passed but a return value

#include <stdio.h>
int getInteger();

int main()
{
    int n, i, flag = 0;

   // no argument is 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 integer entered by the user
int getInteger()       
{
    int n;

    printf("Enter a positive integer: ");
    scanf("%d",&n);

    return n;
}

The empty parentheses in the n = getInteger(); statement indicates that no argument is passed to the function. And, the value returned from the function is assigned to n.

Here, the getInteger() function takes input from the user and returns it. The code to check whether a number is prime or not is inside the main() function.


Example 3: Argument passed but no return value

#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;

    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 is passed to the checkPrimeAndDisplay() work.

Here, the checkPrimeAndDisplay() work checks whether the contention passed is a prime number or not and shows the appropriate message.


Example 4: Argument passed and a return value

#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)
{
    int i;

    for(i=2; i <= n/2; ++i)
    {
        if(n%i == 0)
            return 1;
    }

    return 0;
}

The input from the user is passed to the checkPrimeNumber() function.

The checkPrimeNumber() function checks whether the passed argument is prime or not.

If the passed argument is a prime number, the function returns 0. If the passed argument is a non-prime number, the function returns 1. The return value is assigned to the flag variable.

Depending on whether the flag is 0 or 1, an appropriate message is printed from the main() function.


Which approach is better?

Well, it depends on the difficulty you are attempting to solve. in this case, passing contention and restoring an incentive from the function (example 4) is better.

A function ought to play out a particular undertaking. The checkprimenumber() work doesn’t take input from the client nor it show the appropriate message. It just checks whether a number is prime or not.

Please feel free to give your comment if you face any difficulty here.


salman khan

Written by worldofitech

Leave a Reply

How to Make/Create Android Mobile Apps Without Coding Online/Offline

How to Make/Create Android Mobile Apps Without Coding Online/Offline

Best Backlink Checker Tools

Best Backlink Checker Tools ( Free & Paid)