C Function Parameters

1. What are Function Parameters?

  • Function parameters are values you pass to a function to use inside it.

  • Parameters allow a function to work on different data each time it’s called.

Syntax:

return_type function_name(data_type param1, data_type param2, ...) {
// function body
}

2. Types of Function Parameters

A) Call by Value (Default in C)

  • A copy of the value is passed to the function.

  • Changes inside the function do not affect the original variable.

Example:

#include <stdio.h>

void addTen(int n) {
n = n + 10; // only changes copy
printf("Inside function: %d\n", n);
}

int main() {
int num = 5;
addTen(num);
printf("Outside function: %d\n", num); // original unchanged
return 0;
}

Output:

Inside function: 15
Outside function: 5

B) Call by Reference (Using Pointers)

  • Address of variable is passed to the function.

  • Changes inside the function affect the original variable.

Example:

#include <stdio.h>

void addTen(int *n) {
*n = *n + 10; // modify original variable
}

int main() {
int num = 5;
addTen(&num); // pass address
printf("num = %d\n", num); // original changed
return 0;
}

Output:

num = 15

Use * to access the value at the address (dereference).


3. Multiple Parameters

#include <stdio.h>

int sum(int a, int b, int c) {
return a + b + c;
}

int main() {
int result = sum(5, 10, 15);
printf("Sum = %d\n", result);
return 0;
}

Output:

Sum = 30
  • Functions can have any number of parameters.


4. Default Values

  • C does not support default parameter values (unlike C++).

  • You must pass all required arguments.


5. Key Points About Function Parameters

  1. Parameters allow functions to be flexible and reusable.

  2. Call by Value: passes a copy; original variable is unchanged.

  3. Call by Reference: passes the address; function can modify the original.

  4. Use pointers when you want a function to modify variables outside its scope.

  5. Functions can take multiple parameters of any type (int, float, char, arrays, pointers).

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *