Function
Function: A function in C is a block of code that
performs a specific task and can be used many times in a program. It
helps to divide a big program into small manageable parts.
Subroutine: A subroutine is also a block of code
that performs a specific task, but it does not return any value. In C, a
subroutine is written using a void function.
Uses of
Function and Subroutine
|
Purpose |
Explanation |
|
Code
Reusability |
A function
can be written once and used many times. |
|
Modularity |
Divides a
large program into small sections (modules). |
|
Easy
Debugging |
Errors can
be easily found in small functions. |
|
Improved
Readability |
Makes
program easier to read and understand. |
|
Teamwork |
Different
functions can be made by different programmers. |
Drawbacks of Functions and Subroutines
|
Drawback |
Explanation |
|
Extra
Memory Use |
Function
calls require memory for parameters and return values. |
|
Execution
Overhead |
Jumping in
and out of functions can make the program slightly slower. |
|
Complexity |
Too many
small functions can make code hard to track. |
Declaration and Initialization
(a) Function Declaration
Also called Function
Prototype — it tells the compiler about the function name, return type, and
parameters.
Syntax:
return_type function_name(parameter_list);
📘 Example:
int add(int, int);
void greet();
Function Definition
Defines what
the function will do.
Syntax:
return_type function_name(parameter_list) {
//
statements
}
Example:
int add(int a, int b) {
return a +
b;
}
Function Call
Used to
execute a function.
📘 Syntax:
function_name(arguments);
Example:
sum = add(5, 10);
Types of Functions and Subroutines
1. Library Function
Definition:
Library functions are predefined functions that are already written and
stored in C header files (like stdio.h, math.h, string.h, etc.). You can use them directly
in your program without writing their code.
Examples:
printf(); //
for output
scanf(); //
for input
sqrt(); //
for square root
strlen(); //
for length of a string
Note:
To use a library function, you must include the proper header file at
the beginning of your program.
Example:
#include <math.h>
2. User-Defined
Function
Definition:
A user-defined function is a function created by the programmer to
perform a specific task. It helps to make the program modular, reusable, and
easy to understand.
Example:
int add(int a, int b) {
return a +
b;
}
Note:
User-defined functions are written by the user using the syntax:
return_type function_name(parameter_list) {
//
statements
}
Types of Functions in C (Based on Arguments and Return Value)
1. No Arguments and No Return Value
Definition:
This type of function does not take any
input (arguments) and does not
return any value.
It simply performs a task like displaying a message.
Example:
#include <stdio.h> void greet() { printf("Hello Students!\n");} int main() { greet(); // function call return 0;}
Use:
When you only want to perform an action, not calculations.
2. Arguments but No Return Value
Definition:
This type of function takes input values
(arguments) but does not return
any result to the calling function.
Example:
#include <stdio.h> void displaySum(int a, int b) { printf("Sum = %d\n", a + b);} int main() { displaySum(10, 20); // function call with arguments return 0;}
Use:
When you need to process or display results directly inside the function.
3. No Arguments but Returns a Value
Definition:
This function does not take any input,
but it returns a value to the
main program.
Example:
#include <stdio.h> int getNumber() { return 100;} int main() { int num = getNumber(); // function call printf("Number = %d", num); return 0;}
Use:
When the function itself generates or calculates a value internally.
4. With Arguments and Return Value
Definition:
This type of function takes input values
(arguments) and also returns a
result to the calling function.
It is the most commonly used
type of function.
Example:
#include <stdio.h> int add(int a, int b) { return a + b;} int main() { int sum = add(10, 20); printf("Sum = %d", sum); return 0;}
Use:
When input is needed and output is expected.
Recursion in Function
Recursion is a process in which a function calls itself directly or indirectly to solve a
problem.
In simple words,
A recursive function is a function that calls itself.
Syntax of Recursive Function:
return_type function_name(parameters) { if (condition) return some_value; // Base case else return function_name(modified_parameter); // Recursive call}
Important Terms:
1.
Recursive Call:
When a function calls itself, it is called a recursive call.
2.
Base Case
(Termination Condition):
A condition that stops the function from calling itself again — to prevent infinite recursion.
Example 1: Factorial Using Recursion
Program:
#include <stdio.h> int factorial(int n) { if (n == 0 || n == 1) return 1; // Base case else return n * factorial(n - 1); // Recursive call} int main() { int num = 5; printf("Factorial of %d = %d", num, factorial(num)); return 0;}
Output:
Factorial of 5 = 120
Advantages and Disadvantages of Recursion
Advantages of Recursion
|
No. |
Advantage |
Explanation |
|
1. |
Simplifies Complex Problems |
Problems like factorial, Fibonacci
series, and tree traversal are easier to solve using recursion. |
|
2. |
Reduces Code Length |
Recursive programs are shorter and easier
to write compared to long iterative loops. |
|
3. |
Better Logical Clarity |
The logic of repetitive or mathematical
problems becomes easier to understand. |
|
4. |
Useful for Repetitive Problems |
Recursion automatically handles
repetition through self-calls instead of using loops. |
🔹 Disadvantages
of Recursion
|
No. |
Disadvantage |
Explanation |
|
1. |
High Memory Usage |
Each recursive call uses extra stack
memory, which can lead to memory overflow. |
|
2. |
Slower Execution |
Calling a function repeatedly takes more
time than using loops. |
|
3. |
Difficult to Debug |
Tracing errors in recursive calls can be
complex. |
|
4. |
Risk of Infinite Recursion |
If the base condition is missing or
wrong, the program never stops and crashes. |
No comments:
Post a Comment