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. The function is also known as procedureor subroutinein other programming languages.
There are three aspects of a C function.
No | function aspects | syntax |
---|---|---|
1 | Function declaration | return_type function_name (argument list); |
2 | Function call | function_name (argument_list) |
3 | Function definition | return_type function_name (argument list) {function body;} |
The syntax of creating function in c language is given below:
return_type function_name(data_type parameter...){
//code to be executed
}
There are two types of functions in C programming:
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.
Let's see a simple example of C function that doesn't return any value from the function.
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.
Let's see a simple example of C function that returns int value from the function.
int get(){
return 10;
}
In the above example, we have to return 10 as a value, so the return type is int. If you want to return floating-point value (e.g., 10.2, 3.1, 54.5, etc), you need to use float as the return type of the method.
float get(){
return 10.2;
}
Now, you need to call the function, to get the value of the function.
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.
// Example for Function without argument and without return value
#include<stdio.h>
void printName();
int main ()
{
printf("Hello ");
printName();
}
void printName()
{
printf("hello world");
}
#include<stdio.h>
void sum();
int main()
{
printf("\nGoing to calculate the sum of two numbers:");
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();
int main()
{
int result;
printf("\nGoing to calculate the sum of two numbers:");
result = sum();
printf("%d",result);
}
int sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
return a+b;
}
// program to calculate the area of the square
#include<stdio.h>
int sum();
int square();
int 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);
int main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
Students Empire
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);
}
// program to calculate the average of five numbers.
#include<stdio.h>
void average(int, int, int, int, int);
int main()
{
int a,b,c,d,e;
printf("\nGoing to calculate the average of five numbers:");
printf("\nEnter five numbers:");
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
average(a,b,c,d,e);
}
void average(int a, int b, int c, int d, int e)
{
float avg;
avg = (a+b+c+d+e)/5;
printf("The average of given five numbers : %f",avg);
}
// Example for Function with argument and with return value
#include<stdio.h>
int sum(int, int);
int 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;
}
// Program to check whether a number is even or odd
#include<stdio.h>
int even_odd(int);
int main()
{
int n,flag=0;
printf("\nGoing to check whether a number is even or odd");
printf("\nEnter the number: ");
scanf("%d",&n);
flag = even_odd(n);
if(flag == 0)
{
printf("\nThe number is odd");
}
else
{
printf("\nThe number is even");
}
}
int even_odd(int n)
{
if(n%2 == 0)
{
return 1;
}
else
{
return 0;
}
}
No | Header File | Description |
---|---|---|
1 | stdio.h | This is a standard input/output header file. It contains all the library functions regarding standard input/output. |
2 | conio.h | This is a console input/output header file. |
3 | string.h | It contains all string related library functions like gets(), puts(),etc. |
4 | stdlib.h | This header file contains all the general library functions like malloc(), calloc(), exit(), etc. |
5 | math.h | This header file contains all the math operations related functions like sqrt(), pow(), etc. |
6 | time.h | This header file contains all the time-related functions. |
7 | ctype.h | This header file contains all character handling functions. |
8 | stdarg.h | Variable argument functions are defined in this header file. |
9 | signal.h | All the signal handling functions are defined in this header file. |
10 | setjmp.h | This file contains all the jump functions. |
11 | locale.h | This file contains locale functions. |
12 | errno.h | This file contains error handling functions. |
13 | assert.h | This file contains diagnostics functions. |