Explain parameters and arguments in C language ?

by ManinderSinghPatiala
3 minutes read

In C language, parameters and arguments are closely related concepts in function calls, but they have distinct roles:

Parameters:

  • Variables defined within a function’s declaration, acting as placeholders for values that will be provided when the function is called.
  • They specify the types of data the function expects to receive.
  • They are local to the function, meaning they only exist within its scope.

Arguments:

  • Actual values or expressions passed to a function when it’s called.
  • They are assigned to the corresponding parameters within the function.
  • They can be variables, literals, or expressions from the calling code.

Key Differences:

  • Scope: Parameters are local to the function, while arguments belong to the calling code’s scope.
  • Timing: Parameters are defined when the function is declared, while arguments are provided when the function is called.
  • Role: Parameters define the function’s interface, while arguments provide the specific input for each call.

Example:

C
int add(int x, int y) { // x and y are parameters
    return x + y;
}

int main() {
    int a = 5, b = 3;
    int sum = add(a, b); // a and b are arguments
    printf("Sum: %d\n", sum);
}

Explanation:

  1. The add function has two parameters, x and y, which act as placeholders for the values it will add.
  2. In main, the values of a and b are passed as arguments to the add function when it’s called.
  3. Within add, the arguments a and b are assigned to the parameters x and y, respectively.
  4. The calculation x + y is performed using the values provided by the arguments.
  5. The result (8) is returned from add and stored in the sum variable in main.

Related Posts

Leave a Reply

MSTIPS LOGO

From schoolrooms to YouTube, Maninder Singh’s passion for tech education knows no bounds. MsTips.in empowers learners of all ages with tips, tricks, and resources in multiple languages. Dive into IT tutorials, explore cyber safety, and embrace the fun side of tech—all in one vibrant space!

Copyright © 2023  All Right Reserved – Designed and Developed by Maninder Singh

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
-
00:00
00:00
Update Required Flash plugin
-
00:00
00:00