You are on page 1of 7

1.Name: …Devansh Sharma…………………...

2.Class : CSE
3.Section: …C(Cloud Computing)….

4.Roll No: …RA1911028030052…..

PPS Test 5: Functions And Pointers:-

Q1.Differentiate between Call by value and Call by reference mechanism of


function call.
Ans.
CALL BY VALUE CALL BY REFERENCE

While calling a function, instead of passing the

While calling a function, we pass values of values of variables, we pass address of

variables to it. Such functions are known as variables(location of variables) to the function

“Call By Values”. known as “Call By References.

In this method, the value of each variable in In this method, the address of actual variables in

calling function is copied into corresponding the calling function are copied into the dummy

dummy variables of the called function. variables of the called function.

With this method, the changes made to the With this method, using addresses we would have

dummy variables in the called function have an access to the actual variables and hence we

no effect on the values of actual variables in would be able to manipulate them.

the calling function.


Mechanism Of Function call:
When we call a function by passing the addresses of actual parameters then this
way of calling the function is known as call by reference. In call by reference, the
operation performed on formal parameters, affects the value of actual
parameters because all the operations performed on the value stored in the
address of actual parameters. It may sound confusing first but the following
example would clear your doubts.

Example of Function call by Reference


Lets take a simple example. Read the comments in the following program.

#include <stdio.h>>
void increment(int *var)
{
/* Although we are performing the increment on variable
* var, however the var is a pointer that holds the address
* of variable num, which means the increment is actually done
* on the address where value of num is stored.
*/
*var = *var+1;
}
int main()
{
int num=20;
/* This way of calling the function is known as call by
* reference. Instead of passing the variable num, we are
* passing the address of variable num
*/
increment(&num);
printf("Value of num is: %d", num);
return 0;
}
Output:

Value of num is: 21

Q2. It is possible to have a Function with Arguments but no return value?


Explain.
Ans. Function with arguments but no return value : When a function has
arguments, it receive any data from the calling function but it returns no values.
Syntax :
Function declaration : void function ( int );
Function call : function( x );
Function definition:
void function( int x )
{
statements;
}

Let us take an Example:-


// C code for function 
// with argument but no return value
#include <stdio.h>
  
void function(int, int[], char[]);
int main()
{
    int a = 20;
    int ar[5] = { 10, 20, 30, 40, 50 };
    char str[30] = "geeksforgeeks";
    function(a, &ar[0], &str[0]);
    return 0;
}
  
void function(int a, int* ar, char* str)
{
    int i;
    printf("value of a is %d\n\n", a);
    for (i = 0; i < 5; i++) {
        printf("value of ar[%d] is %d\n", i, ar[i]);
    }
    printf("\nvalue of str is %s\n", str);
}
Output:
value of a is 20
value of ar[0] is 10
value of ar[1] is 20
value of ar[2] is 30
value of ar[3] is 40
value of ar[4] is 50
The given string is : geeksforgeeks

Q3. What do you mean by recursion? Write a program to find factorial of a


number using recursion.
Ans. Recursion:-
Recursion is the process of repeating items in a self-similar way. In programming
languages, if a program allows you to call a function inside the same function, then it is
called a recursive call of the function.
void recursion() {
recursion(); /* function calls itself */
}

int main() {
recursion();
}

The C programming language supports recursion, i.e., a function to call itself. But while
using recursion, programmers need to be careful to define an exit condition from the
function, otherwise it will go into an infinite loop.
Recursive functions are very useful to solve many mathematical problems, such as
calculating the factorial of a number, generating Fibonacci series, etc.

C Program To Find factorial of a number using recursion:

#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}

long int multiplyNumbers(int n) {


if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}

Output

Enter a positive integer: 6


Factorial of 6 = 720

Q4. Write a program to enter the details of 10 employees of a company and


later print their details using function getdata() and putdata() respectively.

Ans. #include <stdio.h>

/*structure declaration*/
struct employee{
char name[30];
int empId;
float salary;
};

int main()
{
/*declare structure variable*/
struct employee emp;

/*read employee details*/


printf("\nEnter details :\n");
printf("Name ?:"); gets(emp.name);
printf("ID ?:"); scanf("%d",&emp.empId);
printf("Salary ?:"); scanf("%f",&emp.salary);

/*print employee details*/


printf("\nEntered detail is:");
printf("Name: %s" ,emp.name);
printf("Id: %d" ,emp.empId);
printf("Salary: %f\n",emp.salary);
return 0;
}

Output

Enter details :
Name ?:Mike
ID ?:1120
Salary ?:76543
Entered detail is:
Name: Mike
Id: 1120
Salary: 76543.000000
Q5.Define Argument and parameter with reference to the function. Also
comment how they differ from each other.
Ans. C functions exchange information by means of parameters and arguments. The
term parameter refers to any declaration within the parentheses following the
function name in a function declaration or definition; the term argument refers to
any expression within the parentheses of a function call.

The following rules apply to parameters and arguments of C functions:

 Except for functions with variable-length argument lists, the number of


arguments in a function call must be the same as the number of parameters in
the function definition. This number can be zero.
 The maximum number of arguments (and corresponding parameters) is 253 for
a single function.
 Arguments are separated by commas. However, the comma is not an operator
in this context, and the arguments can be evaluated by the compiler in any
order. There is, however, a sequence point before the actual call.
 Arguments are passed by value; that is, when a function is called, the parameter
receives a copy of the argument's value, not its address. This rule applies to all
scalar values, structures, and unions passed as arguments.
 Modifying a parameter does not modify the corresponding argument passed by
the function call. However, because arguments can be addresses or pointers, a
function can use addresses to modify the values of variables defined in the
calling function.
 In the old style, parameters that are not explicitly declared are assigned a
default type of int .
 The scope of function parameters is the function itself. Therefore, parameters
of the same name in different functions are unrelated.

In a function call, the types of the evaluated arguments must match the types of their
corresponding parameters. If they do not match, the following conversions are
performed in a manner that depends on whether a prototype is in scope for the
function:
 Arguments to functions specified with prototypes are converted to the
parameter types specified in the prototype, except that arguments
corresponding to an ellipsis (...) are converted as if no prototype were in scope.
(In this case, the rules in the following bullet apply.) For example:
 void f(char, short, float, ...);

 char c1, c2;
 short s1,s2;
 float f1,f2;

 f(c1, s1, f1, c2, s2, f2);

The arguments c1 , s1 , and f1 are passed with their respective types, while the
arguments c2 , s2 , and f2 are converted to int , int , and double , respectively.

 Arguments to functions that have no prototype in scope are not converted to the
types of the parameters. Instead, the expressions in the argument list are
converted according to the following rules:
o Any arguments of type float are converted to double .
o Any arguments of types char , unsigned char , short , or unsigned
short are converted to int .

A parameter is a variable in a method definition. When a method is called,  the


arguments are the data you pass into the method's parameters. Parameter is
variable in the declaration of function. Argument is the actual value of this
variable that gets passed to function.

You might also like