Virtual assistance Chat Bot

C Programming Tutorial

Welcome to AIVista --India's tutorial pages on C Programming

C Programming

Function Parameters in C

Function parameters allow data to be passed into functions, enabling reusability and modular programming.

"Functions with parameters help make code reusable, efficient, and easier to manage."

What Are Function Parameters?

Function parameters are values that are passed into a function when it is called. These parameters allow functions to operate on different inputs.

Types of Function Parameters

Type Description Example
Pass by Value A copy of the actual parameter is passed to the function. void add(int a, int b)
Pass by Reference The address of the actual parameter is passed, allowing modification. void update(int *ptr)
Default Arguments Not supported in C (available in C++), but can be simulated. void show(int a = 10) // Not valid in C

Examples of Function Parameters

1. Passing Arguments by Value

Here, the values of variables are copied and changes inside the function do not affect the original variables.

#include <stdio.h>

void add(int a, int b) {
int sum = a + b;
printf("Sum: %d\n", sum);
}

int main() {
int x = 5, y = 10;
add(x, y);  // Passing values
return 0;
}

2. Passing Arguments by Reference (Using Pointers)

Here, the address of the variable is passed, allowing changes inside the function to reflect outside.

#include <stdio.h>

void square(int *num) {
*num = (*num) * (*num);
}

int main() {
int value = 4;
square(&value);  // Passing address
printf("Squared Value: %d\n", value);  // Value is changed
return 0;
}

3. Passing an Array as a Parameter

Arrays are always passed by reference in C.

#include <stdio.h>

void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
	printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int numbers[] = {1, 2, 3, 4, 5};
printArray(numbers, 5);
return 0;
}

4. Returning Values from a Function

A function can return values to the caller using the return statement.

#include <stdio.h>

int multiply(int a, int b) {
return a * b;
}

int main() {
int result = multiply(5, 6);
printf("Multiplication: %d\n", result);
return 0;
}

Important Notes on Function Parameters

  • Pass by Value: The function works with a copy of the variable, so the original value remains unchanged.
  • Pass by Reference: The function works with the original variable via its memory address, allowing modifications.
  • Arrays: Always passed by reference (passing a pointer to the first element).
  • Pointers: Essential for modifying values inside a function.

Best Practices for Using Function Parameters

  • Use pass by value for small variables like integers, floats, and characters.
  • Use pass by reference when modifying the original value inside the function.
  • Pass **arrays** efficiently by using pointers to avoid copying large amounts of data.

Example: Swapping Two Numbers Using Call by Reference

#include <stdio.h>

void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}

int main() {
int a = 10, b = 20;
printf("Before swap: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}

Conclusion

Understanding function parameters in C allows you to write modular, efficient, and reusable code. Using **pass by value** and **pass by reference** appropriately is key to writing optimized functions! 🚀