Lesson 35 of 60 – Function Parameters in C
58%

Function Parameters in C

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.

Note: Parameters are the variables declared in the function definition. The actual values supplied when calling the function are called arguments.

1. What are Function Parameters?

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.

2. Parameters and Arguments

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.

3. Function with One Parameter

void display(int number)
{
    printf("Number = %d", number);
}

int main()
{
    display(50);

    return 0;
}

The value 50 is passed to the parameter number.

4. Function with Two Parameters

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.

5. Function with Three Parameters

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.

6. Parameter Data Types

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.

7. Passing Integer Values

void printNumber(int number)
{
    printf("Number = %d", number);
}

int main()
{
    printNumber(100);

    return 0;
}

The integer value 100 is passed to the function.

8. Passing Floating-Point Values

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.

9. Passing Character Values

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.

10. Passing Variables as Arguments

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.

11. Passing Expressions as Arguments

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.

12. Parameter Names

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.

13. Function Prototype with Parameters

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.

14. Function Prototype Without Parameter Names

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.

15. Parameters and Return Values

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.

16. Multiple Calls with Different Arguments

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.

17. Parameter Values Are Local to the Function

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.

18. Pass-by-Value in C

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.

19. Passing Multiple Variables

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.

20. Parameters with Different Data Types

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.

21. Function Parameter with Calculation

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.

22. Passing Array to a Function

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.

23. Common Mistake: Wrong Number of Arguments

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.

24. Common Mistake: Wrong Parameter Type

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.

25. Function with a String Parameter

#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.

26. Complete Calculator Using Parameters

#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;
}

27. Complete Student Marks Example

#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.

28. Complete Maximum Number Example

#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.

29. Complete Array Sum Function

#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.

30. Practice Programs Using Function Parameters

Practice the following programs:

  1. Create a function that accepts two numbers and returns their sum.
  2. Create a function that accepts two numbers and returns their difference.
  3. Create a function that accepts two numbers and returns their product.
  4. Create a function to calculate the square of a number.
  5. Create a function to calculate the cube of a number.
  6. Create a function to find the largest of two numbers.
  7. Create a function to calculate the area of a rectangle.
  8. Create a function that accepts three marks and returns the average.
  9. Create a function that accepts an array and returns its sum.
  10. Create a simple calculator using functions with parameters.

Practice passing constants, variables, expressions, arrays, and different data types as function arguments.

📌 Key Points

  • Parameters allow functions to receive values.
  • Parameters are declared in the function definition.
  • Arguments are the actual values supplied during a function call.
  • A function can have one or multiple parameters.
  • Each parameter has a data type.
  • Variables can be passed as arguments.
  • Expressions can also be used as arguments.
  • Ordinary scalar arguments are passed by value in C.
  • Arrays can be passed to functions, with their size commonly passed separately.
  • Parameters make functions reusable with different input values.

🧠 Quick Quiz

Question: In the function int add(int a, int b), what are a and b?