C Programming
About Lesson
1: Passing Parameters to Functions

In C, you can pass parameters (data) to functions to perform operations on that data. Parameters can be passed by value or by reference.

  • Passing Parameters by Value:

    • Copies the value of the actual argument into the formal parameter of the function.
     
    void display(int num) {
    printf("Number: %dn", num);
    }
  • Passing Parameters by Reference (Address):

    • Passes the address of the actual argument into the formal parameter. Changes made to the formal parameter will affect the actual argument.
     
    void increment(int *ptr) {
    (*ptr)++; // Increment the value at the address pointed by ptr
    }
 2: Call by Value vs. Call by Reference
  • Call by Value:

    • Changes made to the formal parameters do not affect the actual arguments.
     
    void swap(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
    }
  • Call by Reference:

    • Changes made to the formal parameters affect the actual arguments.
     
    void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
    }
3: Default Arguments (Not Supported in Standard C)

C language does not directly support default arguments like some other languages (e.g., C++, Python). However, you can achieve similar behavior using function overloading or conditional checks within the function.

  • Example (Simulating Default Arguments):
     
    void printMessage(char *msg, int num) {
    printf("%s: %dn", msg, num);
    }

    void printMessageWithDefault(char *msg) {
    printMessage(msg, 0); // Call printMessage with default value for num
    }

4: Practice and Examples
  • Practice Exercise:
    • Write a program that swaps two integers using both call by value and call by reference methods.
    • Implement a function that calculates the sum of an array of integers and returns the result using call by reference to update the result.
 
#include <stdio.h>

// Function prototype for swapping using call by value
void swapByValue(int a, int b);

// Function prototype for swapping using call by reference
void swapByReference(int *a, int *b);

// Function prototype for calculating sum of an array
void calculateSum(int arr[], int size, int *result);

int main() {
int num1 = 10, num2 = 20;
printf("Before swapping (call by value): num1 = %d, num2 = %dn", num1, num2);
swapByValue(num1, num2);
printf("After swapping (call by value): num1 = %d, num2 = %dn", num1, num2);

printf("nBefore swapping (call by reference): num1 = %d, num2 = %dn", num1, num2);
swapByReference(&num1, &num2);
printf("After swapping (call by reference): num1 = %d, num2 = %dn", num1, num2);

int array[] = {1, 2, 3, 4, 5};
int size = sizeof(array) / sizeof(array[0]);
int sum = 0;
calculateSum(array, size, &sum);
printf("nSum of array elements: %dn", sum);

return 0;
}

// Function definition for swapping using call by value
void swapByValue(int a, int b) {
int temp = a;
a = b;
b = temp;
}

// Function definition for swapping using call by reference
void swapByReference(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

// Function definition for calculating sum of an array using call by reference
void calculateSum(int arr[], int size, int *result) {
*result = 0;
for (int i = 0; i < size; i++) {
*result += arr[i];
}
}

Understanding how to pass parameters to functions and the differences between call by value and call by reference is essential for writing efficient and reusable code in C. Practice using these concepts to manipulate data within functions and perform operations on variables effectively.