Prameters
Actual Parameters: Arguments passed to a function.
- They exist in the calling function (where the function is called).
- They supply input data to the function.
- They can be constants, variables, or expressions.
Formal Parameters: Parameters received by the function.
- They exist inside the function (local to the function).
- They store copies or references of actual parameters depending on the method of passing (value or reference).
- They terminate when the function ends.
#include <stdio.h>
void display(int x) { // x is formal parameterprintf("Value: %d\n", x);}
int main() {int num = 10;display(num); // num is the actual parameterreturn 0;}
Value: 10
Actual Argument:
- Definition: It refers to the expression used within the function call to pass a value or reference.
- Usage: Act as input values during function execution.
- Example: In foo(x), x is the actual argument.
Formal Argument:
- Definition: It refers to the parameters declared in the function definition.
- Usage: Serve as placeholders for receiving values from actual arguments.
- Example: In void foo(int y), y is the formal argument.
Call by Value:
- Copies the value of actual parameter
- Does not affect the actual parameter
- Default method in C.
#include <stdio.h>
void swap(int x, int y){int temp;temp = x;x = y;y = temp;printf("\nInside swap function:");printf("\nValue of x: %d", x);printf("\nValue of y: %d", y);}
int main(){int r = 10, v = 20;swap(r, v); // passing values (call by value)printf("\n\nInside main function:");printf("\nValue of r: %d", r);printf("\nValue of v: %d", v);
return 0;}
Inside swap function:Value of x: 20Value of y: 10
Inside main function:Value of r: 10Value of v: 20
Call by Reference:
- Passes the address of actual parameter
- Changes to formal parameter affect actual parameter
- Implemented using pointers in C.
#include <stdio.h>void changeValue(int* address){*address = 36979898;}
int main(){int a = 34, b =56;printf("The value of a now is %d\n", a);changeValue(&a);printf("The value of a now is %d\n", a);return 0;}
The value of a now is 34The value of a now is 36979898
Example
Section titled “Example”#include <stdio.h>void greetUser(char name[], int age) {printf("Hello %s! You are %d years old.\n", name, age);}int main() {greetUser("Akash", 25);greetUser("Shruti", 30);return 0;}
Hello Akash! You are 20 years old.Hello Shruti! You are 19 years old.