Unit No 1 User Defined Functions and Pointer
Unit No 1 User Defined Functions and Pointer
In C programming language, parameters are variables that are used to pass values or
references between functions. There are two types of
parameters: actual and formal parameters. In this article, we will discuss the difference
between actual and formal parameters in C, how they work, and their importance in function
calls.
Actual Parameters in C:
Actual parameters are the values that are passed to a function when it is called. They are
also known as arguments. These values can be constants, variables, expressions, or even
other function calls. When a function is called, the actual parameters are evaluated, and their
values are copied into the corresponding formal parameters of the called function.
In C, actual parameters are enclosed in parentheses and separated by commas. For example,
consider the following function call:
In this function call, 2 and 3 are the actual parameters, and they are passed to the function
add, which takes two formal parameters.
Formal Parameters in C:
Formal parameters are the variables declared in the function header that are used to receive
the values of the actual parameters passed during function calls. They are also known
as function parameters. Formal parameters are used to define the function signature and to
specify the type and number of arguments that a function can accept.
In C, formal parameters are declared in the function declaration or definition. For example,
consider the following function declaration:
Actual and formal parameters are important in C programming because they enable the
passing of values and references between functions. Functions can be designed to accept
parameters that are passed by value, reference, or by pointer. It enables the creation
of flexible and reusable code that can be used in different contexts.
Passing parameters by value means that the function receives a copy of the value of
the actual parameter. It allows the function to modify the copy without affecting the original
value of the actual parameter.
Passing parameters by reference means that the function receives a reference to the
memory location of the actual parameter. It allows the function to modify the value of the
actual parameter directly.
When passing parameters by value, a copy of the actual parameter's value is made and
passed to the function. It means that any modifications made to the parameter inside the
function do not affect the original value of the actual parameter. It is useful when we want
to manipulate the value of a variable inside a function but don't want to modify the original
value outside the function.
void increment(int a) {
a++;
printf("a inside function: %d\n", a);
}
void main() {
int x = 5;
increment(x);
printf("x outside function: %d\n", x);
getch();
}
Output:
a inside function: 6
x outside function: 5
In this example, the function increment takes an integer parameter a by value. The function
increments the value of a and prints it to the console. We call the function increment with
the variable x as the actual parameter. As we can see, the value of x is not affected by the
function increment, even though we passed it as a parameter.
When passing parameters by reference, the memory address of the actual parameter is
passed to the function. It means that any modifications made to the parameter inside the
function affect the original value of the actual parameter. It is useful when we want to
modify the value of a variable outside the function..
The actual parameters are the values or variables that are passed to a function when it is
called. On the other hand, the formal parameters are the variables in the function definition
that receive the values of the actual parameters. When a function is called, the values of the
actual parameters are copied to the formal parameters. It means that any modifications made
to the formal parameters inside the function do not affect the values of the actual parameters
outside the function.
int main() {
int x = 5;
int y = 10;
add(x, y);
return 0;
}
Output:
5 + 10 = 15
In this example, the function add takes two integer parameters a and b by value. The
function calculates their sum and prints it to the console. We call the function add with
the variables x and y as the actual parameters. As we can see, the values of x and y are not
modified by the function add.
EXP.2 Write a program to demonstrate actual arguments and formal arguments.
#include <stdio.h>
int main() {
// Declare variables for actual arguments
char name_input[50];
int age_input;
return 0;
}
```
In this C program, `name` and `age` are formal parameters of the `greet` function, and `name_input`
and `age_input` are the actual arguments provided when calling the function. The `scanf` function is
used to get user input for the actual arguments. The function then prints a personalized greeting
using these arguments.
In C, there are various general problems which requires passing more than one variable of
the same type to a function. For example, consider a function which sorts the 10 elements in
ascending order. Such a function requires 10 numbers to be passed as the actual parameters
from the main function. Here, instead of declaring 10 different numbers and then passing
into the function, we can declare and initialize an array and pass that into the function. This
will resolve all the complexity since the function will now work for any number of values.
As we know that the array_name contains the address of the first element. Here, we must
notice that we need to pass only the name of the array in the function which is intended to
accept an array. The array defined as the formal parameter will automatically refer to the
array specified by the array name defined as an actual parameter.
There are 3 ways to declare the function which is intended to receive an array as an
argument.
1. First way:
2. Second way:
3. Third way:
You can also use the concept of a pointer. In pointer chapter, we will learn about it.
#include<stdio.h>
int minarray(int arr[],int size){
int min=arr[0];
int i=0;
for(i=1;i<size;i++){
if(min>arr[i]){
min=arr[i];
}
}//end of for
return min;
}//end of function
int main(){
int i=0,min=0;
int numbers[]={4,5,7,3,8,9};//declaration of array
min=minarray(numbers,6);//passing array with size
printf("minimum number is %d \n",min);
return 0;
}
Output
minimum number is 3
#include<stdio.h>
void Bubble_Sort(int[]);
void main ()
{
int arr[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
Bubble_Sort(arr);
}
void Bubble_Sort(int a[]) //array a[] points to arr.
{
int i, j,temp;
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Printing Sorted Element List ...\n");
for(i = 0; i<10; i++)
{
printf("%d\n",a[i]);
}
}
Output
As we know that, a function can not return more than one value. However, if we try to write
the return statement as return a, b, c; to return three values (a,b,c), the function will return
the last mentioned value which is c in our case. In some problems, we may need to return
multiple values from a function. In such cases, an array is returned from the function.
Returning an array is similar to passing the array into the function. The name of the array is
returned from the function. To make a function returning an array, the following syntax is
used.
int * Function_name() {
//some statements;
return array_type;
}
To store the array returned from the function, we can define a pointer which points to that
array. We can traverse the array by increasing that pointer since pointer initially points to the
base address of the array. Consider the following example that contains a function returning
the sorted array.
#include<stdio.h>
int* Bubble_Sort(int[]);
void main ()
{
int arr[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
int *p = Bubble_Sort(arr), i;
printf("printing sorted elements ...\n");
for(i=0;i<10;i++)
{
printf("%d\n",*(p+i));
}
}
int* Bubble_Sort(int a[]) //array a[] points to arr.
{
int i, j,temp;
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a;
}
Output