28
Sigma institute of engineering SUBJECT: COMPUTER PROGRAMMING AND UTILIZATION PREPARED BY : group c ENROLLMENT NO:- NAME OF STUDENT 1. 140500119041 KANADE MAHENDRA 2. 140500119052 MAKWANA BHAVESH 3. 140500119054 MEHUL JHALA ASSIGNED BY ; PROF.SURESH CHAUDHARY

functions

Embed Size (px)

Citation preview

1. Sigma institute of engineering SUBJECT: COMPUTER PROGRAMMING AND UTILIZATION PREPARED BY : group c ENROLLMENT NO:- NAME OF STUDENT 1. 140500119041 KANADE MAHENDRA 2. 140500119052 MAKWANA BHAVESH 3. 140500119054 MEHUL JHALA ASSIGNED BY ; PROF.SURESH CHAUDHARY 2. Topic : functions 3. CONTENTS: Function Function example Vedio on functions Benefits of function Math library functions function prototype Function arguments C++ variables Global variable External variables Other variables Recursion Recursion example 4. Functions A function is a self contained block of code that performs a particular task. Any C program can be seen as a collection/group of these functions. A functions takes some data as input, perform some operation on that data and then return a value. Any C program must contain at least one function, which is main(). There is no limit on the number of functions that might be present in a C program. 5. For ex. main() { message(); printf(I am in main); } message() { printf(I am in message); } 6. Video on function 7. 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 8. 5.3Math Library Functions Math library functions perform common mathematical calculations #include 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 9. Method Description Example ceil( x ) rounds x to the smallest integer not less than x ceil( 9.2 ) is 10.0 ceil( -9.8 ) is -9.0 cos( x ) trigonometric cosine of x (x in radians) cos( 0.0 ) is 1.0 exp( x ) exponential function ex exp( 1.0 ) is 2.71828 exp( 2.0 ) is 7.38906 fabs( x ) absolute value of x fabs( 5.1 ) is 5.1 fabs( 0.0 ) is 0.0 fabs( -8.76 ) is 8.76 floor( x ) rounds x to the largest integer not greater than x floor( 9.2 ) is 9.0 floor( -9.8 ) is -10.0 fmod( x, y ) remainder of x/y as a floating- point number fmod( 13.657, 2.333 ) is 1.992 log( x ) natural logarithm of x (base e) log( 2.718282 ) is 1.0 log( 7.389056 ) is 2.0 log10( x ) logarithm of x (base 10) log10( 10.0 ) is 1.0 log10( 100.0 ) is 2.0 pow( x, y ) x raised to power y (xy) pow( 2, 7 ) is 128 pow( 9, .5 ) is 3 sin( x ) trigonometric sine of x (x in radians) sin( 0.0 ) is 0 sqrt( x ) square root of x sqrt( 900.0 ) is 30.0 sqrt( 9.0 ) is 3.0 tan( x ) trigonometric tangent of x (x in radians) tan( 0.0 ) is 0 Fig. 3.2 Math library functions. Math Library Functions Revisited 10. 5.2Program Modules in C Functions Modules in C Programs combine user-defined functions with library functions C standard library has a wide variety of functions Function calls Invoking functions Provide function name and arguments (data) Function performs operations or manipulations Function returns results Function call analogy: Boss asks worker to complete task Worker gets information, does task, returns result Information hiding: boss does not know details 11. Function Prototype All Identifiers in C must be declared before they are used. This is true for functions as well as variables. For functions, the declarations needs to be done before the first call of the function. A function declaration specifies the name, return type, and arguments of a function. This is also called the function prototype. To be a prototype, a function declaration must establish types for the functions arguments. Having the prototype available before the first use of the function allows the compiler to check that the correct number and types of arguments are used in the function call. 12. The prototype has the same syntax as the function definition, except that it is terminated by a semicolon following the closing parenthesis and therefore has no body. Although, functions that return int values do not require prototypes, prototypes are recommended. 13. Function Definition General form of any function definition is: return-type function-name(argument declarations) { declarations and statements } Return-type refers to the data type of the value being returned from the function. If the return type is omitted, int is assumed. The values provided to a function for processing are the arguments. The set of statements between the braces is called as the function body. 14. Arguments Call by Value In C, all functions are passed by value by default. This means that the called function is given the values of its arguments in temporary variables rather than the originals. For ex. main() { int a=4,b=5; sum(a,b); printf(Sum = %d,a+b); } sum(int a,int b) { a++; b++; printf(Sum = %d,a+b); } 15. Arguments Call by Reference When necessary, it is possible to arrange a function which can modify a variable in a calling routine. The caller must provide the address of the variable to be set (generally, a pointer to a variable), and the called function must declare the parameter to be a pointer and access the variable indirectly through it. For ex: main() { int a=4,b=5; sum(&a,&b); printf(Sum = %d,a+b); } sum(int *a,int *b) { (*a)++; (*b)++; printf(Sum = %d,(*a)+(*b)); } 16. C++ Variables A variable is a place in memory that has A name or identifier (e.g. income, taxes, etc.) A data type (e.g. int, double, char, etc.) A size (number of bytes) A scope (the part of the program code that can use it) Global variables all functions can see it and using it Local variables only the function that declare local variables see and use these variables A life time (the duration of its existence) Global variables can live as long as the program is executed Local variables are lived only when the functions that define these variables are executed 16 17. Local Variables Local variables are declared inside the function body and exist as long as the function is running and destroyed when the function exit You have to initialize the local variable before using it If a function defines a local variable and there was a global variable with the same name, the function uses its local variable instead of using the global variable 17 18. Example of Defining and Using Global and Local Variables #include int x; // Global variable Void fun(); // function signature void main() { x = 4; fun(); cout