Course: CSE 1209 - Computer Fundamentals and Programming tags: cse-1209 c fundumental
Source: Chapter 9: Functions (up to 9.7 Recursion)
Comprehensive Note: Functions in C (Chapter 9)
9.1 & 9.2 Introduction and Need for Functions
A function is a group of statements enclosed within a block that performs a specific task and has a valid identifier (name).
Analogy: Think of a function like a Vending Machine (a black box). It takes some input (money, choice of item), processes it inside without you needing to know its internal mechanics, and provides an output (candy, cupcake, etc.).
Why do we need functions?
- Modular Programming: Dividing a large problem into smaller, manageable subproblems (e.g., separate functions for addition, subtraction, etc.).
- Code Reusability: Write the code once and use (call) it multiple times without rewriting.
- Easy Debugging: Finding and fixing errors is easier in small isolated blocks.
- Reduction in Program Size: Eliminates repetitive code.
9.3 Types of Functions
The C programming language supports two primary types of functions:
- Library (Predefined) Functions: These are built-in functions whose definitions are already known to the compiler (e.g.,
printf(),scanf()). You only use them; you do not write their logic. - User-defined Functions: Functions created by the programmer to solve specific problems (e.g., writing a custom
calculate_area()function).
9.4 & 9.5 User-Defined Functions: Syntax and Components
A user-defined function remains dormant and only executes when it is explicitly “called”.
General Syntax
ReturnType FunctionName (Parameter List)
{
// Body of the Function (Statements)
}
Components of a Function
- Calling Function: The function that calls another function (usually
main()). - Called Function: The function that is being requested to execute.
- Function Prototype (Declaration): Informs the compiler about the function’s name, return type, and arguments before it is used. It is written before
main()and ends with a semicolon;. - Function Definition: The actual body/code of the function containing the logic.
- Function Call: The statement that invokes the function (e.g.,
c = max(a, b);). - Return Type: Specifies the type of data the function sends back (e.g.,
int,float). If it returns nothing, the keywordvoidis used. - Actual Arguments: The real values or variables passed in the function call (e.g.,
aandbinmax(a, b)). - Formal Arguments: The variables declared in the function definition’s header that receive the values passed from the calling function.
9.6 Categories of a Function
Depending on whether arguments are passed and whether a value is returned, user-defined functions fall into four categories:
- Without arguments and Without return type:
- No input passed, no output returned.
- Syntax:
void functionName(void)
- Without arguments and With return type:
- No input passed, but returns a value.
- Syntax:
int functionName(void)
- With arguments and Without return type:
- Input passed, but no output returned.
- Syntax:
void functionName(int x, int y)
- With arguments and With return type: (Most commonly used)
- Input passed, output returned.
- Syntax:
int functionName(int x, int y)
9.7 Recursion
Recursion is a repetitive process where a function calls itself to solve a smaller instance of the same problem.
How it Works (Decomposition & Reassembly)
Every recursive function requires two main elements to prevent infinite loops:
- Base Case (Stopping Condition): The condition under which the function stops calling itself and returns a specific, solved value.
- General Case (Recursive Step): The logic that reduces the size of the problem and makes the recursive call.
Execution Process: Recursion involves a two-way journey:
- Decomposition (Top to Bottom): The function breaks the problem down into smaller general cases until it hits the base case. (e.g.,
Fact(3) = 3 * Fact(2)2 * Fact(1)1 * Fact(0)). - Reassembly (Bottom to Top): Once the base case is hit (e.g.,
Fact(0) = 1), the program returns this value backwards through the chain to compute the final answer.
Code Example: Factorial using Recursion
#include<stdio.h>
int factorial(int n) {
if (n == 0) // Base Case
return 1;
else
return n * factorial(n - 1); // General Case
}
void main() {
int n = 5, fact;
fact = factorial(n);
printf("Factorial = %d", fact);
}