FUNCTIONS IN C
PART-1
Definition
• 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.
Advantage of functions in C
• There are the following advantages of C functions.
• By using functions, we can avoid rewriting same
logic/code again and again in a program.
• We can call C functions any number of times in a
program and from any place in a program.
• We can track a large C program easily when it is
divided into multiple functions.
• Reusability is the main achievement of C
functions.
Function Aspects
There are three aspects of a C function.
• Function declaration A function must be declared
globally in a c program to tell the compiler about the
function name, function parameters, and return type.
• Function call Function can be called from anywhere
in the program. The parameter list must not differ in
function calling and function declaration. We must
pass the same number of functions as it is declared in
the function declaration.
• Function definition It contains the actual statements
which are to be executed. It is the most important
aspect to which the control comes when the function
is called.
SN C function aspects Syntax
return_type function_name
1 Function declaration
(argument list);
2 Function call function_name(argument_list)
return_type function_name
(argument list)
3 Function definition {
function body;
}
Types of Functions
Return Value
• A C function may or may not return a value
from the function. If you don't have to return
any value from the function, use void for the
return type.
• EX:
void hello()
{
printf("hello c");
}
• If you want to return any value from the
function, you need to use any data type such
as int, long, char, etc. The return type depends
on the value to be returned from the function.
Example with return value:
int get(){
return 10;
}
E.X:
float get(){
return 10.2;
}
Different aspects of function calling
• A function may or may not accept any argument.
It may or may not return any value. Based on
these facts, There are four different aspects of
function calls.
function without arguments and without return
value
function without arguments and with return
value
function with arguments and without return
value
function with arguments and with return value
Example for Function without argument and
return value
#include<stdio.h>
void show();
void main ()
{
printf("Hello ");
show();
}
void show()
{
printf(“Hello World");
}
Example 2
#include<stdio.h>
void sum();
void main()
{
sum();
}
void sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
printf("The sum is %d",a+b);
}
Example for Function without argument and with
return value
#include<stdio.h>
int sum();
void main()
{
int result;
result = sum();
printf("%d",result);
}
int sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
return a+b;
}
Example 2: program to calculate the area of the square
#include<stdio.h>
int sum();
void main()
{
printf("Going to calculate the area of the square\n");
float area = square();
printf("The area of the square: %f\n",area);
}
int square()
{
float side;
printf("Enter the length of the side in meters: ");
scanf("%f",&side);
return side * side;
Example for Function with argument and without
return value
#include<stdio.h>
void sum(int, int);
void main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
sum(a,b);
}
void sum(int a, int b)
{
printf("\nThe sum is %d",a+b);
}
Example for Function with argument and with return
value
#include<stdio.h>
int sum(int, int);
void main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
result = sum(a,b);
printf("\nThe sum is : %d",result);
}
int sum(int a, int b)
{
return a+b;
}