What is a Function in C Programming?
C Function: Function is a collection of statement that perform a specific task. It executes when it is called by its name. A large program is divided into a number of small building block for simplicity and this building block is called function.
We can call a function again and again. The most important features of function is code reusability. The C library provides many predefined functions.
Best Functions C Language| Calling Function With Examples |
C Programming Function Syntax
return_type function_name(parameter List) |
C Programming Function Description
1. Return Type: It is a keyword which indicates that which type of value is being returning by the function. If we do not to return any value then we use void keyword in place of return_type. 2. Function Name: It is the actual name of the function. it is also used at the time of calling the function. 3. Parameter List: It is the place where we can pass a number of parameter/variables may be used in the program. The value of the parameter can be initialized, or we can pass it from the calling of the function. It is an optional part. 4. Body: It is the place where the actual code is written to perform the specific task. |
Predefined Functions in C Programming
Built-in Function: The function which is predefined in the library is called predefined function.
Example: printf(),scanf(),clrscr(),getch() are predefined function.
User Defined Functions in C Programming
Key Point About the C Function
void add() { int x=10; int y=30; int z=x+y; printf("Addition:%d",z); } |
This is a function definition and here we can see that code is written to perform the addition task.
Function Calling: At this stage the function is called. add();
To call a function just write function just write function name and put semicolon(;) after it.
Function Complete Example in C
#include<stdio.h> #include<conio.h> void add() { int x=10; int y=30; int z=x+y; printf("Addition:%d",z); } void main() { clrscr(); add(); getch(); } **********OUTPUT********** Addition: 40 |
How Many Types of Function are There in C.
There are four categories of user defined function. 1. Function with no return type and no parameter. 2. Function with no return type and with parameter. 3. Function with return type and no parameter. 4. Function with return type and with parameter. |
Function with No Return type and No Parameter Example
#include<stdio.h> #include<conio.h> void add() { int x=10; int y=30; int z=x+y; printf("Addition:%d",z); } void main() { clrscr(); add(); getch(); } **********OUTPUT********** Addition: 40 |
Function with no return type and with parameter
#include<stdio.h> #include<conio.h> void add(int x,int y) { int z; int z=x+y; printf("Addition:%d",z); } void main() { clrscr(); add(20,30); getch(); } **********OUTPUT********** Addition: 50 |
Function with return type and no parameter
#include<stdio.h> #include<conio.h> int add() { int x=10; int y=30; int z=x+y; return z;
} void main() { clrscr(); int res=add(); printf("Addition:%d",res); getch(); } **********OUTPUT********** Addition: 40 |
Function with return type and with parameter
#include<stdio.h> #include<conio.h> int add (int x,int y) { int z; int z=x+y; return z; } void main() { clrscr(); int res=add(30,20); printf("Addition:%d",res); getch(); } **********OUTPUT********** Addition: 50 |
Calling a Function in C Programming
#include<stdio.h> #include<conio.h> void add(int x) { x=x+100; } void main() { clrscr(); int y=10; printf("Before calling y:%d\n",y); add(y); printf("After calling y:%d",y); getch(); } **********OUTPUT********** Before calling x:110 After calling x:110 |
#include<stdio.h> #include<conio.h> void add(int *x) { *x=*+10; } void main() { clrscr(); int y=10; printf("Before calling y:%d\n",y); add(&y); printf("After calling y:%d",y); getch(); } **********OUTPUT********** Before calling x:10 After calling x:20 |
Defining Python Functions With Default and Optional Arguments
Function Complete Example in C
#include<stdio.h> #include<conio.h> void sum(int x=10,int y=20) { int res=x+y; printf("Addition:%d",res); } void main() { clrscr(); int x=10,y=20; printf("Without Value\n"); sum(); printf("With Value\n"); sum(5,8); getch(); } **********OUTPUT********** Without Value Addition: 30 With Value Addition: 40 |
What Happens when an Array is Passed to a Function
#include<stdio.h> #include<conio.h> void sum(int ar[5]) { int s=0 for(int i=0;i<5;i++) s=s+ar[i]; printf("Total Sum of Array Element:%d",s); } void main() { clrscr(); int x[5]={10,20,50,40,60}; sum(x); getch(); } **********OUTPUT********** Total Sum of Array Element:180 |
Multiplication Function with Recursion
#include<stdio.h> #include<conio.h> void factorial(int n,int f) { if(n>=1) { f=f*n; n--; factorial(n,f) } else { printf("Factorial=%d",f); } void main() { clrscr(); int x; printf("Enter Any Number:"); scanf("%d",&x); factorial(x,1); getch(); } **********OUTPUT********** Enter Any Number: 6 Factorial=720 |
0 Comments