User Defined Function In C

The format of a custom function in C appears as:

Example

return_type function_name( parameter list) {
   body of the function
}

The returntype is flexible and can be any valid C data type, while the functionname is variable and can be any valid identifier. The parameter list defines the arguments that the function receives as input, and the function body includes the statements that are executed upon calling the function.

Example:

Here is a sample custom function that accepts a pair of integer values as input and outputs the result of adding them together:

Example

#include <stdio.h>

int add(int a, int b) {
   int sum = a + b;
   return sum;
}

int main() {
   int num1 = 5, num2 = 10, result;
   result = add(num1, num2);
printf("The sum of %d and %d is %d\n", num1, num2, result);
   return 0;
}

Output:

Output

The sum of 5 and 10 is 15

Explanation:

In this instance, a function named add is specified to accept two integer parameters and yield their total. Within the primary function, two variables, num1 and num2, are initialized with the values 5 and 10 correspondingly. Subsequently, the add function is invoked with num1 and num2 as inputs, and the outcome is captured in a variable named result. Ultimately, the outcome is displayed using the printf function.

Types of User-Defined Functions

There are two categories of user-defined functions in the C programming language:

1. Function with return value

This kind of function sends a result back to the function that called it. The return statement is employed to give back a value from the function. Below is an illustration of a function that includes a return value:

Example

#include <stdio.h>

int max(int num1, int num2) {
   if (num1 > num2)
      return num1;
   else
      return num2;
}

int main() {
   int a = 10, b = 20, c;
   c = max(a, b);
printf("The maximum of %d and %d is %d\n", a, b, c);
   return 0;
}

Output:

Output

The maximum of 10 and 20 is 20

Explanation:

In this illustration, we've created a function named max, which accepts a pair of integers and outputs the greater of the two. Within the primary function, we've initialized three variables named a, b, and c. The max function is invoked with a and b as parameters, and the outcome is assigned to c. Ultimately, the outcome is displayed utilizing the printf function.

2. Function without return value

This category of function does not provide any output to the calling function. The absence of a return statement characterizes this type of function. Below is an illustration of a function that does not return a value:

Example

#include <stdio.h>

void greet() {
printf("Hello World!\n");
}

int main() {
greet();
   return 0;
}

Output:

Output

Hello World!

Explanation:

In this instance, we've established a function named greet that displays "Hello World!" on the console. Within the primary function, we've invoked the greet function, resulting in the message being displayed on the console.

Benefits of User-Defined Functions

There are numerous advantages to incorporating user-defined functions in C programming, such as:

Reusability: A key advantage of user-defined functions lies in their ability to be utilized in various sections of a program. Instead of duplicating code, you can create a function and invoke it whenever a particular task needs to be executed. This enhances code efficiency and simplifies maintenance efforts.

Modularity: Custom functions encourage modularity within a software by dividing it into more digestible segments. Every function is designed to execute a particular operation, and the software is constructed by integrating these functions. This approach simplifies comprehension of the code and facilitates troubleshooting of potential problems.

Simplified code: Introducing user-defined functions can streamline the code by encapsulating intricate operations within a singular function. This enhances code readability and comprehension, diminishing the chances of errors and enhancing the program's quality.

Enhanced testing: Breaking down a program into smaller functions simplifies the process of testing each function separately. This approach facilitates the detection and isolation of issues within the code, thereby streamlining the bug-fixing process and enhancing the program's overall quality.

Enhanced teamwork: Custom functions can enhance collaboration by simplifying the process for several developers to contribute to various sections of a program concurrently. Dividing the program into smaller functions enables each developer to concentrate on their assigned task autonomously, facilitating the integration of their contributions at a later stage.

Examples of User-Defined Functions

Here are several instances of user-created functions in the C programming language:

Example 1: Function to calculate the factorial of a number.

Example

#include <stdio.h>

int factorial(int num) {
   int fact = 1;
   for (int i = 1; i<= num; i++) {
      fact = fact * i;
   }
   return fact;
}

int main() {
   int num = 5;
   int result = factorial(num);
printf("The factorial of %d is %d\n", num, result);
   return 0;
}

Output:

Output

The factorial of 5 is 120

Explanation:

In this instance, we've created a function named factorial, which accepts an integer as a parameter and outputs the factorial value. The function employs a for loop to compute the factorial and then provides the outcome.

Example 2: Function to check if a number is even or odd.

Example

#include <stdio.h>

int is_even(int num) {
   if (num % 2 == 0) {
      return 1;
   } else {
      return 0;
   }
}

int main() {
   int num = 5;
   if (is_even(num)) {
      printf("%d is even\n", num);
   } else {
      printf("%d is odd\n", num);
   }
   return 0;
}

Output:

Output

5 is odd

Explanation:

In this instance, a function named iseven has been established to accept an integer and output 1 if the number is even, and 0 if it is odd. The primary function invokes iseven and displays the parity of the given number.

Example 3: Function to swap two integers.

Example

#include <stdio.h>

void swap(int* a, int* b) {
   int temp = *a;
   *a = *b;
   *b = temp;
}

int main() {
   int num1 = 5, num2 = 10;
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
swap(&num1, &num2);
printf("After swapping: num1 = %d,
num2 = %d\n", num1, num2);
 return 0; 
}

Output:

Output

Before swapping: num1 = 5, num2 = 10 
After swapping: num1 = 10, num2 = 5

Explanation:

In this illustration, a function named swap has been established to interchange the values of two integer pointers. The primary function invokes the swap function and displays the variables' values both pre and post swapping.

It is crucial to understand that custom functions in the C programming language can include extra parameters like pointers, arrays, and structures. This feature enhances their flexibility and enables them to handle a variety of tasks. Furthermore, custom functions can be employed to build collections of functions that can be utilized across various programs, enhancing their utility in extensive software development endeavors. In essence, custom functions represent a core element of C programming that all programmers should grasp in order to craft effective and sustainable code.

Conclusion

In summary, user-created functions in the C programming language are a robust capability that empowers users to define custom functions for executing particular tasks. Functions consist of a series of statements grouped together to accomplish a particular objective. These user-defined functions are callable from any part of the program, enhancing code modularity and readability.

The structure of a custom function in C is straightforward and comprehensible. The output_type can be any legitimate C data type, and the name of the function can be any valid label. The list of parameters defines the inputs that the function requires, while the function's block comprises the instructions executed upon function invocation.

There exist two categories of user-defined functions in C: functions that yield results and functions that do not yield results. Functions that yield results provide a value back to the calling function, whereas functions that do not yield results do not provide any value back to the calling function.

In summary, custom functions in C programming are a valuable asset that enhances modularity and readability of code. Crafting your own functions enables you to execute particular tasks and recycle code across various sections of your program.

Input Required

This code uses input(). Please provide values below: