Functions
functions
🔹 What is a Function in C?
A function is a block of code that performs a specific task. It improves modularity, avoids repetition, and enhances code readability.
🔹 Types of Functions in C
Functions are categorized based on input parameters and return values:
1. No Arguments, No Return Value
#include <stdio.h>
void greet() {
printf("Hello, welcome to C programming!\\n");
}
int main() {
greet();
return 0;
}
2. No Arguments, Returns a Value
#include <stdio.h>
int getNumber() {
return 42;
}
int main() {
int num = getNumber();
printf("The number is: %d\\n", num);
return 0;
}
#include <stdio.h>
void printSquare(int n) {
printf("Square: %d\\n", n * n);
}
int main() {
printSquare(5);
return 0;
}
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(4, 6);
printf("Sum: %d\\n", result);
return 0;
}
🔹 Summary Table
Function Type | Takes Arguments | Returns Value | Use Case |
---|---|---|---|
No Argument, No Return | No | No | Simple output |
No Argument, Return | No | Yes | Fetch constant data |
Argument, No Return | Yes | No | Display or act on inputs |
Argument, Return | Yes | Yes | Mathematical operations |
🔹 Function Declaration
Function declaration tells the compiler about the function’s return type,
name, and parameters before its definition.
int add(int, int);
It usually appears above main()
or in a header file.
🔹 Function Definition
Function definition provides the actual code that will execute when the
function is called.
int add(int x, int y) {
return x + y;
}
🔹 Function Call
A function call executes the function and may return a result.
int result = add(5, 3);
🔹 Full Example
#include <stdio.h>
// Function declaration
int add(int, int);
int main() {
int a = 10, b = 20;
int sum = add(a, b); // Function call
printf("Sum: %d", sum);
return 0;
}
// Function definition
int add(int x, int y) {
return x + y;
}
🔹 Parameter Passing in C
In C, arguments can be passed to functions using two techniques:
1. Call by Value – A copy of the value is sent to the function.
Changes do not affect the original variable.
void modify(int x) {
x = x + 10;
}
2. Call by Reference – The address of the value is sent using a pointer.
Changes affect the original variable.
void modify(int *x) {
*x = *x + 10;
}
🔹 Comparison Table
Method | What is Passed | Original Value Modified? | Uses Pointer? |
---|---|---|---|
Call by Value | Copy of variable | No | No |
Call by Reference | Address of variable | Yes | Yes |
🔹 What is the Scope of a Variable?
The scope of a variable defines where it is accessible in the program.
🔹 1. Local Scope
Declared inside a function or block. It exists only within that function.
void test() {
int x = 10; // Local variable
printf("%d", x);
}
🔹 2. Global Scope
Declared outside of all functions. Can be used by any function in the file.
int x = 10; // Global variable
void display() {
printf("%d", x);
}
🔹 3. Block Scope
Declared within a specific block and accessible only inside it.
int main() {
{
int y = 20;
printf("%d", y);
}
// y is not accessible here
}
🔹 Summary Table
Scope Type | Declared In | Accessible From | Lifetime |
---|---|---|---|
Local | Inside function | That function/block | During function call |
Global | Outside functions | All functions | Entire program |
Block | Inside { } | That block only | During block execution |
🔹 What are Storage Classes in C?
Storage classes define the scope, lifetime, and memory location of variables in C programming.
🔹 1. auto
Default for local variables.
void test() {
auto int x = 10;
printf("%d", x);
}
🔹 2. register
Stored in CPU register (if available).
void speed() {
register int counter = 0;
}
🔹 3. static
Retains value between function calls.
void counter() {
static int count = 0;
count++;
printf("%d ", count);
}
🔹 4. extern
Refers to a global variable declared elsewhere.
extern int total;
void show() {
printf("%d", total);
}
🔹 Summary Table
Storage Class | Scope | Lifetime | Default Value | Feature |
---|---|---|---|---|
auto | Local | Block level | Garbage | Default for local variables |
register | Local | Block level | Garbage | Stored in CPU register |
static | Local | Entire program | 0 | Retains value between calls |
extern | Global | Entire program | Depends | Uses global variable from elsewhere |
🔹 What is Recursion?
Recursion is a process in which a function calls itself to solve a smaller instance of the same problem.
🔹 Key Components
Base Case: Condition when the recursion should stop.
Recursive Case: The function calls itself.
🔹 Syntax
returnType functionName(parameters) {
if (base_condition)
return result;
else
return functionName(smaller_problem);
}
🔹 Example: Factorial
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
🔹 Advantages
✅ Simple and clean code for problems like trees, factorials, and Fibonacci.
✅ Helps with divide-and-conquer strategies.
🔹 Disadvantages
❌ Can cause stack overflow on large inputs.
❌ Uses more memory than iterative methods.
❌ Slower due to repeated function calls.
Comments
Post a Comment
write your complements and complaints :)