Skip to content

Commit ebca600

Browse files
authored
Functions
1 parent 7f941c0 commit ebca600

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

Basic_Functions.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <stdio.h>
2+
#include<conio.h>
3+
4+
int maximum(int, int); /*Function Declaration*/
5+
void multiply(int f, int g);/*Function Declaration, Here f and g are optional */
6+
void introduction();
7+
int addition(int, int);
8+
9+
int main()
10+
{
11+
int a, b, c, max;
12+
printf("\nEnter the Values of a and b :: ");
13+
scanf_s("%d %d", &a, &b);
14+
15+
introduction(); /*Function Call*/
16+
c = addition(a, b);
17+
printf("\nThe Addition of a and b is :: %d", c);
18+
max = maximum(a, b); /* Calling above function (Function Call) to find max of 'a' and 'b' */
19+
20+
printf("\nThe Maximum among the Two Numbers is %d", max);
21+
multiply(a,b);
22+
_getch();
23+
return 0;
24+
}
25+
26+
/* An example function that takes two parameters 'x' and 'y' as input and returns max of two input numbers */
27+
int maximum(int x, int y) /*Function Definition*/
28+
{
29+
if (x > y) /* Here x and y are called formal parameters */
30+
return x; /*Whereas a and b are called actual parameters */
31+
else
32+
return y;
33+
}
34+
35+
void multiply(int p, int q) /*Function Defintion, this can be done in one line. This Function does not return anything */
36+
{
37+
printf("\nThe Product of a and b is :: %d", p*q);
38+
}
39+
40+
inline int addition(int r, int s)
41+
{
42+
return(r + s);
43+
}
44+
45+
void introduction() /*This Fucntion does not receive any parameters/arguments */
46+
{
47+
printf("Hi\n");
48+
printf("How have you'll been during this Lockdown");
49+
/* There is no return statement inside this function, since its return type is void */
50+
}

Call_Value_Reference.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
4+
void swap_value(int, int);
5+
void swap_reference(int *, int *);
6+
7+
void main()
8+
{
9+
int a, b; /*a is name for a memory location*/
10+
printf("\nEnter the Values of a and b :: ");
11+
scanf_s("%d %d", &a, &b);
12+
13+
printf("\n********IN MAIN *************");
14+
printf("\nThe Values of a and b before Swapping are :: a = %d b = %d", a, b);
15+
//swap_value(a,b); /*Here the values of a and b are sent to the Swap Function */
16+
swap_reference(&a, &b); /*Here the address of a and b are sent and not their values*/
17+
printf("\n********BACK IN MAIN *************");
18+
printf("\nThe Values of a and b after Swapping are :: a = %d b = %d", a, b);
19+
_getch();
20+
}
21+
22+
void swap_value(int x, int y)
23+
{
24+
int temp;
25+
printf("\n********IN SWAP Function*************");
26+
printf("\nThe Values of x and y before Swapping are :: x = %d y = %d", x, y);
27+
temp = x;
28+
x = y;
29+
y = temp;
30+
printf("\nThe Values of x and y after Swapping are :: x = %d y = %d", x, y);
31+
32+
}
33+
34+
void swap_reference(int *x, int *y)
35+
{
36+
int temp;
37+
printf("\n********IN SWAP Function*************");
38+
printf("\nThe Values of x and y before Swapping are :: x = %d y = %d", *x, *y);
39+
temp = *x; /* '*' has to be read as value at */
40+
*x = *y;
41+
*y = temp;
42+
printf("\nThe Values of x and y after Swapping are :: x = %d y = %d", *x, *y);
43+
44+
}

Iteration_Recursion.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
4+
int factorial(int ); /*Recursive Function*/
5+
int factorial_iter(int );
6+
7+
void main()
8+
{
9+
int num;
10+
printf("\nEnter the Number :: ");
11+
scanf_s("%d", &num);
12+
13+
printf("\nThe Factorial of %d through Recursion is :: %d", num, factorial(num));
14+
printf("\nThe Factorial of %d through Iteration is :: %d", num, factorial_iter(num));
15+
16+
_getch();
17+
}
18+
19+
int factorial(int n) /*Note that a Recursive function has to have an if statement in order to end */
20+
{ /* otherwise it would go into an infinite loop. No Loop is permitted in a recursive function*/
21+
if (n == 0)
22+
return 1;
23+
else
24+
return n*factorial(n - 1);
25+
}
26+
27+
int factorial_iter(int n)
28+
{
29+
int i, fact = 1;
30+
31+
for (i = n; i >= 1; i--)
32+
fact = fact * i;
33+
34+
return fact;
35+
}

0 commit comments

Comments
 (0)