Docsity

Understanding Functions and Function Calls in C Programming, Slides of Computer Science

Partial preview of the text

Download Understanding Functions and Function Calls in C Programming and more Slides Computer Science in PDF only on Docsity!

 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.

Main program

  • Function
  • function
  • function
  • function

 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

  • indicates that the function returns nothing ◦ Parameter-list: comma separated list, declares parameters^  A type must be listed explicitly for each parameter unless, theparameter is of type int

 Header files^ ◦ Contain function prototypes for library functions ◦ <stdlib.h> ,^ <math.h> , etc ◦ Load with #include #include <math.h>  Custom header files^ ◦ Create file with functions ◦ Save as filename.h ◦ Load in other files with #include "filename.h" ◦ Reuse functions

 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.