1 TOPICS TO DISCUSS : FUNCTIONS TYPES OF FUNCTIONS HEADER FILES PRESENTED BY : AVISHEK MAJUMDAR(837837) GUNJAN AGARWAL(856587) SATYAPRIYA DEY(856624)
2 FUNCTION C Functions are a set of instructions enclosed by “{ }” which performs specific operations in a C program. The advantage of using function is to avoid rewriting of of same code and logic. There are 3 aspects in each C function: 1.Function declaration or prototype 2.Function call 3.Function definition
3 #include float sum ( float x,float y ); // Function declaration int main( ) // main function { float a,b, s; printf ( "\n Enter two numbers” \n"); scanf ( "%f%f", &a,&b ); s=add(a,b); // Function call printf ( "\nThe Sum of two numbers are %f ",s ); } float sum ( float x,float y ) // Function definition { float p; p = x +y; return p; } EXAMPLE OF FUNCTIONS
4 CONTINUED.... CALL BY VALUE The value of the variable is passed to the function. float square ( float x ); int main( ) { float m, n; printf ( "\n Enter a number for finding the square \n"); scanf ( "%f", &m ); n = square ( m ); printf ( "\n Square of the given number %f is %f",m,n ); } float square ( float x ) { float p; p = x * x; return p; } CALL BY REFERENCE The address of the variable is passed to the function void swap (int *x, int *y); int main( ) { int num1 = 5, num2 = 6; swap ( &num1, &num2 ); // function call printf ( "\n After swapping the values are %d and %d”, num1, num2 ); } void swap ( int *x, int *y ) { int temp; temp = *x; *x = *y; *y = temp;}
5 TYPES OF FUNCTION Functions are basically of two types: 1.Library Functions 2.User Defined Function LIBRARY FUNCTION: Library functions are the in-built functions in C Programming Language. Examples: main(), printf(), scanf()
6 TYPES OF User Defined FUNCTION Function can basically be categorized as: Function with arguments and return type- declaration:float calculate_area(int); definition: float calculate_area(int radius){ float area_of_circle; area_of_circle=3.14*radius*radius; return area_of_circle;} Function with arguments and without return type- declaration:void calculate_area(int); definition: void calculate_area(int radius){ float area_of_circle; area_of_circle=3.14*radius*radius; printf(“Area of the circle of radius-%d is-%d”,radius,area); }
7 Function without arguments and without return type - declaration:void calculate_area(); definition: void calculate_area(){ float area_of_circle,radius; printf(“Enter the value of radius”); scanf(“%f”,&radius); area_of_circle=3.14*radius*radius; printf(“Area of the circle of radius-%d is-%d”,radius,area);} Function without arguments and with return type - declaration:int calculate_area(); definition: int calculate_area(){ float area_of_circle,radius; printf(“Enter the value of radius”); scanf(“%f”,&radius); area_of_circle=3.14*radius*radius; return area_of_circle; }
8 printf() :formatted write scanf() :formatted read gets() :read string puts() :write string #include int main() { int number; printf("Enter an integer\n"); scanf("%d", &number); printf("Integer that you have entered is %d\n", number); return 0; } Header file is a file,containing C declarations and macro definitions to be shared between several source files. To use the header files in a program we have to include it,with the C preprocessing directive ‘#include’. A header file have extension.h. stdio.h What is Header files..
9 Header files serve two purposes. System header files declare the interfaces to parts of the operating system. I have to include them in program to supply the definitions and declarations. My own header files contain declarations for interfaces between the source files of programs. Each time I have a group of related declarations and macro definitions all or most of which are needed in several different source files, it is a good idea to create a header file for them.
10 Header files used in C.. stdio.h: Include all the input output function stdlib.h: include all the library function math.h : Include all the mathematical functions string.h: Include all string manipulation functions ctype.h: Include all character manipulating functions conio.h: Include all console input output functions stddef.h: Standard type definitions Time.h: Contains the C date and time operations
11