PPS Test 5: Functions and Pointers:-: 1.name: Devansh Sharma ... 2.class: CSE 3.section: C (Cloud Computing)
PPS Test 5: Functions and Pointers:-: 1.name: Devansh Sharma ... 2.class: CSE 3.section: C (Cloud Computing)
2.Class : CSE
3.Section: …C(Cloud Computing)….
variables to it. Such functions are known as variables(location of variables) to the function
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
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
#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:
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.
#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;
}
Output
/*structure declaration*/
struct employee{
char name[30];
int empId;
float salary;
};
int main()
{
/*declare structure variable*/
struct employee emp;
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.
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 .