Table of Contents
Function in C Programming Language
In this tutorial, we will discuss of the Function in C Programming Language
C functions
In the tutorial, We learn how to use functions in C language. generally, the function is a block of statement that together perform one of the specific tasks in C language similar to any programming languages etc java(called methods), C++(called methods)
Syntex in the function of C programming
return_type: The return_type is a data type of the value returned by the function. when use void data type, it never returns any value but any data type return the value eg – int
function_name:funtion_name helps to identify the function in C Programming. Every function should be a name because the function_name represent the unique function and function_name use to call the function to need
parameter list: a function may contain a parameter and some functions may contain no parameter (it is optional) however when a funtion is invoked, pass a value to the parameter of the parameter list refer to the parameter type, parameter order, and number of parameters of the function.
Body of the function – the body of the function consist of group of statements that will be invoked when this function is called. Because a function contains a set of statements of for the unique purpose. we can call any function to my need of a group of the function of the program
Two type of function available in C Programming.
- Standard library function – pre-drfine function – Already define and included compiler in C programming
- uder defined function – a function define the user to need
4 type of function available in C Language
- No return without argument
- No return with argument
- Return without argument
- Retunn with argment
1. Add two numbers using the function (No return without argument)
Program 1
#include <stdio.h>
#include <stdlib.h>
void main()
{
add();
return 0;
}
void add()
{
int a,b,c;
printf(“n Enter first number”);
scanf(“%d”,&a);
printf(“n Enter second number”);
scanf(“%d”,&b);
c=a+b;
printf(“Total value of c:%d”,c);
}
2. Different two numbers using function (No return with argument)
#include <stdio.h> #include <stdlib.h> void main() { sub();//function call return 0; } void sub(int a,int b) { printf("Enter first numbern"); scanf("%d",&a); printf("Enter second numbern"); scanf("%d",&b); int c; c=a-b; printf("Total value of c:%d",c); }
When the above code is executed it producesthe following result
3 multiply two numbers using the function (Return without argument)
#include <stdio.h> #include <stdlib.h> int main() { printf("mul total: %d",mul()); return 0; } int mul() { int a,b,c; printf("Enter first numbern"); scanf("%d",&a); printf("Enter second numbern"); scanf("%d",&b); c=a*b; return(c); }
When the above code is executed it producesthe following result
4 add two numbers using function (Return with argument)
#include <stdio.h> #include <stdlib.h> int main() { int result; result=add(100,50); printf("the result is :%d",result); return 0; } int add(int a,int b){ int temp; temp=a+b; return temp; }
When the above code is executed it producesthe following result