User Defined Functions in C | Meaning, Types and Examples

In this blog, we will discuss about User Defined Functions in C in detail from scratch! First we will understand what are functions & then, jump into the world of User Defined Functions in C more deeply.
What are Functions in C?
Functions are a fundamental building block of C programs and are used to modularize and organize code. They provide a way to break down a program into smaller, manageable units, which makes the code more readable, reusable, and maintainable. They are of two types: Built-in and User Defined.
Also, explore: Function Prototype in C
Let’s see the types of Functions below:
1. Built-in Functions
Built-in functions in C, often referred to as library functions or standard library functions, are predefined functions that are part of the C standard library. These functions provide essential capabilities to C programmers and can be used without the need for writing their implementations.
2. User Defined Functions
User-defined functions in programming are functions created by the programmer to perform specific tasks or operations within a program. These functions are not predefined or built into the programming language but are written by the programmer to modularize code, improve code organization, enhance reusability, and make the program more understandable.
Let’s Talk About User-Defined Functions in C in Detail
They are of two types
- Function with a return value
- Function without a return value (void functions)
Parts of a User-Defined Function
- Function Return Type: The type of value that the function returns (e.g., int, float, void for no return value).
- Function Name: The name assigned to the function, which is used to call it in the program.
- Parameters: The inputs to the function, are specified within parentheses and separated by commas (optional).
- Function Body: The block of code which constitutes the function and contains statements that define what the function does.
- Return Statement: A statement which returns a value from the function to the calling code (optional, depending on return type).
Syntax of a User-Defined Function in C
return_type function_name(parameter_list) { // Function body // ... return value; // Optional, based on return type }
For Example
#include <stdio.h>
// Function Declarationint add(int a, int b);
int main(){ int result = add(8, 9); printf("The sum is a + b = %d\n", result); return 0;}
// Function Definitionint add(int a, int b){ return a + b;}
Output
The sum is a + b = 17
In the above code, a function named add is defined, which takes two integer parameters and returns their sum. The add function is declared before main, and defined after main. Inside main, the add function is called with two arguments, and its return value is printed.
Let’s Discuss About the Types of User-Defined Functions in C in detail
1. Function With a Return Value
Functions in this category return a value after execution, which can be of any data type such as int, float, char, etc.
Syntax
return_type function_name(parameter_list) { // Function body // ... return value; // The value to be returned must match the return_type }
Example 1
#include <stdio.h>
// Function Declarationfloat multiply(float a, float b);
int main(){ float result = multiply(4.5, 4.0); printf("The result is: %.2f\n", result); return 0;}
// Function Definitionfloat multiply(float a, float b){ return a * b;}
Output 1
The result is: 18.00
Example 2
#include <stdio.h>
// Function Declarationint find_max(int a, int b);
int main(){ int num1 = 2, num2 = 7; int max_value = find_max(num1, num2); printf("The maximum value between %d and %d is: %d\n", num1, num2, max_value); return 0;}
// Function Definitionint find_max(int a, int b){ if(a > b) return a; else return b;}
Output 2
The maximum value between 2 and 7 is: 7
2. Function Without a Return Value (Void Functions)
Void functions are those which do not return any value. They are used for tasks that don’t require a return value to the calling function. They are declared with the void keyword as the return type.
Syntax
void function_name(parameter_list) { // Function body // ... }
Example 1
#include <stdio.h>
// Function Declarationvoid greet(char name[]);
int main(){ greet("Esha"); return 0;}
// Function Definitionvoid greet(char name[]){ printf("Heya, %s!\n", name);}
Output 1
Heya, Esha!
Example 2
#include <stdio.h>
// Function Declarationvoid display_triangle(int rows);
int main(){ display_triangle(7); return 0;}
// Function Definitionvoid display_triangle(int rows){ for(int i = 1; i <= rows; i++) { for(int j = 1; j <= i; j++) { printf("*"); } printf("\n"); }}
Output 2
* ** *** **** ***** ****** *******
Conclusion
Thus, User-Defined Functions in C allow programmers to break down complex problems into smaller, manageable pieces, which promotes code reuse, improves readability, and eases maintenance.
Boost Your Learning:
If you’re serious about mastering C, consider enrolling in an online course. Here are some top recommendations:
- Coursera’s “C for Everyone” Series: A comprehensive introduction to C, suitable for both beginners and those looking to refresh their knowledge.
- Udemy’s “C Programming For Beginners”: This course takes a hands-on approach, ensuring you get practical experience as you learn.
By investing time in a structured course, you’ll gain a more profound understanding, benefit from expert insights, and have a clear path of progression.
So, gear up and dive deeper into the world of C programming. The journey you’ve embarked on is filled with challenges, but the rewards, in terms of knowledge and skills, are immeasurable. Happy coding!
Download this article as PDF to read offline
Download as PDF
I'm Esha Gupta, a B.Tech graduate in Computer Science & Engineering, specializing in Front End Web Dev. I've interned at GeeksforGeeks & Coding Minutes, fueling my passion for crafting appealing and functional digit... Read Full Bio
Comments