Categories of Function Declaration in C Programming

In C programming, functions can be classified into four categories based on whether they take arguments and whether they return a value. This helps in organizing how functions behave with respect to input (arguments) and output (return type). The four categories are:

  1. Four categories
    • Functions with Return Type and Arguments
    • Functions with Return Type and No Arguments
    • Functions with No Return Type but with Arguments
    • Functions with No Return Type and No Arguments

Let’s explore each category with examples:


1. Functions with Return Type and Arguments

  • Description: These functions accept arguments and return a value. This is the most common type of function, where you provide inputs (arguments), and the function processes these inputs and returns a result.
  • Use Case: This is useful when you need a function to take inputs and give back a result.

Example:

  • Explanation: The add() function takes two integer arguments and returns their sum as an integer.

Key Points:

  • Return Type: int
  • Arguments: Two integers (int a, int b)

2. Functions with Return Type and No Arguments

  • Description: These functions do not take any arguments but return a value. The function does not need any input from the caller but provides a result when called.
  • Use Case: Useful when the result does not depend on external inputs but still needs to be returned.

Example:

  • Explanation: The get_five() function doesn’t need any input but always returns the value 5.

Key Points:

  • Return Type: int
  • Arguments: None

3. Functions with No Return Type but with Arguments

  • Description: These functions accept arguments but do not return a value. These are typically used for performing operations or actions without needing to return a result.
  • Use Case: Useful when you want the function to perform a task, like printing something or updating a global variable, without returning a value.

Example:

  • Explanation: The display_sum() function takes two integers as input and prints their sum, but it does not return any value.

Key Points:

  • Return Type: void (no return)
  • Arguments: Two integers (int a, int b)

4. Functions with No Return Type and No Arguments

  • Description: These functions do not take any arguments and do not return a value. They are used to perform tasks that do not need any input or return values.
  • Use Case: Useful when a function simply performs an action, like printing a message or setting a value, without needing to take input or return output.

Example:

  • Explanation: The say_hello() function takes no input and prints a message but does not return any value.

Key Points:

  • Return Type: void (no return)
  • Arguments: None

Summary of Function Categories

CategoryReturn TypeArgumentsExample
Functions with Return Type and ArgumentsYesYesint add(int a, int b)
Functions with Return Type and No ArgumentsYesNoint get_five(void)
Functions with No Return Type but with ArgumentsNo (void)Yesvoid display_sum(int a, int b)
Functions with No Return Type and No ArgumentsNo (void)Novoid say_hello(void)

When to Use Each Type:

  • With Return Type and Arguments: When you need to process input and return a result.
  • With Return Type and No Arguments: When the function does not require any input but returns a value (e.g., retrieving constant values).
  • With No Return Type but with Arguments: When you need to perform an action or operation using the input but don’t need to return a result (e.g., displaying output).
  • With No Return Type and No Arguments: When no input is required, and no value needs to be returned (e.g., printing static messages).

Understanding these categories helps in writing clear and effective functions based on the needs of your program!

error: Content is protected !!
Open chat
1
Hi,how Can We Help You ?