MCT-242L CP1 2022 LM13 33
MCT-242L CP1 2022 LM13 33
MCT-242L CP1 2022 LM13 33
OBJECTIVE:
This lab builds on the concept of user-defined functions and scope of variables in C Language. At the end of
this lab, you should be able to:
APPARATUS:
FUNCTION ARGUMENTS
If a function is to use arguments, it must declare variables that accept the values of the arguments. These
variables are called the formal parameters of the function. Formal parameters behave like other local
variables inside the function and are created upon entry into the function and destroyed upon exit.
While calling a function, there are two ways in which arguments can be passed to a function:
By default, arguments in C functions are passed by value. When arguments are passed by value, a copy of
argument is passed to the function. Therefore, changes made to the formal parameter by the called function
have no effect on a corresponding parameter. Consider the function swap() definition as follows:
#include <stdio.h>
// Function declaration
void swap(int, int);
int main ()
{
// local variable definition
int a = 100, b = 200;
return 0;
}
// Function definition to swap the values
void swap(int x, int y)
{
int temp;
temp = x; // save the value of x
x = y; // put y into x
y = temp; // put temp into y
}
// End of program
Before swap, value of a = 100
Before swap, value of b = 200
Program Output
After swap, value of a = 100
After swap, value of b = 200
It shows that there are no changes in the values, though they had been changed inside the function.
The passing arguments by reference method of passing arguments to a function copies the address of an
argument into the formal parameter. Inside the function, the address is used to access the actual argument
used in the call. It means the changes made to the parameter affect the passed argument.
When passing arguments by value, the only way to return a value back to the caller is using the return
statement. While this is suitable in many cases, there are few cases where better options are available. One
such cases when writing a program that needs to modify the value of an array (e.g., sorting an array). In this
case, it is more efficient and clearer to have the function modify the actual array passed to it, rather than
trying to get something back to the caller. One way to allow functions to modify the value of the argument is
by using pass by reference.
To pass a value by reference, argument pointers are passed to the functions just like any other value. So
accordingly, you need to declare the function parameters as pointer types as in the following function
swap(), which exchanges the values of the two integer variables pointed to, by their arguments.
#include <stdio.h>
// Function declaration
void swap(int *, int *);
int main ()
{
// local variable definition
int a = 100, b = 200;
// End of program
Before swap, value of a = 100
Before swap, value of b = 200
Program Output
After swap, value of a = 200
After swap, value of b = 100
The function parameters int *x and int *y are pointers, allowing the function to receive the addresses of
actual variables. By dereferencing the pointers with *x and *y, the function can access and modify the
values at the memory locations pointed to by x and y. This is a form of "call by reference" because the
function can indirectly modify the original values of x and y in the calling code.
● temp is a regular integer variable, not a pointer. It is used to store the value, not the address.
● temp = *x; saves the value pointed to by x in the variable temp. It doesn't store the address of x; it
stores the value that x points to.
● *x = *y; assigns the value pointed to by y to the memory location pointed to by x. It modifies the value
stored at the address pointed to by x.
● *y = temp; assigns the value stored in temp (the original value of *x) to the memory location pointed to
by y. It modifies the value stored at the address pointed to by y.
temp is a regular variable storing values. *x = *y; and *y = temp; are operations that modify the values at
the memory locations pointed to by x and y, respectively. They do not deal with addresses; they deal with
the values stored at those addresses. In summary, the use of pointers in the swap function allows it to modify
the values at the addresses pointed to by x and y, achieving a form of "call by reference" behavior. In
contrast, passing by value would involve working with copies of the values and would not modify the
original variables.
{
int x = 42; int *ptr; // pointer variable 'ptr'
ptr = &x; // Referencing: 'ptr' stores the address of 'x'
int y=*ptr; //Dereferencing 'ptr' to get the value: y now holds the value stored at
the memory address pointed to by 'ptr'
printf("Value of x: %d\n", x);
printf("Address of x: %p\n", ptr); // address of x, %p is used to print addresses
printf("Address of x: %p\n", &x); // %p is used to print addresses
printf("Value at the address stored in ptr: %d\n", y); // Dereferencing
printf("Value at the address stored in ptr: %d\n", *ptr);
return 0;
}
}
// End of program
Value of x: 42
Address of x: 000000d3553ffc74
Program Output Address of x: 000000d3553ffc74
Value at the address stored in ptr: 42
Value at the address stored in ptr: 42
Referencing:
Purpose: Obtaining the address of a variable allows you to create pointers that point to that address.
Dereferencing:
Definition: Dereferencing is the process of accessing the value stored at a memory address pointed to by a
pointer.
Operator: The dereference operator * is used for dereferencing.
Example: From example 13.3
int y=*ptr; // 'y' now holds the value stored at the memory address pointed to by
'ptr'
Purpose: Dereferencing allows you to access and manipulate the value stored at a specific memory location
through a pointer.
Referencing (&x) obtains the address of variable x and stores it in the pointer ptr. Dereferencing (*ptr)
accesses the value stored at the memory address pointed to by ptr. In summary, referencing is about
obtaining memory addresses, and dereferencing is about accessing values stored at those addresses through
pointers.
However, when we do not intend our function to alter the state of our object outside of our function, copying
prevents us from making unintentional mistakes and introduce bugs. Now we know when to use call by
value and call by reference in C.
Now we will discuss the advantages and disadvantages of call by value and call by reference in C.
● Guarantees that changes that alter the behavior of a parameter stay within its scope and do not affect
the value of an object passed into the function
● Reduce the chance of introducing subtle bugs which can be difficult to monitor.
● Passing by value removes the possible side effects of a function which makes your program easier to
maintain and reason with.
● Calling a function by reference does not incur performance penalties that copying would require.
Likewise, it does not duplicate the memory necessary to access the content of an object that resides
in our program.
● Allows a function to update the value of an object that is passed into it.
● Allows you to pass functions as references through a technique called function pointers which may
alter the behavior of a function. Likewise, lambda expressions may also be passed inside a function.
Both enable function composition which has neat theoretical properties.
● For every function that shares with the same object, your responsibility of tracking each change also
expands.
● Making sure that the object does not die out abruptly is a serious issue about calling a function by
reference. This is especially true in the context of a multithreaded application.
LAB SUBMISSION:
Create a Visual Studio Code Workspace, Lab_13 and c files (Task_13_1.c to Task_13_7.c) for individual
tasks and add them to Lab_13 workspace.
int main()
{
int x, y, z;
return 0;
}
int main()
{
float operand1, operand2, addition, subtraction, multiplication, division;
printf("Enter the 1st number >> ");
scanf("%f", &operand1);
printf("Enter the 2nd number >> ");
scanf("%f", &operand2);
return 0;
}
{
if (a > b)
{
int temp = a;
a = b;
b = temp;
}
if (a > c)
{
int temp = a;
a = c;
c = temp;
}
if (a > d)
{
int temp = a;
a = d;
d = temp;
}
if (b > c)
{
int temp = b;
b = c;
c = temp;
}
if (b > d)
{
int temp = b;
b = d;
d = temp;
}
if (c > d)
{
int temp = c;
c = d;
d = temp;
}
*x = a;
*y = b;
*m = c;
*o = d;
}
int main ()
{
int n1, n2, n3, n4, x, y, m, o;
printf("Enter n1: ");
scanf("%d", &n1);
printf("Enter n2: ");
scanf("%d", &n2);
printf("Enter n3: ");
scanf("%d", &n3);
printf("Enter n4: ");
scanf("%d", &n4);
Ascending_Order(n1, n2, n3, n4, &x, &y, &m, &o); // function call
return 0;
int main()
{
int digit, value;
printf("Enter the number >> ");
scanf("%d", &digit);
Number_of_Digits(digit, &value);
int main()
{
int number, digit, kth = 0;
printf("Enter the Number >> ");
scanf("%d", &number);
b = c + 1 - unit;
x = pow(10, b);
a = valV % x;
y = pow(10, b - 1);
z = a / y;
return z;
}
Answer: #include<stdio.h>
int main()
{
int n , fb;
printf("Enter the number of terms >> ");
scanf("%d", &n);
Fibonacci_Sequence(n , &fb);
return 0;
}
term1 = term2;
term2 = term3;
fb++;
}
printf("\n");
}
Answer: #include<stdio.h>
int main()
{
int num , sqre;
printf("Enter the number >> ");
scanf("%d" , &num);
Perfect_Number(num , &sqre);
return 0;
}
Students are advised to fill the manual and submit it before the upcoming lab. Kindly rename the file as
‘MCT-242L_CP1_2022_LM13_XX’, where XX is your roll number. After completing the manual, turn it in Google Classroom.