Search This Blog

Thursday, January 4, 2024

Functions in ‘C’ Part 2:Categories of user-defined functions

 

Categories of user-defined functions

All C functions can be called either with arguments or without arguments in a C program. Also, they may or may not return any values.

A function depending on whether arguments are present or not and whether value is returned or not, may belong to one of the following categories:

  1. Function with no arguments and no return value

  2. Function with no arguments and a return value

  3. Function with arguments and no return value

  4. Function with arguments and a return value



  1. Function with no arguments and no return value

When a function has no arguments, it does not receive any data from the calling function. Similarly when it does not return a value, the calling function does not receive any data from the called function.

Syntax:

Function declaration : void function();

Function call : function();

Function definition :

void function()

{

     //statements;

}


Below is an example of a function, which takes 2 numbers as input from user, and display which is the greater number.


#include<stdio.h>

void greatNum();       // function declaration

int main()

{

    greatNum();        // function call

    return 0;

}

void greatNum()        // function definition

{

    int i, j;

    printf("Enter 2 numbers that you want to compare...");

    scanf("%d%d", &i, &j);

    if(i > j) {

        printf("The greater number is: %d", i);

    }

    else {

        printf("The greater number is: %d", j);

    }

}


Output:


Enter 2 numbers that you want to compare...

10 20

The greater number is: 20



  1. Function with no arguments and a return value

There could be occasions where we may need to design functions that may not take any arguments but return a value to the calling function.

An example for this is getchar() function it has no parameters but it returns an integer type data that represents a character.

Syntax:

Function declaration: return_type function ();

Function call: function();

Function definition:

return_type function( ) {

//statements;

return x;

}


Below is an example of a function with no arguments but have return value


// C code for function with no arguments

// but have return value

#include<stdio.h>

int greatNum();       // function declaration

int main()

{

    int r = greatNum();        // function call

    printf("The greater number is: %d", r);

    return 0;

}

int greatNum()        // function definition

{

    int i, j;

    printf("Enter 2 numbers that you want to compare...");

    scanf("%d%d", &i, &j);

    if(i > j) 

        return  i;

    else 

        return  j;

}

Output:

Enter 2 numbers that you want to compare...

10

20

The greater number is: 20

  1. Function with arguments and no return value

When a function has arguments, it receives data from the calling function but it returns no values.


Syntax:

Function declaration :

         void function (data_type arg1,data_type arg2,.. );

Function call :

         function(arg1,arg2,..  );

Function definition:

         void function(data_type arg1,data_type arg2,..  )

         {

               statements;

         }


Below is an example of a function with arguments but no return value


// C code for function with arguments

// and no return value

#include<stdio.h>

void greatNum(int,int);       // function declaration

int main()

{

    int i, j;

    printf("Enter 2 numbers that you want to compare...");

    scanf("%d%d", &i, &j);

    greatNum(i,j);        // function call

    

    return 0;

}

void greatNum(int n1,int n2)        // function definition

{

    if(n1 > n2) 

       printf("The greater number is: %d", n1);

    else 

        printf("The greater number is: %d", n2);

}


Output:

Enter 2 numbers that you want to compare...

10 20

The greater number is: 20


  1. Function with arguments and a return value

Syntax:

Function declaration :

int function (data_type arg1,data_type arg2,..);

Function call : 

function( arg1, arg2,..  );

Function definition:

return_type function(data_type arg1,data_type arg2,..  )

{

statements;

return x;

}

Below is an example of a function with arguments and return value


// C code for function with arguments

// and return value

#include<stdio.h>

int greatNum(int,int);       // function declaration

int main()

{

    int i, j, r;

    printf("Enter 2 numbers that you want to compare...");

    scanf("%d%d", &i, &j);

    r = greatNum(i,j);        // function call

    printf("The greater number is: %d", r);

    return 0;

}

int greatNum(int n1,int n2)        // function definition

{

    if(n1 > n2) 

       return n1;

    else 

  return n2;

}


Output:

Enter 2 numbers that you want to compare...

10

20

The greater number is: 20


No comments:

Post a Comment

Function in C Part 4: Recursion Function, Nested Functions

  Recursion Function A function that calls itself is known as a recursive function. And, this technique is known as recursion. The recursion...