Mubashir Farooq 1405( BScs ) GOVT Municipal Degree College FSD Functions in C
Definition A Function is a self-contained block of statement that perform a task of some kind. OR Function is a set of instruction to carryout a particular task. Function after its execution returns a single value when we call it in our program. Functions in C
1.Built-in-Functions The built-in-function are also called library function or Standard Function. These functions are provided by programmer. printf (); and scanf (); are Some common types of built-in-Functions. 2.User-Defined Fun . Defined by user at the time of writing the program. User set these program for her own uses. Add(); Max(); etc are some kinds of user-defined functions. Functions in C
Need of functions Enabling code reuse Better readability Improved debugging and testing Improved maintainability Reduce lines of code Functions in C
Steps of User-Defined-F. There are three steps of working with user Defined functions: Function Declaration, also known as function prototype Function Dentition means body of function. Function use, also known as function call. Functions in C
Programs. Simple program by Function :- #include< stdio.h > void massage(); /*Function prototype declaration*/ void main() { void massage(); /*function call*/ } void massage() /*function definition*/ { printf (“This is a simple program"); } start Function call Void massag Output end Out put :- This is simple program
To find area and circumference of circle . #include< stdio.h > #define PI 3.14159 float carea (float); float ccir (float); void main() { float radius; printf ("Enter floater Radius:"); scanf ("% f",&radius ); carea (radius); ccir (radius); getch (); } float carea (float radius) { float area; area=PI*radius*radius; printf ("Area= %f\ n",area );} float ccir (float radius) { float ccir ; cir=2*PI*radius; printf ("Circumference= % f",cir );} Flo. Programs. start Input radius Function call Float carea,crim area=PI*radius*radius cir=2*PI* radius Output Area,Cri end Enter floater Radius _5.0 Area=78.53975 Circumference=31.4159
To Exchange Values of 2 variables. include< stdio.h > void swap( int *x, int *y); void main() { int x, y; printf ("\ nEnter First number : "); scanf ("%d", &x); printf ("\ nEnter Second number : "); scanf ("%d", &y); printf ("\ nBefore Swaping x = %d and y = %d", x, y); swap(&x, &y); printf ("\ nAfter Swaping x = %d and y = %d", x, y); getch ();} void swap( int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp;} Programs. start Input x,y Output Values of x and y end Function call Void swap Temp=*x *x=*y Y=*temp Enter First number_4 Enter Second number_8 Before Swaping x=4 and y=8 After Swaping x=8 and y=4