For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
Tired of writing the same block of code again and again in C?
That’s exactly why you need to use functions in C.
Functions in C are self-contained blocks of code that perform a specific task. Instead of repeating code, you can create a function once and call it whenever needed. This makes your programs modular, clean, and easier to debug or update. Functions also help break down large problems into smaller, manageable parts.
In this tutorial, you’ll learn the basics of functions in C, including syntax, declaration, definition, and calling. We’ll also cover different types—like built-in, user-defined, void, and functions with arguments and return values. Real code examples will help you understand how functions are used in real applications.
By the end, you’ll be able to write and use functions to simplify any C program. Want to master core C concepts through real-world projects? Check out our Software Engineering Courses designed to turn your basics into industry-ready skills.
Functions in C programming allow you to divide your code into smaller, manageable parts, making it easier to maintain and debug. The compiler uses functions to organize the code during the compilation process, ensuring that each part is executed in the right order.
Let's break down the key aspects of functions in C with examples and see how each part fits together.
A function in C consists of several important components:
You’ve done great mastering the fundamentals of C! Why not take the next step and dive deeper into today’s most sought-after technologies? Here are three impactful options:
Here's the basic syntax structure of a function in C:
return_type function_name(parameter1, parameter2, ...)
{
// Function body
// Perform the desired action and return a value if applicable
return value; // optional, depending on the return type
}
Now, let’s look at an example that demonstrates each aspect of a function in action:
Example: Calculating the Area of a Rectangle
#include <stdio.h>
// Function declaration
float calculateArea(float length, float width); // Function prototype
int main() {
// Variables for length and width
float length = 5.5;
float width = 3.2;
// Function call
float area = calculateArea(length, width); // Calling the function
// Output the result
printf("The area of the rectangle is: %.2f\n", area); // Print the result
return 0;
}
// Function definition
float calculateArea(float length, float width) {
// Function body: Calculating the area
return length * width; // Return the calculated area
}
Output:
The area of the rectangle is: 17.60
Explanation:
Once you understand these elements, it's important to know where to define a function in your code. The location of function definitions plays a critical role in organizing your program and ensuring its scalability.
Functions can be defined in source files (.c) or header files (.h), depending on how you want to organize and use them across your program.
Let’s break down the two primary locations for defining functions:
The most common place to define functions is directly in source files (.c). You’ll typically define functions in the source file where they are used. This is the simplest approach, especially for smaller programs, as it keeps the logic of the function and its usage within the same file.
Why is this useful?
Example:
#include <stdio.h>
// Function definition in the source file
int add(int a, int b) {
return a + b;
}
int main() {
printf("Sum: %d\n", add(5, 3)); // Function call
return 0;
}
Explanation:
If you plan to use a function across multiple source files, you can declare the function prototype in a header file (.h). The actual definition of the function, however, should still reside in the source file (.c). This practice allows you to share function declarations among different source files.
Why is this useful?
Example:
Function prototype in the header file (math_functions.h):
// math_functions.h
int add(int a, int b); // Function prototype
Function definition in the source file (math_functions.c):
#include "math_functions.h"
// Function definition in source file
int add(int a, int b) {
return a + b;
}
Using the function in the main file (main.c):
#include <stdio.h>
#include "math_functions.h" // Include header file for function prototype
int main() {
printf("Sum: %d\n", add(5, 3)); // Function call
return 0;
}
Explanation:
Understanding these aspects is key to writing well-structured functions.
But what happens if you attempt to use a function before it’s declared? Let’s explore this important aspect next.
If you call a function before its declaration, the compiler will produce an error since it doesn’t know the function's details. Here's an example to demonstrate the issue:
#include <stdio.h>
int main() {
printf("Sum: %d", add(5, 3)); // Error: add() function is called before declaration
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
Error:
error: 'add' undeclared
You can fix this by using a forward declaration to inform the compiler of the function's signature.
Example:
#include <stdio.h>
// Forward declaration
int add(int, int);
int main() {
printf("Sum: %d", add(5, 3));
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
Output:
Sum: 8
Explanation:
Also Read: Compiler vs Interpreter: Difference Between Compiler and Interpreter
Now that you’re clear on the essentials, let’s look at the types of functions in C programming.
There are 2 types of functions in C: Library Functions and User-defined Functions. Both types serve different purposes and offer flexibility depending on the task at hand.
Library functions are pre-defined functions that come with the C Standard Library. They are ready-to-use functions that perform a specific task, such as mathematical operations, input/output operations, and string manipulation.
Some important library functions include:
User-defined functions are functions that you define yourself. These functions allow you to break down your code into modular pieces, making it easier to manage, test, and reuse.
Key Points for User-defined Functions:
Also Read: Top 9 Popular String Functions in C with Examples Every Programmer Should Know in 2025
Now that we've covered the function structure, let's move on to understanding return types and arguments, which are essential for defining the behavior of your functions.
In C, functions can vary in terms of their arguments and return types. The return type indicates what kind of data the function returns (if any), while arguments are the inputs the function accepts.
Functions can be classified into four major categories based on these conditions.
This type of function does not accept any parameters and does not return any value. It is often used for tasks that perform actions without needing inputs or outputs.
Let’s look at an example:
#include <stdio.h>
// Function with no arguments and no return value
void displayMessage() {
printf("Hello, this is a simple function!\n");
}
int main() {
displayMessage(); // Call the function
return 0;
}
Output:
Hello, this is a simple function!
Explanation:
This type of function does not take any parameters but returns a value of a specified type. It can be used for calculations or fetching data where no input is required.
Example:
#include <stdio.h>
// Function with no arguments and with return value
int getNumber() {
return 42; // Return a fixed number
}
int main() {
int num = getNumber(); // Call the function and store the return value
printf("Returned value: %d\n", num);
return 0;
}
Output:
Returned value: 42
Explanation:
This function takes parameters but does not return a value. It is useful for operations like modifying data or printing results based on input arguments.
Example:
#include <stdio.h>
// Function with arguments and no return value
void printSquare(int number) {
printf("Square of %d is %d\n", number, number * number);
}
int main() {
printSquare(5); // Call the function with argument
return 0;
}
Output:
Square of 5 is 25
Explanation:
This type of function accepts arguments and returns a value, making it suitable for operations like mathematical calculations or any scenario where the function needs input and produces output.
Example:
#include <stdio.h>
// Function with arguments and with return value
int add(int a, int b) {
return a + b; // Return the sum of two numbers
}
int main() {
int sum = add(10, 20); // Call the function with two arguments
printf("Sum: %d\n", sum);
return 0;
}
Output:
Sum: 30
Explanation:
Also Read: Command Line Arguments in C Explained
Now that we've grasped return types and arguments, let's explore how to pass parameters to functions and make them even more flexible and dynamic!
The data provided when calling a function is referred to as the Actual Parameters. In the example below, 10 and 30 are the actual parameters.
Formal Parameters, on the other hand, are the variables and their data types specified in the function declaration. In this case, a and b are the formal parameters.
In C, functions can receive parameters in two ways: pass by value and pass by reference.
Let’s break them down and see how they work with examples.
When you pass a variable by value, a copy of the actual value is passed to the function. The function works with this copy, and any changes made inside the function do not affect the original variable.
Let’s look at an example:
#include <stdio.h>
void addTen(int num) {
num = num + 10; // This modifies the local copy of 'num'
printf("Inside function: %d\n", num); // Prints modified value
}
int main() {
int number = 5;
printf("Before function: %d\n", number); // Prints original value
addTen(number); // Pass by value, original 'number' remains unchanged
printf("After function: %d\n", number); // Prints original value again
return 0;
}
Output:
Before function: 5
Inside function: 15
After function: 5
Explanation:
In pass by reference, the memory address of the variable is passed to the function. Any changes made in the function will directly affect the original variable.
Let’s look at an example:
#include <stdio.h>
void addTen(int* num) {
*num = *num + 10; // Dereference the pointer and modify the actual value
printf("Inside function: %d\n", *num); // Prints modified value
}
int main() {
int number = 5;
printf("Before function: %d\n", number); // Prints original value
addTen(&number); // Pass by reference, changes the original 'number'
printf("After function: %d\n", number); // Prints modified value
return 0;
}
Output:
Before function: 5
Inside function: 15
After function: 15
Explanation:
Try both pass-by-value and pass-by-reference to get a better grasp on how C handles data. The more you explore, the clearer these concepts will become!
1. What is a function in C?
a) A global variable
b) A block of code executed once per program
c) A reusable block of code that performs a specific task
d) A pointer variable
2. Which keyword is used to declare a function returning no value?
a) return
b) void
c) null
d) none
3. What is the default return type of a function if not specified in C (older versions)?
a) int
b) void
c) float
d) char
4. What happens if a function is called before its declaration?
a) Error
b) Warning
c) Works normally
d) Depends on return type
5. Which of the following defines a function correctly?
a) function int sum(a, b) { return a + b; }
b) int sum(int a, int b) { return a + b; }
c) def sum(a, b): return a + b
d) function sum(a, b) { a + b; }
6. What is the difference between call by value and call by reference in C?
a) No difference
b) Call by reference copies values
c) Call by value passes address
d) Call by reference allows changes to actual arguments
7. Which of these passes actual values and does not affect the original data?
a) Call by value
b) Call by reference
c) Static function
d) Global function
8. How are arguments passed to functions in C by default?
a) By reference
b) By value
c) By pointer
d) Using arrays only
9. You write:
int update(int a) {
a += 10;
return a;
}
And call update(x) in main without assigning. What happens to x?
a) It gets updated
b) Compiler error
c) No change in x
d) x becomes 0
10. A function in C is not returning any value but used in an expression. What is the outcome?
a) Undefined behavior
b) Compiles normally
c) Returns NULL
d) Returns 0
11. You are asked to create a recursive function to calculate factorial. Which key element must be included?
a) A for loop
b) A base condition
c) A global variable
d) Pointer arithmetic
Now that you've tested your knowledge of functions in C programming, it's time to take your skills to the next level. Upgrading your knowledge is crucial, and upGrad offers in-depth courses designed to enhance your understanding of C programming.
Explore functions in C with examples and more advanced topics like recursion, passing parameters by reference, and memory management in C. upGrad provides a comprehensive learning experience, combining theoretical knowledge with hands-on practice.
Check out upGrad’s programs to advance your knowledge:
You can also get personalized career counseling with upGrad to guide your career path, or visit your nearest upGrad center and start hands-on training today!
Similar Reads:
Explore C Tutorials: From Beginner Concepts to Advanced Techniques
Array in C: Introduction, Declaration, Initialisation and More
Exploring Array of Pointers in C: A Beginner's Guide
What is C Function Call Stack: A Complete Tutorial
Binary Search in C
Constant Pointer in C: The Fundamentals and Best Practices
Find Out About Data Structures in C and How to Use Them?
In C, return types can be void (no return value), or any data type such as int, float, char, etc.
Yes, you can create many types of functions in C programming with no arguments and a return type, like a function that returns a constant or a calculated value.
Arguments allow functions in C programming to process different values, enabling code reuse and flexible functionality by passing different data.
Pass-by-value sends a copy of the variable's value, while pass-by-reference passes the actual memory address, allowing modification of the original variable.
Use void when a function doesn't need to return a value, indicating that the function performs a task without producing a result.
Yes, you can pass multiple parameters of different data types in C functions to process more complex data.
Use pass-by-reference for modifying data inside functions, as it allows the function to change the original value passed to it.
Recursion allows a function to call itself. It's useful for solving problems that can be broken down into smaller, similar problems.
No, since you can't return arrays in C, you can return a pointer to the array or use dynamic memory allocation.
Use return types when you need to send back a calculated result, and void when the function just performs an action without returning anything.
Yes, structures can be passed to functions either by value or by reference, enabling functions to manipulate or return complex data structures.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author|900 articles published
Previous
Next
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.