🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to C Notes
Topic #62

C Function Parameters


Parameters and Arguments

Information can be passed to functions as a parameter. Parameters act as variables inside the function.

Parameters are specified after the function name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma:

Syntax

returnType functionName(parameter1, parameter2, parameter3) {

// code to be executed
}

In the example below, the function takes a string of characters with name as parameter. When the function is called, we pass along a name, which is used inside the function to print "Hello" and the name of each person:

Example

void myFunction(char name[]) {
  printf("Hello %s\n", name);
}

int main() {

myFunction("Liam");
  myFunction("Jenny");

myFunction("Anja");
  return 0;
}

// Hello Liam
// Hello Jenny
// Hello Anja

Note: When a parameter is passed to the function, it is called an argument. So, from the example above: name is a parameter, while Liam, Jenny and Anja are arguments.


Multiple Parameters

Inside the function, you can add as many parameters as you want:

Example

void myFunction(char name[], int age) {
  printf("Hello %s.
You are %d years old.\n", name, age);
}

int main() {

myFunction("Liam", 3);
  myFunction("Jenny", 14);

myFunction("Anja", 30);
  return 0;
}

// Hello Liam.
You are 3 years old.
//
Hello Jenny. You are 14 years old.
// Hello Anja. You are 30 years old.

If we consider the "Calculate the Sum of Numbers" example from the previous page, we can make a more sustainable program by using function parameters:

Example

void calculateSum(int x, int y) {
  int sum = x + y;

printf("The sum of %d + %d is: %d\n", x, y, sum);
}

int main() {

calculateSum(5, 3);
  calculateSum(8, 2);
  calculateSum(15,
15);
  return 0;
}

Note: Notes on Parameters Note that when you are working with multiple parameters, the function call must have the same number of arguments as there are parameters, and the arguments must be passed in the same order.


Pass Arrays as Function Parameters

You can also pass arrays to a function:

Example

void myFunction(int myNumbers[5]) {
  for (int i = 0; i < 5; i++) {

printf("%d\n", myNumbers[i]);
  }
}

int main() {
  int
myNumbers[5] = {10, 20, 30, 40, 50};
  myFunction(myNumbers);

return 0;
}

Note: Example Explained The function (myFunction) takes an array as its parameter (int myNumbers[5]), and loops through the array elements with the for loop. When the function is called inside main(), we pass along the myNumbers array, which outputs the array elements. Note that when you call the function, you only need to use the name of the array when passing it as an argument myFunction(myNumbers). However, the full declaration of the array is needed in the function parameter (int myNumbers[5]).


Return Values

The void keyword, used in the previous examples, indicates that the function should not return a value. If you want the function to return a value, you can use a data type (such as int or float, etc.) instead of void, and use the return keyword inside the function:

Example

int myFunction(int x) {
  return 5 + x;
}

int main() {

printf("Result is: %d", myFunction(3));
  return 0;
}

// Outputs
8 (5 + 3)

This example returns the sum of a function with two parameters:

Example

int myFunction(int x, int y) {
  return x + y;
}

int main()
{
  printf("Result is: %d", myFunction(5, 3));
  return 0;
}

// Outputs 8 (5 + 3)

You can also store the result in a variable:

Example

int myFunction(int x, int y) {
  return x + y;
}

int main()
{
  int result = myFunction(5, 3);
  printf("Result is =
%d", result);
  return 0;
}
// Outputs 8 (5 + 3)

If we consider the "Calculate the Sum of Numbers" example one more time, we can use return instead and store the results in different variables. This will make the program even more flexible and easier to control:

Example

int calculateSum(int x, int y) {
  return x + y;
}

int main() {

int result1 = calculateSum(5, 3);
  int result2 = calculateSum(8, 2);

int result3 = calculateSum(15, 15);

  printf("Result1 is:
%d\n", result1);
  printf("Result2 is: %d\n", result2);

printf("Result3 is: %d\n", result3);

  return 0;
}

Tip: If you have many "result variables", it is better to store the results in an array:

Example

int calculateSum(int x, int y) {
  return x + y;
}

int main() {

  // Create an array
  int resultArr[6];

  //
Call the function with different arguments and store the results in
the array
  resultArr[0] = calculateSum(5, 3);
  resultArr[1]
= calculateSum(8, 2);
  resultArr[2] = calculateSum(15, 15);

resultArr[3] = calculateSum(9, 1);
  resultArr[4] = calculateSum(7,
7);
  resultArr[5] = calculateSum(1, 1);

  for (int i = 0;
i < 6; i++) {
    printf("Result%d is = %d\n", i + 1,
resultArr[i]);
  }

  return 0;
}

Real-Life Example

To demonstrate a practical example of using functions, let's create a program that converts a value from fahrenheit to celsius:

Example

// Function to convert Fahrenheit to Celsius
float toCelsius(float
fahrenheit) {
  return (5.0 / 9.0) * (fahrenheit - 32.0);
}

int main() {
  // Set a fahrenheit value
  float f_value =
98.8;

  // Call the function with the fahrenheit value

float result = toCelsius(f_value);

  // Print the fahrenheit value

printf("Fahrenheit: %.2f\n", f_value);

  // Print the result

printf("Convert Fahrenheit to Celsius: %.2f\n", result);

  return
0;
}

Want to go beyond the notes?

Join CodingNow 2.0's C course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

C Function Parameters – FAQs

Quick answers about learning C Function Parameters in C.

This free note from CodingNow 2.0 explains C Function Parameters in C — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every C topic on CodingNow 2.0, including C Function Parameters, is 100% free with no signup required.
With focused practice, most students grasp C Function Parameters in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now