-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFUNCTIONS
More file actions
105 lines (78 loc) · 2.05 KB
/
FUNCTIONS
File metadata and controls
105 lines (78 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
1️⃣ Without Parameter & Without Return Value
👉 No input
👉 No output
#include <stdio.h>
void greet(void)
{
printf("Hello\n");
}
int main()
{
greet();
return 0;
}
✔ Used when function just performs an action (like LED ON).
2️⃣ With Parameter & Without Return Value
👉 Takes input
👉 Does not return anything
#include <stdio.h>
void printSquare(int num)
{
printf("Square = %d\n", num * num);
}
int main()
{
printSquare(5);
return 0;
}
✔ Used when function performs operation but no need to send result back.
3️⃣ Without Parameter & With Return Value
👉 No input
👉 Returns output
#include <stdio.h>
int getNumber(void)
{
return 10;
}
int main()
{
int value = getNumber();
printf("%d", value);
return 0;
}
✔ Used when function generates or reads a value (like ADC read).
🔹 4️⃣ With Parameter & With Return Value ⭐ (Most Important)
👉 Takes input
👉 Returns output
C
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int main()
{
int result = add(5, 3);
printf("Sum = %d", result);
return 0;
}
✔ Most commonly used in embedded systems.
🔥 Summary Table
Type Parameter Return Usage
1 ❌ ❌ Simple action
2 ✅ ❌ Action using input
3 ❌ ✅ Generate value
4 ✅ ✅ Process and return result
🎯 Embedded Example Mapping
Embedded Task Function Type
LED ON - No param, No return
Set PWM duty - With param, No return
Read ADC - No param, With return
Calculate CRC - With param, With return
🚀 Interview Style Answer
Functions in C are classified into four types based on the presence of parameters and return values: with/without parameters and with/without return value. The most commonly used type in embedded systems is with parameters and with return value.
🔹 “Return” and “Output” Are NOT the Same
There are two different things:
1️⃣ Returning a value → sends value back to the calling function
2️⃣ Printing output → displays something on the screen
These are completely different.