In the previous post, we explored why
functions exist — how they make our code modular, readable, and reusable.
Now, it’s time to peek under the hood and see how data actually travels in
and out of a function.
🧩 1. The
Function Flow — From Call to Return
When you call a function in C, three things happen:
- Control jumps from the caller to the function definition.
- Values are sent as arguments (inputs).
- A result may or may not come back.
Think of it like ordering a pizza:
- You send parameters (toppings, size, crust type).
- The pizza shop (the function) prepares it.
- You get a return value (your pizza 🍕).
🧠 2.
Parameters and Arguments — What’s the Difference?
- Parameters are placeholders in the function definition.
- Arguments are the actual values you pass when calling the function.
Example:
#include <stdio.h>
void greetUser(char name[]) { // 'name' is a parameterprintf("Hello, %s! Welcome to ProgVeda!\n", name);}
int main() {greetUser("Sanchayeta"); // "Sanchayeta" is an argument return 0;}
Output:
Hello, Sanchayeta! Welcome to ProgVeda!
So, parameters are like variable names in the function — they receive values sent from the caller.
🔁 3.
Return Type — How Functions Send Data Back
Some functions only do things (like printing), while others calculate and return something.
Example:
#include <stdio.h>
int add(int a, int b) { return a + b;}
int main() { int result = add(5, 7);printf("Sum = %d\n", result); return 0;}
Output:
Sum = 12
Here,
- int add(int a, int b) means the function returns an int.
- The return statement sends the result back.
⚙️ 4. Function
Categories Based on Parameters & Return Types
C functions fall into four classic types:
|
Function Type |
Parameters |
Return Value |
Example |
|
Type 1 |
No |
No |
void greet(); |
|
Type 2 |
Yes |
No |
void greetUser(char name[]); |
|
Type 3 |
No |
Yes |
int getLuckyNumber(); |
|
Type 4 |
Yes |
Yes |
int add(int a, int b); |
Example for each:
#include <stdio.h>
// Type 1: No parameter, no returnvoid greet() {printf("Hello, C Learner!\n");}
// Type 2: With parameter, no returnvoid greetUser(char name[]) {printf("Hi %s, keep learning C!\n", name);}
// Type 3: No parameter, with returnint getLuckyNumber() { return 7;}
// Type 4: With parameter, with returnint add(int a, int b) { return a + b;}
int main() {greet();greetUser("Ananya");printf("Lucky Number = %d\n", getLuckyNumber());printf("Sum = %d\n", add(3, 4)); return 0;}
🧪 5.
Memory Game — Call by Value in C
When you pass variables to a function in C,
only copies are sent.
The original values remain unchanged. This is called Call by Value.
Example:
#include <stdio.h>
void modify(int x) { x = 100;}
int main() { int num = 5; modify(num);printf("Value of num = %d\n", num); return 0;}
Output:
Value of num = 5
Even though modify() changed x, it didn’t affect num — because it was just a copy.
💡 Key
Takeaways
- Parameters act as placeholders for input data.
- Return values send results back to the caller.
- Every function in C has a return type — void means “no return”.
- Call by Value ensures your original data stays safe.
🧭
Assignment / Lab Challenges
Challenge 1:
Write a function square(int n) that returns the square of a number.
Input: 5 → Output: 25
Challenge 2:
Create a function convertToFahrenheit(float celsius) that returns the
equivalent temperature in Fahrenheit.
Formula: F = (C * 9/5) + 32
Challenge 3:
Design a function sumOfDigits(int n) that returns the sum of digits of a
number.
Example: Input 123 → Output 6
Challenge 4 (Thinking Corner 🧠):
Why is “Call by Reference” not the default in C?
Try predicting what would happen if it were!
🌱 Coming
Up Next (Post 3 Preview)
“Functions Revisited — Local vs Global Variables,
Scope, and Storage Classes”
We’ll uncover how C remembers variables inside and outside functions (and how
it forgets them too).
Post a Comment