You are on page 1of 29

PARAMETER PASSING METHODS

 There are two different ways of passing parameters to a method, they are:

Call by value

.

 Call by reference
CALL BY VALUE
 Actual arguments are passed to the formal arguments.
 Any changes made to the formal argument does not affect the actual
argument.

.
EXAMPLE

#include <stdio.h> int change(int a,int b)


#include<conio.h> {

.
void main() int c;
{ c=a;
int x,y,change(int,int); a=b;
printf("\nEnter value of x:"); b=c;
scanf("%d",&x); printf("\nValues in the
printf("\nEnter value of y:"); Fuction -->x=%d,y=
scanf("%d",&y); %d",a,b);
change(x,y); }
printf("\n\nValues in the Main()--
>x=%d,y=%d",x,y);
}
OUTPUT
Enter value of x:5
Enter value of y:6

.
Values in the Function -->x=6,y=5(FORMAL ARGU)
Values in the Main()-->x=5,y=6(ACTUAL ARGU)
CALL BY REFERENCE

 Instead of passing values, the address of the argument will be passed.

.
 Any changes made to the formal argument will affect the actual argument.
EXAMPLE
printf("\n\nValues in the Main()-->x=
#include <stdio.h> %d,y=%d",x,y);
#include<conio.h> }
void main() int change(int *a,int *b)
{ {
int x,y,change(int*,int*); int c;
printf("\nEnter value of x:"); c=*a;
scanf("%d",&x); *a=*b;
printf("\nEnter value of y:");
*b=c;
scanf("%d",&y);
printf("\nValues in the Function ->x=
change(&x,&y);
%d,y=%d",*a,*b);
}
OUTPUT
Enter value of x:5
Enter value of y:6

.
Values in the Function -->x=6,y=5
Values in the Main()-->x=6,y=5
RECURSION

 It is a process of calling the same function itself again and


again until some condition is satisfied.

.
Syntax:
func1()
{
………..
func1();
…………
}
EXAMPLE

#include<stdio.h> if(x==1)
#include<conio.h> return(1);

.
void main() else
{ f=x*rec(x-1);
int a; return(f);
int rec(int); }
printf("\nEnter the number:");
scanf("%d",&a);
Output:
printf("The factorial of %d! is Enter the number:5
%d",a,rec(a));
The factorial of 5! is 120
}
int rec(int x)
{
int f;
EXAMPLE: WORKING OF 3!

.
LIBRARY FUNCTION
 Library functions are the pre-defined functions.
 The library function provides functions like mathematical, string
manipulation etc,.

.
 In order to use a library function, it is necessary to call the
appropriate header file at the beginning of the program.
 The header file informs the program of the name, type, and number and
type of arguments, of all of the functions contained in the library in
question.
 A header file is called via the preprocessor statement.
SOME EXAMPLES OF
LIBRARY FUNCTIONS
 sqrt(x):

.
It is used to find the square root of x
Example: sqrt(36) is 6

 abs(x):
It is used to find the absolute value of x
Example: abs(-36) is 36

 pow(x,y):
It is used to find the value of xy
Example: pow(5,2) is 25
 ceil(x):
It is used to find the smallest integer greater than or equal to x.
Example: ceil(7.7) is 8

 rand():

.
It is used to generate a random number.

 sin(x):
It is used to find the sine value of x
Example: sin(30) is 0.5

 cos (x):
It is used to find the cosine value of x
Example: cos(30) is 0.86
 tan(x):
It is used to find the tan value of x
Example: tan(30) is 0.577

.
 toascii(x):
It is used to find the ASCII value of x
Example: toascii(a) is 97

 toupper(x):
It is used to convert lowercase character to uppercase.
Example: toupper(‘a’) is A toupper(97) is A

 tolower(x):
It is used to convert uppercase character to lowercase.
Example: tolower(‘A’) is a
EXAMPLE:

#include<stdio.h>
#include<conio.h>
#include<math.h>

.
#include<ctype.h>
void main()
{
int x,y=2;
printf("\nEnter the number:");
scanf("%d",&x);
printf("\nThe square root of %d is %f",x,sqrt(x));
printf("\nThe value of %d power%dis%f ",x,y,pow(6,2));
printf("\nThe ceiling of 6.7 is %f",ceil(6.7));
printf("\nThe floor of 6.7 is %f",floor(6.7));
printf("\nThe absolute value of -6 is %d",abs(-6));
printf("\nThe value of sin 45 is %f",sin(45));
printf("\nThe uppercase of 'a' is %c",toupper('a'));

.
printf("\nThe uppercase of 97 is %c",toupper(97));
getch();
}
OUTPUT:
Enter the number:6

The square root of 6 is 2.449490

.
The value of 6 power 2 is 36.000000
The ceiling of 6.7 is 7.000000
The floor of 6.7 is 6.000000
The absolute value of -6 is 6
The value of sin 45 is 0.850904
The uppercase of 'a' is A
The uppercase of 97 is A
APPLICATIONS:
 Math functions
 Computation of Sine Series

.
 Random Number Generation

 Tower of Hanoi

 Factorial using Recursive functions.


COMPUTATION OF SINE SERIES
#include <stdio.h>
do {
#include <math.h>
denominator = 2 * n * (2 * n + 1);
#include <stdlib.h> term = -term * x * x / denominator;
void main() sinx = sinx + term; n = n + 1;
{ int n, x1; }
while (accuracy <= fabs(sinval - sinx));
float accuracy, term, denominator, x, sinx, sinval; printf("Sum of the sine series = %f \n", sinx);
printf("Enter the value of x (in degrees) \n"); printf("Using Library function sin(%d) = %f\n",
scanf("%f", &x); x1 = x; x1, sin(x));
/* Converting degrees to radians */ }

x = x * (3.142 / 180.0);
sinval = sin(x);
printf("Enter the accuracy for the result \n");
scanf("%f", &accuracy);
term = x;
sinx = term;
n = 1;
Problem Solution
It’s a non-differentiable function. Start at zero, then goes up to 1, then back down to 0. But
then, instead of going negative, it will just “reflect” about the x-axis. The derivative is 1 and
then -1 for every x such that sin(x) = 0 (i.e. 0, 180, 360, 540, 720 …).
Program Explanation
In this C program, we are reading the number of the terms in a series using ‘x’ variable. To
convert degrees to radians the following formula is used
Sin(x) = x *(3.142/180.0).
Do while loop is used to compute the sum of the sine series. Compute the summation of the
value of ‘n’ variable with 1 and multiply the value with 2 and again multiply with the value
of ‘n’ variable.
Multiply the value of ‘x’ variable twice with the value of ‘term’ variable and take negation of
the value then divide the value by ‘denominator’ variable. Compute the summation of the
value of ‘sinx’ variable with the value of ‘term’ variable.
While condition is used to check the value of ‘accuracy’ variable is less than or equal to fabs()
function value. If the condition is true, then execute the iteration of the loop. Print the value
of sin(x) using printf statement.
RANDOM NUMBER GENERATION
 The random function is used to find the random number between any two defined
numbers. In the C programming language, the random function has two inbuilt
functions: rand() and srand() function.
rand() function
In the C programming language, the rand() function is a library function that generates
the random number in the range [0, RAND_MAX]. When we use the rand() function in
a program, we need to implement the stdlib.h header file because rand() function is
defined in the stdlib header file. It does not contain any seed number. Therefore, when
we execute the same program again and again, it returns the same values.
srand() function
 The srand() function is a C library function that determines the initial point to generate
different series of pseudo-random numbers. A srand() function cannot be used without
using a rand() function. The srand() function is required to set the value of the seed
only once in a program to generate the different results of random integers before
calling the rand() function.
RAND()
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
/* It returns the same sequence of random
number on every execution of the program. */
printf(" Random Numbers are: \n");
for (i = 0; i <5; i++)
{
printf(" %d", rand());
}
return 0;
}
SRAND()
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int num, i;
time_t t1; // declare time variable
printf(" Enter a number to set the limit for a random number \n");
scanf (" %d", &num);
/* define the random number generator */
srand ( (unsigned) time (&t1)); // pass the srand() parameter
printf("\n"); // print the space
/* generate random number between 0 to 50 */
for (i = 0; i <num; i++) {
printf( "%d \n", rand() % 50);
}
return 0; }
TOWER OF HANOI
Tower of Hanoi is a mathematical puzzle where we have three rods and n disks.
The objective of the puzzle is to move the entire stack to another rod, obeying the
following simple rules:
1) Only one disk can be moved at a time.

2) Each move consists of taking the upper disk from one of the stacks and placing it on
top of another stack i.e. a disk can only be moved if it is the uppermost disk on a
stack.
3) No disk may be placed on top of a smaller disk.
PROGRAM
#include <stdio.h>
// C recursive function to solve tower of hanoi puzzle
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)
{
if (n == 1)
{
printf("\n Move disk 1 from rod %c to rod %c", from_rod, to_rod);
return;
}
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
printf("\n Move disk %d from rod %c to rod %c", n, from_rod, to_rod);
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
}
int main()
{
int n = 4; // Number of disks
towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
return 0;
}
FIBONACCI SERIES USING RECURSION
#include<stdio.h>
void printFibonacci(int m){
static int m1=0,m2=1,m3;
if(m>0){
m3 = m1 + m2;
m1 = m2;
m2 = m3;
printf(“%d “,m3);
printFibonacci(m-1);
}}
int main(){
int m;
printf(“Please enter your preferred number of elements here: “);
scanf(“%d”,&m);
printf(“The Fibonacci Series will be: “);
printf(“%d %d “,0,1);
printFibonacci(m-2); //We have used m-2 because we have 2 numbers already printed here
return 0;}
OUTPUT:
The output obtained out of the code mentioned above would be:
Please enter your preferred number of elements here: 15
The Fibonacci Series will be:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

You might also like