




























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























Divide and conquer^ ◦ Construct a program from smaller pieces or components^ These smaller pieces are called modules ◦ Each piece more manageable than the original program
Math library functions^ ◦ perform common mathematical calculations ◦ #include <math.h> Format for calling functions^ ◦ FunctionName( argument ); If multiple arguments, use comma-separated list ◦ printf( "%.2f", sqrt( 900.0 ) );^ Calls function sqrt , which returns the square root of its argument All math functions return data type double ◦ Arguments may be constants, variables, or expressions
Functions^ ◦ Modularize a program ◦ All variables declared inside functions are local variables^ Known only in function defined ◦ Parameters^ Communicate information between functions Local variables Benefits of functions^ ◦ Divide and conquer^ Manageable program development ◦ Software reusability^ Use existing functions as building blocks for new programs Abstraction - hide internal details (library functions) ◦ Avoid code repetition
These functions are also called as 'library functions'.These functions are provided by system. Thesefunctions are stored in library files. e.g. scanf() printf() strcpy strlwr strcmp strlen strcat
User define function: This function as to be developed by the user at the time of writing a program. Eg: main() ‘main’ is specially used function in C. Ever program must have main function to indicate, where the program begins itsexecution. When a program to large and complex then the result ofdebugging testing and maintaining becomes difficult.
The main functions and other library functions doesneed to be declared and defined but the main function’sbody need to be defined by the programmer.
In C user- written functions should normally bedeclared prior to its use to allow compiler to performtype checking on arguments used in its call statement. The general form is: Retirn_data_type function_name (data_type Var_name,…..);
Function name: this is the name given to the function.It follows the same naming convention as that of anyvalid variable in C. Return data type: this specifies the type of data givenback to the calling construct. Data type list: this list specifies the data type of eachvariables, the values of which are expected to betransmitted to the function. These variables are knownas formal parameters.
Function definition format return-value-type function-name( parameter-list ) { declarations and statements } ◦ Function-name: any valid identifier ◦ Return-value-type: data type of the result (default int ) void
Header files^ ◦ Contain function prototypes for library functions ◦ <stdlib.h> ,^ <math.h> , etc ◦ Load with #include
The general form of calling stmt is: Function_name (var1, var2,..); Or var_name=function name(var1, var2,..);
The func name and the type and number of argumentsmust match with that of the function declaration stmtand the header of the function definition. Arguments present in the form of expression areevaluated and converted to the type of formalparameters at the beginning of the body of the function.