Function with no argument but return something in C

  1.  There could be occasions where we may need to design functions that may not take any arguments but returns a value to the calling function. A example for this is getchar function it has no parameters but it returns an integer an integer type data that represents a character.
    Syntax :
    Function declaration : int function();
    Function call : function();
    Function definition :
                     int function()
                     {
                         statements;
                          return x;
                      }
        

    // C code for function with no arguments 
    // but have return value
    #include <math.h>
    #include <stdio.h>
      
    int sum();
    int main()
    {
        int num;
        num = sum();
        printf("\nSum of two given values = %d", num);
        return 0;
    }
      
    int sum()
    {
        int a = 50, b = 80, sum;
        sum = sqrt(a) + sqrt(b);
        return sum;
    }
    Output:
    Sum of two given values = 16

No comments:

Post a Comment

Featured Post

Core and Advance Python

                      Language Fundamentals