The document discusses formal and actual arguments in functions, call by value and call by reference methods of passing arguments, and use of header files. Formal arguments are parameters in a function declaration while actual arguments are the values passed in a function call. Call by value copies argument values while call by reference passes argument addresses allowing modification. Header files contain function prototypes and are included for library functions and custom functions.
The document discusses formal and actual arguments in functions, call by value and call by reference methods of passing arguments, and use of header files. Formal arguments are parameters in a function declaration while actual arguments are the values passed in a function call. Call by value copies argument values while call by reference passes argument addresses allowing modification. Header files contain function prototypes and are included for library functions and custom functions.
Call by Value • Call by value – In this method the values of actual arguments are copied to the formal arguments of the function. – Changes in function do not effect original – Use when function does not need to modify argument • Avoids accidental changes – The method of passing arguments by value is know as call by value
#include <stdio.h> int cubeByValue(int n); // prototype int main( void ) { int number = 5; // initialize number printf("The original value of number is %d", number); cubeByValue(number); // pass number by value printf( "\nThe new value of number is %d\n", number ); } // end main
int cubeByValue( int n )
return n*n*n; //cube local variable n and return value
} The original value of number is 5 The new value of number is 5
Call by reference • The address of actual argument are copied to the formal arguments. • The called function uses the address to refer to the actual location. • Changes made by the function are effective when the control returns to the calling function. – If we want to make changes even in the actual arguments, then we use call by address
#include <stdio.h> void cubeByReference(int *nPtr); //function prototype int main( void ) { int number = 5; printf("The original value of number is %d", number); cubeByReference( &number ); //pass address of number printf("\nThe new value of number is %d\n", number); } // end main
//calculate cube of *nPtr; actually modifies number in main
Header Files Standard library header Explanation <ctype.h> Contains function prototypes for functions that test characters for certain properties, and function prototypes for functions that can be used to convert lowercase letters to uppercase letters and vice versa. <errno.h> Defines macros that are useful for reporting error conditions. <float.h> Contains the floating point size limits of the system. <limits.h> Contains the integral size limits of the system. <math.h> Contains function prototypes for math library functions. <stddef.h> Contains common definitions of types used by C for performing certain calculations. <stdio.h> Contains function prototypes for the standard input/output library functions, and information used by them. <stdlib.h> Contains function prototypes for conversions of numbers to text and text to numbers, memory allocation, random numbers, and other utility functions. <string.h> Contains function prototypes for string processing functions.