Function
Function
C Functions
#include <stdio.h>
int addition(int num1, int num2)
{
int sum;
/* Arguments are used here*/
sum = num1+num2;
/* Function return type is integer so we are returning *
an integer value, the sum of the passed numbers. */
return sum;
}
int main()
{
int var1, var2;
printf("Enter number 1: ");
scanf("%d",&var1);
printf("Enter number 2: ");
scanf("%d",&var2);
/* Calling the function here, the function return type * is integer
so we need an integer variable to hold the * returned value of
this function. */
int res = addition(var1, var2);
printf ("Output: %d", res);
return 0;
}
#include <stdio.h>
int add (int x, int y);
int main()
{
int a, b, result;
a = 5; b = 10;
result = add(a, b);
printf("%d + %d\ = %d\n", a, b, result);
return 0;
}
int add (int x, int y)
{
x += y;
return(x);
}
#include <stdio.h>
int max(int x, int y)
{
if (x > y)
return x;
else
return y;
}
int main()
{
int a = 10, b = 20;
// Calling above function to find max of 'a' and 'b'
int m = max(a, b);
printf("m is %d", m);
return 0;
Function with no argument and no return
value
When a function has no arguments, it does
not receive any data from the calling function.
Similarly when it does not return a value, the
calling function does not receive any data
from the called function.
#include <stdio.h>
void introduction()
{
printf("Hi\n");
printf("My name is Chaitanya\n");
printf("How are you?");
}
int main()
{
/*calling function*/
introduction();
return 0;
}
Function with no arguments but returns a value :
#include <stdio.h>
int area(); //function prototype with return type int
int main()
{
int square_area;
square_area = area(); //function call
printf("Area of Square = %d”, square_area);
return 0;
}
int area()
{
int square, a;
printf("Enter the side of square :");
scanf("%d”, &a);
square = a*a;
return square;
Function with arguments and no return values
#include <stdio.h>
void area(int a); //function prototype
int main()
{
int a;
printf("Enter the side of square :");
scanf("%d",&a);
area(a); //function call return 0;
}
void area(int a)
{
int s;
s = a*a;
printf(“Area of Square = %d”, a);