Definition
A function in C is a block of code that performs a specific task. Instead of writing the same code many times, we can write it once in a function and use it whenever needed.
Example: A function add()
can be used to add any two numbers in different parts of a program without rewriting the logic again.
Why are functions important?
Extra[You can skip this during writing on notebooks. It is just an extra knowledge.]- They bring structure and modularity to programs.
- They allow collaborative development (different programmers can work on different functions).
- They make debugging easier since issues can be isolated to specific functions.
Types of Functions
1. Library Functions
These are predefined functions provided by the C standard library. They are ready-to-use and save development time.
Examples: printf()
, scanf()
, sqrt()
, strlen()
, strcmp()
.
2. User-Defined Functions
These are functions created by programmers to perform specific tasks as required by the program.
Examples:
void greet();
void display();
int add(int num1, int num2);
Advantages of Functions
1. Reusability
Code written once can be reused multiple times, saving time and effort.
Example: Example: A function add() can be used to add any two numbers in different parts of the program.
2. Separation of Task
Each function performs a separate task, making programs modular and organized.
Example: Example: One function handles input, another handles calculation, and another displays output.
3. Readability
Programs become easier to read, understand, and maintain. Functions with meaningful names explain themselves.
Example: Example: A function named calculateSalary() makes the purpose clear.
4. Handling Complexity
Large problems are divided into smaller, manageable parts. Each function can be designed, tested, and debugged independently.
5. Collaboration
Extra[You can skip this during writing on notebooks. It is just an extra knowledge.]In large projects, different team members can work on different functions simultaneously.
6. Reduced Redundancy
Extra[You can skip this during writing on notebooks. It is just an extra knowledge.]Repeating code multiple times increases errors and maintenance issues. Functions reduce duplication.
Parts of a Function in C
1. Function Signature
Declares the return type, function name, and parameters.
Example: int add(int a, int b);
2. Parameters
Variables that receive values from the calling function.
Example: int add(int a, int b) → here a and b are parameters.
3. Return Value
The result produced by a function and sent back to the caller.
Example: Example: return sum;
4. Return Keyword
Used to send back a value or exit from a function.
Example: Example: return a + b;
5. Function Definition
The full implementation of the function.
Example:
int add(int x, int y) {
return x + y;
}
6. Function Description
A short explanation of what the function does.
Example: "add() takes two numbers and returns their sum."
7. Function Body
The actual set of instructions inside { }.
void greet() {
printf("Hello, Welcome to C Functions!\\n");
}
8. Function Header
The first line of the function which includes return type, function name, and parameters.
Example: int add(int a, int b)
Calling a Function
Calling a function means using the function in the program where it is needed. A function is called by its name.
void greet() {
printf("Hello, Welcome to C Functions!\n");
}
int main() {
greet(); // function call
greet(); // called again (reusability)
return 0;
}
Arguments
Actual values passed to a function when it is called.
Example: add(5, 10);
→ here 5 and 10 are arguments.
Parts of Function Definition (Enlist)
- Return Type
- Function Name
- Parameters (if any)
- Function Body
Examples of Functions in C
Extra[You can skip this during writing on notebooks. It is just an extra knowledge.]Example 1: Addition Function
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 7);
printf("Sum = %d", result);
return 0;
}
Example 2: Greeting Function
#include <stdio.h>
void greet() {
printf("Hello, Welcome to Functions in C!\\n");
}
int main() {
greet();
return 0;
}
Interview Questions on Functions in C
- What is the difference between call by value and call by reference?
- Can a C function return multiple values?
- What is recursion? Give an example of a recursive function.
- Explain the difference between function declaration and function definition.
- What are inline functions? Are they supported in C?
- Why do we use header files in relation to functions?
Real-Life Applications of Functions
- Banking Systems: A function to calculate interest, another to handle transactions.
- Games: Functions to control movement, scoring, and rendering.
- Healthcare: Functions to process patient data, calculate BMI, and generate reports.
- Operating Systems: Functions manage memory allocation, file handling, and device drivers.
Best Practices for Writing Functions
- Use meaningful names (e.g.,
calculateSalary()
instead ofcs()
). - Keep functions short and focused on one task.
- Document each function with comments.
- Avoid global variables; prefer parameters and return values.
- Test functions individually before integrating them.
MCQs – Functions in C
- What is a function in C?
a) A storage unit
b) A block of code that performs a specific task ✅
c) A variable type
d) None of the above - Which of the following is a library function?
a) add()
b) greet()
c) printf() ✅
d) display() - User-defined functions are:
a) Predefined in C
b) Created by programmers ✅
c) Built-in to the compiler
d) None of the above - Which part of a function contains actual instructions?
a) Function Header
b) Function Body ✅
c) Function Signature
d) Return Type - What is the keyword used to return a value from a function?
a) send
b) give
c) return ✅
d) exit - Which of the following is true about recursion?
a) A function calling itself ✅
b) A function that never returns
c) A loop inside a function
d) None of the above - What is the default return type of a function in C (if not specified)?
a) void
b) int ✅
c) float
d) char - Why are header files important for functions?
a) They provide function prototypes ✅
b) They store variables
c) They make functions run faster
d) None of the above - Which function is always present in every C program?
a) printf()
b) scanf()
c) main() ✅
d) exit() - What is call by reference?
a) Passing values
b) Passing memory addresses ✅
c) Copying arguments
d) Ignoring arguments
Related Posts
Frequently Asked Questions (FAQs)
Functions in C are reusable blocks of code that perform specific tasks, making programs modular and efficient.
C has two main types of functions: Library Functions (predefined in the standard library) and User-Defined Functions (created by programmers).
Recursion is when a function calls itself directly or indirectly. It is useful for problems like factorial, Fibonacci, and tree traversal.
No, a function in C returns only one value directly. However, multiple values can be returned using pointers, arrays, or structures.
Header files contain function prototypes that allow the compiler to understand how to call a function before it is defined.