2. Functions
A function is a self-contained block of
statements that perform a coherent
task of some kind. If you have a task
that is always performed in the same
way than instead of putting the same
functionality at every point we should
make function and can call by just
declaring function name.
#include<stdio.h>
void func1();
int main()
{
func1();
printf("nthis is first program with funtion");
return 0;
}
void func1()
{
printf("nthis is func1");
}
Output:
This is func1
This is the first program with function
3. Important points about function
A function gets called when the function name is followed by a semincolon.
A function is defined when function name is followed by a pair of braces in
which one or more statements may be present.
Any function can be called from any other function. Even main can be called
from other function.
A function calls itself such process known as recursion.
5. Definition of Function
int func1()
{
int a;
printf("nthis is func1");
return 0;
}
Function Definition-
1. Function name
2. Function type
3. List of parameters
4. Local variable declaration
5. Function statement
6. A return statement
6. Return values and their type
A function may or may not send back any value to the calling function. If it does it is done through return
statement. Called function can only return only one value per call.
return;
or
return(expression)
return: will not return any value but will return the control back to the calling function.
If(error)
{
return(0);
}
Return expression: will return some value of the function back to the called function.
return(p);
return(x*y);
7. …
So basically, return serves two purposes:
On executing the return statement, it immediately transfers the control back to
the calling function.
It returns the value present in the parentheses after return, to the calling function.
8. Passing values between functions-
Passing the vales to the function allows us to communicate with the function. Using this
we can pass our values in order to get the results, for that we need to specify the
function name with arguments.
Example:
void main()
{
Funtion1();
Function2(a);
}
Function1()
{ }
Function2(int a)
{ }
9. Categories of Function-
Functions with no arguments and no return value.
Functions with arguments and no return value.
Functions with no arguments and return a value.
Functions that return multiple return values.
10. Swapping a Number – General
#include <stdio.h>
int main()
{
int x, y, t;
printf("Enter two integersn");
scanf("%d%d", &x, &y);
printf("Before SwappingnFirst integer = %dnSecond integer = %dn", x, y);
t = x;
x = y;
y = t;
printf("After SwappingnFirst integer = %dnSecond integer = %dn", x, y);
return 0;
}
11. #include <stdio.h>
int main()
{
int x, y, t;
printf("Enter two integersn");
scanf("%d%d", &x, &y);
printf("Before SwappingnFirst integer = %dnSecond integer = %dn", x, y);
swap(x,y);
printf("After SwappingnFirst integer = %dnSecond integer = %dn", x, y);
return 0;
}
void swap(int x, int y) //Swap function definition
{
int t;
t = x;
x = y;
y = t;
printf("After SwappingnFirst integer = %dnSecond integer = %dn", x, y);
}
12. Swapping a number
#include <stdio.h>
void swap(int*, int*); //Swap function declaration
int main()
{
int x, y;
printf("Enter the value of x and yn");
scanf("%d%d",&x,&y);
printf("Before Swappingnx = %dny = %dn", x, y);
swap(&x, &y);
printf("After Swappingnx = %dny = %dn", x, y);
return 0;
}
void swap(int *a, int *b) //Swap function definition
{
int t;
t = *b;
*b = *a;
*a = t;
}
13. Recursion
Recursion is a special case where
function calls itself again and
again.
main()
{
printf(“this is the example of recursion”);
main();
}
14. …
Another example is factorial:
Factorial of n=n(n-1)(n-2)…….1
Factorial of 4=4*3*2*1 =24
factorial(int n)
{
int fact;
if(n==1)
return 1;
else
fact=n*factorial(n-1);
return(fact);
}
fact=3*factorial(2)
fact=3*2*factorial(1)
fact=3*2*1
fact=6
15. 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.
17. …
Desired Input/output
Input : 2
Output : Disk 1 moved from A to B
Disk 2 moved from A to C
Disk 1 moved from B to C
Input : 3
Output : Disk 1 moved from A to C
Disk 2 moved from A to B
Disk 1 moved from C to B
Disk 3 moved from A to C
Disk 1 moved from B to A
Disk 2 moved from B to C
Disk 1 moved from A to C
18. #include <stdio.h>
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)
{
if (n==1)
{
printf("nMove disk 1 from rod : %d",from_rod);
printf("nMove disk 1 to rod : %d",to_rod);
return;
}
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
printf("nMove disk %d from rod : %d",n,from_rod);
printf("nMove disk %d to rod : %d",n,to_rod);
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
int main()
{
int n = 3; // Number of disks
towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
return 0;
}