In C programming language, a function is a self-contained block of code that performs a specific task. Functions allow you to break down your program into smaller, manageable pieces, making your code more organized, easier to understand, and reusable.
In C programming, functions can be categorized into several types based on their characteristics and usage Like Standard Library Functions, User-defined Functions,Recursive Functions,Function Pointers,Inline Functions,Static Functions,Callback Functions,Library Functions.
These are functions provided by the C standard library, which are commonly used for tasks like input/output operations (printf, scanf), memory management (malloc, free), string manipulation (strcpy, strlen), mathematical calculations (sin, cos), etc.
You don't need to define these functions yourself, just include the appropriate header file (e.g.,
These functions are created by the programmer to perform specific tasks within the program. They offer modularity, code reusability, and better organization.
#include <stdio.h> void show(int x) { printf("\n x= %d", x); } int main() { int a = 10; show(a); return 0; }
x = 10
#include <stdio.h> void show(int x); // Function Declaration //Function Prototype int main() { int a = 10; show(a); // Calling a Function return 0; } void show(int x) // Function defination { printf("\n x = %d", x); }
x= 10
#includevoid Simp_Int(); // Prototype // Declaration int main() { Simp_Int(); return 0; } void Simp_Int() { float p,r,t,si; printf("Enter Principal, Rate and Time :\n"); scanf("%f %f %f",&p,&r,&t); si = (p*r*t)/100; printf("\n Simple Interest = %f",si); }
Enter Principal, Rate and Time : 15 100 10 Simple Interest = 150.000000 -------------------------------- Process exited after 10.66 seconds with return value 0 Press any key to continue . . .
#includefloat Simple_Int(); int main() { float si; si = Simple_Int(); printf("\nSimple Interest = %f",si); return 0; } float Simple_Int() { float p,r,t,si; printf("Enter Principal, Rate and Time :"); scanf("%f %f %f",&p,&r,&t); si = (p*r*t)/100; return si; }
Enter Principal, Rate and Time : 15 100 10 Simple Interest = 150.000000 -------------------------------- Process exited after 10.66 seconds with return value 0 Press any key to continue . . .
#includevoid Simple_Int(float,float,float); int main() { float p,r,t; printf("Enter Principal, Rate and Time :"); scanf("%f %f %f",&p,&r,&t); Simple_Int(p,r,t); return 0; } void Simple_Int(float p, float r,float t) { float si; si = (p*r*t)/100; printf("\nSimple Interest = %f",si); }
Enter Principal, Rate and Time : 10 10 10 Simple Interest = 10.000000 -------------------------------- Process exited after 3.797 seconds with return value 0 Press any key to continue . . .
#includefloat Simple_Int(float,float,float); int main() { float p,r,t,si; printf("Enter Principal, Rate and Time :"); scanf("%f %f %f",&p,&r,&t); si=Simple_Int(p,r,t); printf("\nSimple Interest = %f",si); return 0; } float Simple_Int(float p, float r,float t) { float si; si = (p*r*t)/100; return si; }
Enter Principal, Rate and Time : 100 100 10 Simple Interest = 1000.000000 -------------------------------- Process exited after 6.021 seconds with return value 0 Press any key to continue . . .
#include// Function prototype can acept only type as well as variable declaration with in parameter. void add(int , int ); // only type void sub(int x, int y); // type with variable declaration void div(int,int); void mul(int, int); void mod(int, int); int main() { add(4,5); sub(4,5); div(8,2); mul(4,2); mod(5,2); } void add(int x ,int y) { printf("The sum is : %d\n",x+y); } void sub(int x, int y) { printf("The subtraction is : %d\n",x-y); } void div(int x, int y) { printf("The Division is : %d\n",x/y); } void mul(int x, int y) { printf("The multiplication is : %d\n",x*y); } void mod(int x, int y) { printf("The Remainder is : %d\n",x%y); }
The sum is : 9 The subtraction is : -1 The Division is : 4 The multiplication is : 8 The Remainder is : 1 -------------------------------- Process exited after 0.06449 seconds with return value 21 Press any key to continue . . .
#includevoid test(); int g; // Globle variable int main() { int x;//=8; // Local variable printf("%d\n",x); printf("%d\n",g); test(); } void test() { int x=5; //local variable printf("%d\n",x); printf("%d\n",g); }
0 0 5 0 -------------------------------- Process exited after 0.06643 seconds with return value 2 Press any key to continue . . .
#includevoid counter() { static int i=1; printf("%d\n",i); i++; } int main() { counter(); counter(); counter(); }
1 2 3 -------------------------------- Process exited after 0.04872 seconds with return value 4 Press any key to continue . . .
#include#include int main(void) { char *result, *string = "A Blue Danube"; char *chars = "ab"; result = strpbrk(string, chars); printf("The first occurrence of any of the characters \"%s\" in " "\"%s\" is \"%s\"\n", chars, string, result); }
The first occurrence of any of the characters "ab" in "A Blue Danube" is "anube" -------------------------------- Process exited after 0.04989 seconds with return value 81 Press any key to continue . . .