Function Parameters
Passing data to functions in C
📥 What are Function Parameters?
Parameters allow functions to receive input values. They make functions flexible and reusable by letting you pass different data each time you call the function.
// Function with parameters
#include <stdio.h>
void greetUser(char name[]) {
printf("Hello, %s!\n", name);
}
int main() {
greetUser("Alice");
greetUser("Bob");
return 0;
}
Types of Parameters
Pass by Value
Copy of value is passed
void func(int x) {
x = 10; // Original unchanged
}
Pass by Reference
Address of variable is passed
void func(int *x) {
*x = 10; // Original changed
}
Array Parameters
Passing arrays to functions
void func(int arr[], int size) {
// Process array
}
Multiple Parameters
Functions with several inputs
int add(int a, int b, int c) {
return a + b + c;
}
🔹 Basic Parameter Example
Simple functions with single parameters demonstrate how data passes into functions and calculations occur. Parameters receive values from function calls, enabling functions to operate on different data each time. The function processes the parameter value, performs calculations, and returns results. Understanding parameter passing is fundamental to functional programming, enabling code reuse and modularity. Practice writing functions with various parameter types to master data passing mechanics.
#include <stdio.h>
void printSquare(int number) {
int result = number * number;
printf("Square of %d is %d\n", number, result);
}
int main() {
printSquare(5);
printSquare(8);
printSquare(12);
return 0;
}
Output:
Square of 5 is 25
Square of 8 is 64
Square of 12 is 144
🔹 Multiple Parameters
Functions can accept multiple parameters enabling complex operations on different data values simultaneously. Each parameter has its own data type and name, allowing functions to work with various inputs in coordinated ways. Parameters pass from left to right in function calls, matching the function definition order. This flexibility enables designing versatile functions handling different combinations of inputs and producing diverse outputs based on multiple inputs simultaneously.
#include <stdio.h>
float calculateArea(float length, float width) {
return length * width;
}
void displayInfo(char name[], int age, float height) {
printf("Name: %s\n", name);
printf("Age: %d years\n", age);
printf("Height: %.1f cm\n", height);
}
int main() {
float area = calculateArea(10.5, 8.2);
printf("Area: %.2f\n\n", area);
displayInfo("John", 25, 175.5);
return 0;
}
Output:
Area: 86.10
Name: John
Age: 25 years
Height: 175.5 cm
🔹 Pass by Value vs Pass by Reference
Understanding pass-by-value and pass-by-reference is critical for controlling how function parameters behave and affect original variables. Pass-by-value creates copies of arguments, preventing function modifications from affecting original variables, ensuring safety and predictability. Pass-by-reference using pointers allows functions to modify original variables, enabling functions to produce multiple outputs or modify caller data directly. Choose the appropriate passing mechanism based on whether functions should modify original data.
🔸 Pass by Value (Copy)
#include <stdio.h>
void changeValue(int x) {
x = 100; // Only changes the copy
printf("Inside function: %d\n", x);
}
int main() {
int num = 50;
printf("Before function: %d\n", num);
changeValue(num);
printf("After function: %d\n", num); // Original unchanged
return 0;
}
Output:
Before function: 50
Inside function: 100
After function: 50
🔸 Pass by Reference (Pointer)
#include <stdio.h>
void changeValue(int *x) {
*x = 100; // Changes the original
printf("Inside function: %d\n", *x);
}
int main() {
int num = 50;
printf("Before function: %d\n", num);
changeValue(#); // Pass address
printf("After function: %d\n", num); // Original changed
return 0;
}
Output:
Before function: 50
Inside function: 100
After function: 100
🔹 Array as Parameter
Passing arrays to functions enables processing collections of data within functions, making code modular and reusable. Array parameters pass by reference by default, allowing functions to modify array contents. Include array size parameters to enable functions to process entire arrays safely. Use this pattern for functions performing calculations on collections, filtering data, or transforming arrays while maintaining array contents outside the function.
#include <stdio.h>
void printArray(int arr[], int size) {
printf("Array elements: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int findSum(int numbers[], int count) {
int sum = 0;
for (int i = 0; i < count; i++) {
sum += numbers[i];
}
return sum;
}
int main() {
int myArray[] = {10, 20, 30, 40, 50};
int size = 5;
printArray(myArray, size);
int total = findSum(myArray, size);
printf("Sum: %d\n", total);
return 0;
}
Output:
Array elements: 10 20 30 40 50
Sum: 150