0% found this document useful (0 votes)
7 views

Function

The document explains the concept of functions in C programming, highlighting their importance for modularity and reusability. It details two types of functions: library functions and user-defined functions, along with their syntax and examples. Additionally, it covers various scenarios for functions, including those with no arguments, those that return values, and those that take arguments without returning values.

Uploaded by

igntu.hima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Function

The document explains the concept of functions in C programming, highlighting their importance for modularity and reusability. It details two types of functions: library functions and user-defined functions, along with their syntax and examples. Additionally, it covers various scenarios for functions, including those with no arguments, those that return values, and those that take arguments without returning values.

Uploaded by

igntu.hima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Function

C Functions

In c, we can divide a large program into the


basic building blocks known as function. The
function contains the set of programming
statements enclosed by { }.
A function can be called multiple times to
provide reusability and modularity to the C
program. In other words, we can say that the
collection of functions creates a program.
Types of Functions

There are two types of functions in C


programming:
• Library Functions: are the functions which are
declared in the C header files such as scanf(),
printf(), gets(), puts(), ceil(), floor() etc.
• User-defined functions: are the functions
which are created by the C programmer, so that
he/she can use it many times. It reduces the
complexity of a big program and optimizes the
code.
Syntax of a function

return_type function_name (argument list)


{
Set of statements – Block of code
}
return_type: Return type can be of any data
type such as int, double, char, void, short etc.
• function_name: It can be anything, however it is
advised to have a meaningful name for the
functions so that it would be easy to understand
the purpose of function just by seeing it’s name.
• argument list: Argument list contains variables
names along with their data types. These
arguments are kind of inputs for the function. For
example – A function which is used to add two
integer variables, will be having two integer
argument.
• Block of code: Set of C statements, which will be
executed whenever a call will be made to the
function.
Creating a user defined function addition()

#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);

You might also like