Function parameters are variables listed in a function definition that receive values from the calling code. They allow a function to work with different data each time it is called.
A parameter is a variable that allows a function to receive a value from the calling code.
void greet(char name[])
{
printf("Hello %s", name);
}
Here, name is a parameter of the function.
Consider the following function:
int add(int a, int b)
{
return a + b;
}
int result = add(10, 20);
a and b are parameters.10 and 20 are arguments.void display(int number)
{
printf("Number = %d", number);
}
int main()
{
display(50);
return 0;
}
The value 50 is passed to the parameter number.
int add(int a, int b)
{
return a + b;
}
int main()
{
printf("%d", add(10, 20));
return 0;
}
The function receives two integer values and returns their sum.
int addThree(int a, int b, int c)
{
return a + b + c;
}
int main()
{
printf("%d", addThree(10, 20, 30));
return 0;
}
A function can have multiple parameters separated by commas.
Each parameter must have a data type.
void show(int age, float price, char grade)
{
printf("%d %.2f %c", age, price, grade);
}
Here the parameters have different data types.
void printNumber(int number)
{
printf("Number = %d", number);
}
int main()
{
printNumber(100);
return 0;
}
The integer value 100 is passed to the function.
void showPrice(float price)
{
printf("Price = %.2f", price);
}
int main()
{
showPrice(99.50f);
return 0;
}
The function receives a floating-point value through the
price parameter.
void showGrade(char grade)
{
printf("Grade = %c", grade);
}
int main()
{
showGrade('A');
return 0;
}
A character can be passed to a function using a char
parameter.
int square(int number)
{
return number * number;
}
int main()
{
int value = 6;
printf("Square = %d", square(value));
return 0;
}
A variable can be passed as an argument instead of directly passing a literal value.
int square(int number)
{
return number * number;
}
int main()
{
printf("%d", square(2 + 3));
return 0;
}
An expression can also be evaluated and its resulting value passed to a function.
Parameter names are local to the function and should describe the data they represent.
int multiply(int firstNumber, int secondNumber)
{
return firstNumber * secondNumber;
}
Meaningful names make functions easier to understand.
int add(int a, int b);
int main()
{
printf("%d", add(10, 20));
return 0;
}
int add(int a, int b)
{
return a + b;
}
The prototype tells the compiler the function name, return type, and parameter types before the function is called.
Parameter names can be omitted from a function prototype when only the types are being declared.
int add(int, int);
int add(int a, int b)
{
return a + b;
}
The parameter types in the prototype must describe the function's parameters correctly.
A function can receive values through parameters and send a result back
using return.
int multiply(int a, int b)
{
return a * b;
}
int main()
{
int result = multiply(5, 4);
printf("Result = %d", result);
return 0;
}
This creates a simple input-and-output flow through the function.
int square(int number)
{
return number * number;
}
int main()
{
printf("%d\n", square(2));
printf("%d\n", square(5));
printf("%d\n", square(10));
return 0;
}
The same function can process different arguments.
void display(int number)
{
printf("Inside function: %d\n", number);
}
int main()
{
int value = 50;
display(value);
return 0;
}
The parameter number is a local variable of the function.
For ordinary scalar arguments such as int, C passes a value
to the function. The function receives its own parameter variable.
void change(int number)
{
number = 100;
}
int main()
{
int value = 10;
change(value);
printf("%d", value);
return 0;
}
Output:
10
Changing the parameter does not directly change the original
value.
int total(int a, int b, int c)
{
return a + b + c;
}
int main()
{
int x = 10;
int y = 20;
int z = 30;
printf("Total = %d", total(x, y, z));
return 0;
}
Multiple variables can be passed in the same function call.
void display(int age, float marks, char grade)
{
printf("Age = %d\n", age);
printf("Marks = %.2f\n", marks);
printf("Grade = %c\n", grade);
}
int main()
{
display(20, 85.5f, 'A');
return 0;
}
Different parameters can have different data types.
float calculateArea(float length, float width)
{
return length * width;
}
int main()
{
float area;
area = calculateArea(10.5f, 5.0f);
printf("Area = %.2f", area);
return 0;
}
Parameters make the function reusable for different lengths and widths.
An array can be passed to a function. In a parameter list, an array parameter is adjusted to a pointer to its first element.
void display(int numbers[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", numbers[i]);
}
}
int main()
{
int numbers[] = {10, 20, 30};
display(numbers, 3);
return 0;
}
The size is commonly passed separately because the array parameter does not carry its element count.
The number and types of arguments should match the function's parameter list appropriately.
int add(int a, int b)
{
return a + b;
}
int main()
{
int result = add(10, 20);
printf("%d", result);
return 0;
}
The function above expects two arguments, so the call supplies two values.
float divide(float a, float b)
{
return a / b;
}
int main()
{
printf("%.2f", divide(10.0f, 4.0f));
return 0;
}
Using appropriate parameter types helps preserve the intended calculation and avoid unintended conversions.
#include <stdio.h>
void greet(char name[])
{
printf("Hello, %s!", name);
}
int main()
{
greet("Rahul");
return 0;
}
A character array can be used as a parameter when passing a string to a function.
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int subtract(int a, int b)
{
return a - b;
}
int multiply(int a, int b)
{
return a * b;
}
int main()
{
int a = 20;
int b = 5;
printf("Addition = %d\n", add(a, b));
printf("Subtraction = %d\n", subtract(a, b));
printf("Multiplication = %d\n", multiply(a, b));
return 0;
}
#include <stdio.h>
float calculateAverage(
float math,
float english,
float computer
)
{
return (math + english + computer) / 3.0f;
}
int main()
{
float average;
average = calculateAverage(
80.0f,
75.0f,
90.0f
);
printf("Average = %.2f", average);
return 0;
}
The function accepts three marks and returns their average.
#include <stdio.h>
int maximum(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
int main()
{
int result;
result = maximum(25, 40);
printf("Maximum = %d", result);
return 0;
}
The function receives two numbers and returns the larger one.
#include <stdio.h>
int calculateSum(int numbers[], int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += numbers[i];
}
return sum;
}
int main()
{
int numbers[] = {10, 20, 30, 40};
int sum = calculateSum(numbers, 4);
printf("Sum = %d", sum);
return 0;
}
The array and its number of elements are passed to the function.
Practice the following programs:
Practice passing constants, variables, expressions, arrays, and different data types as function arguments.
Question: In the function int add(int a, int b), what are a and b?