C Programming • Arrays & Functions
C Programming / Parameter passing in functions

Parameter passing in functions

Notes 3 Arrays & Functions

Definition: Parameter passing is the process of providing data from the calling function to the parameters of the called function.

Notes

Parameter Passing in Functions

Definition: Parameter passing is the process of providing data from the calling function to the parameters of the called function.

💡 Example: In add(x, y), the values of x and y are passed to the corresponding parameters of the add() function.

Why is Parameter Passing Needed?

Functions often need data from the calling program to perform their task. Parameter passing provides that data to the function.

💡 Example: A function that calculates the square of a number needs the number to be passed to it.

Basic Parameter Passing

A function can receive one or more parameters and use them inside its body.

💡 Example: In square(int n), n is the parameter that receives the value supplied during the function call.
int square(int n)
{
    return n * n;
}

Parameter Passing Flow

Calling Function ↓ Actual Arguments ↓ Function Call ↓ Formal Parameters ↓ Function Executes ↓ Result / Action

Example

In the following example, the values of x and y are passed to a and b.

int add(int a, int b)
{
    return a + b;
}

int result = add(x, y);
💡 Mapping:

xa
yb

Ways of Passing Parameters

In C programming, parameter passing is commonly discussed in terms of passing values and using addresses to allow a function to work with the caller's data.

Method What is Passed? Effect on Original Variable
Call by Value Value of the variable Changes inside function do not change the original variable.
Address-based passing Address of the variable Function can modify the original variable.
📌 Important: Detailed Call by Value and Call by Reference concepts are covered separately in the next two topics.

Parameter Passing and Data Flow

The direction of data flow depends on how the function is called and how its parameters are used.

💡 Example: A value can be supplied to a function, processed there, and the result can be returned to the calling function.

Returning a Value

A function may return a calculated value to the calling function. This allows the calling code to use the result after the function completes.

💡 Example: result = add(10, 20); stores the value returned by add() in result.

Practical Example — Passing Parameters to a Function

Problem Statement

Write a C program that accepts two numbers, passes them to a user-defined function, and displays their product.

Learning Outcomes

  • Understand how parameters are passed to a function.
  • Pass actual arguments to formal parameters.
  • Return the calculated result to the calling function.

Hint

Create a function multiply() with two integer parameters. Pass the two input variables during the function call.

Theory

When a function is called, the arguments supplied by the caller are associated with the parameters defined by the called function. The function then uses those parameters to perform its task.

Program

#include <stdio.h>

int multiply(int a, int b);       // function declaration

int main()
{
    int x, y, result;

    printf("Enter two numbers: ");
    scanf("%d %d", &x, &y);

    result = multiply(x, y);      // pass actual arguments

    printf("Product = %d
", result);

    return 0;
}

int multiply(int a, int b)
{
    return a * b;                 // calculate and return product
}

Expected Output

Enter two numbers: 6 8
Product = 48

Note

Here, x and y are the actual arguments, while a and b are the formal parameters. The function receives the data, performs the multiplication and returns the result.

Important Points for Exam

  • Parameter passing transfers data to a called function.
  • Actual arguments are supplied during the function call.
  • Formal parameters receive the supplied data inside the function.
  • Arguments are matched with parameters according to their position.
  • A function may process the parameters and return a result.
  • Call by Value and Call by Reference are studied separately.

Quick Revision

Term Remember
Parameter Passing Providing data to a function.
Actual Argument Value or variable supplied in function call.
Formal Parameter Variable that receives the supplied data.
Return Value Result sent back to the calling function.

Important Exam Questions

Short Answer Questions

  1. What is parameter passing in functions?
  2. Why are parameters passed to a function?
  3. What is the difference between an actual argument and a formal parameter?
  4. How are arguments associated with parameters?
  5. What is the role of a return value?

Long Answer Questions

  1. Explain parameter passing in functions with a suitable example.
  2. Explain the relationship between actual arguments and formal parameters during a function call.
  3. Write a C program to pass two values to a function and return the calculated result.
🎥 Recommended Learning

Watch a beginner-friendly explanation of parameter passing in C functions.

▶ Watch: Parameter Passing in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for parameter passing will be provided here.

🧠 Mind Map

Use the mind map for quick revision of actual arguments, formal parameters and return values.