# 9.7 – Functions – programs to practice part-1

by on November 17, 2017

### Functions accept no arguments and return no value:

Program:

```#include<stdio.h>
int main()
{
address();            /* calling the function */
return 0;
}
{
printf("\n Stephen William Hawking");
printf("\n Oxford, England");
printf("\n United Kingdom");
}
```

Output:
Stephen William Hawking
Oxford, England
United Kingdom

Example explained:
address() is a function accepts no arguments and returns no value.

### Functions accept arguments and return no values:

Specification: Accept two numbers and print the sum, subtraction, product and division of them using a function manip().

Program:

```#include<stdio.h>
void manip(int,int);             /* optional */
int main()
{
int a,b;
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
manip(a,b);                     /* sending two numbers as arguments */
return 0;
}
void manip(int x,int y)
{
printf("Sum %d",x+y);
printf("\n Subtraction %d",x-y);
printf("\n Product %d",x*y);
printf("\n Division %f",(float)x/y);
}
```

Execution:
Enter two numbers:
25
2
Sum 27
Subtraction 23
Product 50
Division 12.5

Example explained:
The function manip() accepts two integers from main() and prints their sum, subtraction, product and division of them

Specification: Accept the length, breadth of a rectangle and print the area, perimeter using the function rectangle.

Program:

```#include<stdio.h>
void rectangle(int,int);  /* optional */
int main()
{
int l,b;
printf("length of rectangle:");
scanf("%d",&l);
scanf("%d",&b);
rectangle(l,b);               /* sending two sides as arguments */
return 0;
}
void rectangle(int x,int y)
{
printf("Area %d",x*y);
printf("\n Perimeter %d",2*(x+y));
}
```

Execution:
length of rectangle: 7
Area: 35
Perimeter: 24

Specification: Accept the radius of a circle and print the area, circumference using the function circle()

Program:

```#include<stdio.h>
void circle(int);                        /* optional */
int main()
{
return 0;
}
void circle(int r)
{
printf("Area %f",3.14*r*r);
printf("\n Circumference %f",2*3.14*r);
}
```

Execution:
Area 706.500000
Circumference 94.200000

### Functions accept arguments and return value:

Specification: Accept three sides of a box and print its volume using the function box.

Program:

```#include<stdio.h>
int box(int,int,int);                  /* optional */
int main()
{
int breath,width,height,vol;
printf("Volume of box %d",vol);
return 0;
}
int box(int b,int w,int h)
{
return b*w*h;
}
```

Execution:
5   7   3
Volume of box: 105

Example explained:
Here the function box() accepts breadth, width and height from the main and returns a single integer that is volume.
The returned value is assigned to the variable “vol” of main() and is printed as output.

Specification: Accept the limit and print the sum of natural numbers from 1 to the given limit using the function getsum()

Program:

```#include<stdio.h>
int getsum(int);
int main()
{
int n,sum;
printf("Enter the limit:");
scanf("%d",&n);
sum=getsum(n);
printf("Sum of natural numbers %d",sum);
return 0;
}
int getsum(int n)
{
int  i,s;
for(s=0,i=1;i<=n;i++)
s=s+i;
return s;
}
```

Execution:
Enter the limit: 10
Sum of natural numbers 55

Example explained:
Here both the actual and formal arguments are defined with the same name “n”.
It can be done because they are local variables to their functions.

Specification: Accept any number and print the sum of individual digits using the function digitsum().

Program:

```#include<stdio.h>
int digitsum(int);
int main()
{
int n,s;
printf("Enter any integer:");
scanf("%d",&n);
s=digitsum(n);
printf("Sum of all the digits %d",s);
return 0;
}
int digitsum(int n)
{
int sum;
for(sum=0;n!=0;n=n/10)
sum=sum+n%10;
return sum;
}
```

Execution:
Enter any integer: 6547
Sum of all the digits 22

Previous post: