PPS Test 5: Functions and Pointers
PPS Test 5: Functions and Pointers
PPS Test 5: Functions and Pointers
2.Class : CSE
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;
}
/*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
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:
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 .