Types user defined functions with example in C.

Subject: PPS (Programming for Problem Solving)

Contributed By: Sanjay

Created At: February 3, 2025

Question:


Explain in detail the types of user defined functions with example.

Explanation Video:

Custom Image

Explanation:

 

Types of User-Defined Functions in C

In C, user-defined functions allow programmers to structure code into reusable blocks, improving readability and maintainability. Functions can be classified based on their return type and arguments.


Types of User-Defined Functions:

  1. Function with No Arguments and No Return Value

  2. Function with Arguments but No Return Value

  3. Function with No Arguments but Returns a Value

  4. Function with Arguments and Returns a Value


1. Function with No Arguments and No Return Value

  • The function does not take any input parameters.
  • It does not return any value.
  • It performs its task and prints the result inside the function.

Example:

#include <stdio.h>

 

void greet() { 

    printf("Hello! Welcome to C programming.\n"); 

}

 

int main() {

    greet(); // Function call

    return 0;

}

Output:

Hello! Welcome to C programming.


2. Function with Arguments but No Return Value

  • The function takes input parameters but does not return any value.
  • It performs operations inside the function and prints the result.

Example:

#include <stdio.h>

 

void add(int a, int b) { 

    printf("Sum = %d\n", a + b); 

}

 

int main() {

    add(5, 10); // Function call with arguments

    return 0;

}

Output:

Sum = 15


3. Function with No Arguments but Returns a Value

  • The function does not take any input parameters.
  • It processes data and returns a value to the calling function.

Example:

#include <stdio.h>

 

int getNumber() { 

    return 100; 

}

 

int main() {

    int num = getNumber();  // Function call

    printf("Returned Value: %d\n", num);

    return 0;

}

Output:

Returned Value: 100


4. Function with Arguments and Returns a Value

  • The function takes input parameters and returns a result.
  • This type of function is commonly used for mathematical operations.

Example:

#include <stdio.h>

 

int multiply(int a, int b) { 

    return a * b; 

}

 

int main() {

    int result = multiply(4, 5);  // Function call with arguments

    printf("Multiplication Result: %d\n", result);

    return 0;

}

Output:

Multiplication Result: 20


Conclusion:

  • No arguments, no return value  Function does everything internally.
  • Arguments, no return value  Takes input but does not return any value.
  • No arguments, returns a value  No input but returns a result.
  • Arguments, returns a value  Takes input and returns a result.
Share this Article & Support Us:
Status
printf('Loading...');