Open In App

Function Parameters in C

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

C function can receive some values to work on from its caller. These values are called function parameters or arguments and the process of supplying these values is called passing parameter/arguments.

Syntax

Function parameters must be specified in the function definition inside the parenthesis as shown:

C
// Single parameter
returnType name (parameter_type parameter_name) {
    ...
}

// Multiple parameters
rType name (p1_type p1_name, p2_type p2_name, .......) {
    ...
}

Here, the variables declared inside () are placeholders. They are not the actual values but instead variables that will be assigned the value that is received. They are also called formal arguments or simply parameters.

After parameters are defined in the function definition, we have to pass the exact same number of values in the function call. The values passed here are called actual arguments or simply arguments.

C
// Passing single argument
function_name(value/variable);

// Passing multiple argument
function_name(val1, val2, val3, ....);

The type and the sequence of the passed values should be same as in the function definition, otherwise, an error may occur.

Example

C
#include <stdio.h>

// Function definition with a single
// parameter
void printNum(int num) {
    printf("%d", num);
}

int main() {
    
    // Calling function and passing
    // argument
    printNum(10);
    return 0;
}

Output
10
function-parameters-and-arguments

Let's take another example in which we pass multiple values:

C
#include <stdio.h>

// Function definition with multiple
// parameters
int sum(int a, int b) {
    return a + b;
}

int main() {
    int num1 = 20, num2 = 10;
    
    // Calling function and passing
    // argument
    int res = sum(num1, num2);
    printf("%d + %d = %d", num1, num2, res);
    return 0;
}

Output
20 + 10 = 30

Parameters Passing Techniques

In C, there are 2 ways in which we can pass parameters:

  1. Pass by Value
  2. Pass by Pointer

In all the above examples, we have used pass by value method in which the copy of the value of the variable is created in the function. On the other hand, in pass by pointer method, we pass the address of the argument instead of the copy.

Example

C
#include <stdio.h>

// Passing a by value and b by pointer
void change(int a, int* b) {
    a = 99;
    *b = 99;
}

int main() {
    int num1 = 20, num2 = 10;
    
    // Passing num1 normally but passing
    // num2's address
    change(num1, &num2);
    
    printf("%d + %d", num1, num2);
    return 0;
}

Output
20 + 99

As we can see, only the value of num2 is changed which was passed by pointer instead of normal pass by value. This concept requires the knowledge of pointers in C. Refer to this article to know more - Parameter Passing Techniques in C

Properties of Function Parameters

1. Scope and Lifetime

Function parameters are created as soon as the function is called. Their lifetime and scope are as same as that of local variables defined inside functions. They are stored inside the function's call stack frame and removed when it is destroyed.

C
#include <stdio.h>

int sum(int a, int b) {
    
    // Trying to declare integer with
    // name a
    int a;
    return a + b;
}

int main() {
    int num1 = 10, num2 = 20;
    int res = sum(num1, num2);
    
    // Trying to access parameter of the
    // function
    printf("%d + %d = %d", a, b, res);

    return 0;
}


Output

main.c: In function ‘sum’:
main.c:7:9: error: ‘a’ redeclared as different kind of symbol
7 | int a;
| ^
main.c:3:13: note: previous definition of ‘a’ with type ‘int’
3 | int sum(int a, int b) {
| ~~~~^
main.c: In function ‘main’:
main.c:16:28: error: ‘a’ undeclared (first use in this function)
16 | printf("%d + %d = %d", a, b, res);
| ^
main.c:16:31: error: ‘b’ undeclared (first use in this function)
16 | printf("%d + %d = %d", a, b, res);
| ^

In the above code, two error occurs:

  • When we try to declare int a in the sum function. The compiler says that it is already declared in the definition.
  • When we try to access the arguments of function sum inside main which is out of scope. So, compiler shows undeclared variable error.

2. Parameters in Function Declaration

Function declaration is generally used when the function is called before its definition. In this case, it is optional to include function parameters.

C
return_type name();

The above statement tells the compiler about a function's name and return type. Mentioning parameters in the declaration is optional but valid.

C
return_type name(type1, type2, ...);

// With parameter names
return_type name(type1 p1, type2 p2, ...);

This type of function declaration is known as a function prototype or function signature. If it appears before the function call, we can define the function anywhere in the program.

Note: It is compulsory to include parameters inside the function definition.


Article Tags :

Similar Reads