function definition:
a block of code for a specific task.
Function Declaration:
Ex:
int addNumbers(int x, int y);
Function Calling:
Ex:
Int m = 5, n = 7;
int result = addNumbers(m,n);
Function Definition:
Ex:
int addNumbers(int x, int y)
{
int sum = x + y;
return sum;
}
Parameters:
the variables in the declaration and the definition of a function
(a, b).
Arguments:
the value of the variables that are passed to the parameters
(m = 5, n = 7).
Calling a function
Ex:
Add (m, n); // calling a function named add()
The actual function:
Add(int a, int b)
{
Return a+B;
}
m,n --> Actual parameters.
a,b --> Formal parameter.
Calling By values:
the values of actual parameters are copied to formal parameters and stored in the formal parameters' Memory location.
Ex:
Int x = 10, y = 20;
Fun(x, y); //fun calling
Printf("%d %d", x, y);
The actual function:
int fun(int x, int y)
{
x = 50;
y= 70;
}
OUTPUT --> 10 20
Because after the function got executed, its parameters got destroyed and its value didn’t get assigned to a variable in the main(), so no place in the memory.
Calling Buy references:
Both actual and formal parameters refer to the same memory location so any changes made to the formal parameter will get reflected in the actual parameters.
so ---> Instead of passing values we pass addresses (pointers).
Ex:
int x = 10, y = 20; Fun(&x, &y); // function calling
Printf("%d %d", x, y);
The actual function:
int fun(int **Ptr1, int **Ptr2) {
*Ptr1 = 50;
*Ptr2 = 70;
}
OUTPUT --> 50 70
SO
Referencing is the address of a variable (&) --> pointer.
When we want the value of what that address (pointer) refers to,
we want to((dereference)) it.
So:
we want to *&var which is *Ptr.
Pointer Declaration:
int *ptr;
// we tell the compiler to save memory space for a pointer named ptr.
Pointer Assignment:
int x = 5; ptr = &x;
Dereferencing a Pointer:
printf("The value of x is: %d", *ptr);
So, when we put all the parts together, the code will look like this:
#include <stdio.h>
int main()
{
int *ptr; int x = 5;
// Assigning the address of x to ptr
ptr = &x;
// Getting the value of the variable (x) which the pointer (ptr) refers to
printf("The value of x is: %d", *ptr);
return 0;
}
OUTPUT --> The value of x is: 5
In conclusion,
Formal parameters (parameters),
Actual parameters(arguments),
And pointers
are essential concepts in C language that are used to pass information between variables. By using these concepts, C functions can communicate information and modify variables more efficiently and flexibly. Understanding these concepts is crucial for any C programmer who wants to write efficient and effective code.
Hope it was helpful.