Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
Sudeshna Sarkar, IIT Kharagpur 2 Class Test on 7 th February Thursday 5:30 to 6:30 pm
Sudeshna Sarkar, IIT Kharagpur 3 A Problem Suppose we are writing a program that displays messages on the screen. We want to display rows of =============================== to separate sections of output.
Sudeshna Sarkar, IIT Kharagpur 4 Solution #include int main (void) { /* produce some output */ /* print banner line */ printf(“==============\n”) ; /* produce more output */ printf(“==============\n”); /* produce even more output */ printf(“==============\n”); /* produce final output */ }
Sudeshna Sarkar, IIT Kharagpur 5 Critique Redundant code What if we want to change the display e.g. to print a blank line before and after each line with =’s ? What if we want to print banner lines in some other program?
Sudeshna Sarkar, IIT Kharagpur 6 The Solution: Functions Definition: A function is a named code sequence A function can be executed by using its name as a statement or expression. The function may have parameters - information that can be different each time the function is executed. The function may compute and return a value.
Sudeshna Sarkar, IIT Kharagpur 7 Why use functions ? Functions provide an abstraction when writing a program - allow low-level details to be packaged. Able to package a computation we need to perform at multiple spots within a program. Write once, use many times. If changes are needed, they only have to be done once, in one place.
Sudeshna Sarkar, IIT Kharagpur 8 Why use functions ? Allows the programmer to create a program from smaller, simpler sub-components. Many programs are far too large to understand all at once. Functions give us a way to break a large program into smaller pieces A function should perform a well-defined task.
Sudeshna Sarkar, IIT Kharagpur 9 Common functions int main (void) {... } Function definition for main() printf (“%d + %d is %d\n”,x,y,z); scanf (“%d%d”, &x, &y) ; limit = sqrt ((double) num); Function calls
Sudeshna Sarkar, IIT Kharagpur 10 More common functions Every standard C compiler comes with a set of standard libraries. #include Tells the compiler you will use the standard IO library #include C’s standard math library functions: sqrt, pow, sin, cos, exp,... isspace,...
Sudeshna Sarkar, IIT Kharagpur 11 Defining your own functions You may define your own functions in your programs You define a function by giving its name and writing the code that is executed when the function is called.
Sudeshna Sarkar, IIT Kharagpur 12 High level programming structure main () {... SetUpBoard (); do { redsTurn (); blacksTurn (); } while (gamenotover ()); }
Sudeshna Sarkar, IIT Kharagpur 13 Function definition /* print banner line */ void print_banner (void) { printf (“=========”); printf (“=========\n”); } heading comment function name function body (statements to be executed) A function can have ANY number of ANY kind of statements
Sudeshna Sarkar, IIT Kharagpur 14 /* print banner line */ void print_banner (void) { printf (“=========”); printf (“=========\n”); } void void has two different roles in this function definition. indicates that the function does not return (have) an output value. indicates that the function has no parameters.
Sudeshna Sarkar, IIT Kharagpur 15 Calling a function int main (void) { /* produce some output */... print_banner (); /* produce more output */... print_banner (); /* produce final output */... print_banner (); } To execute the function, it is called or invoked from within a program or another function. Note: A function that does not return a value can be called wherever a statement is allowed.
Sudeshna Sarkar, IIT Kharagpur 16 Terminology main () is the caller print_banner() is the callee. main() invokes / calls print_banner() 3 times. print_banner() is called from main()
Sudeshna Sarkar, IIT Kharagpur 17 Function Control Flow /* print banner line */ void print_banner (void) { printf(“**************\n”); } int main (void) {... print_banner ();... print_banner (); return (0); } main (void) { print_banner (); print_banner (); } print_banner { } print_banner { }
Sudeshna Sarkar, IIT Kharagpur 18 Control Flow All C programs Start at main () /*no matter where main()is */ Continue in top-to-bottom order, statement by statement, unless the order is changed by: function call function return if loops
Sudeshna Sarkar, IIT Kharagpur 19 Function Type and Value A function can return a value. Like all values in C, a function return value has a type. The function has the type of its returned value. /* Get number from user */ int get_input (void) { int num; printf (“Enter a number:”); scanf (“%d”, &num); return (num); } function type return statement returned value
Sudeshna Sarkar, IIT Kharagpur 20 Calling a function A value-returning function is called by including it in an expression. int main (void) { int x, y; x = get_input() ; y = get_input() ; printf (“ %d + %d = %d\n”, x, y, x+y); return (0); } Note : A value returning function can be used anywhere an expression of the same type can be used.
Sudeshna Sarkar, IIT Kharagpur 21 return In a value-returning function (result type is not void) return does two distinct things : 1. specify the value returned by the execution of the function 2. terminate that execution of the function. In a void function: return is optional at the end of the function body. return may also be used to terminate execution of the function explicitly. No return value should appear following return.
Sudeshna Sarkar, IIT Kharagpur 22 void compute_and_print_itax () { double income; scanf (“%f”, &income); if (income < 50000){ printf (“Income tax = Nil\n”); return; } if (income < 60000){ printf (“Income tax = %f\n”, 0.1*(income-50000); return; } if (income < ) { printf (“Income tax = %f\n”, 0.2*(income-60000)+1000); return ; } printf (“Income tax = %f\n”, 0.3*(income )+19000); } Terminate function execution before reaching the end
Sudeshna Sarkar, IIT Kharagpur 23 Function parameters It is very often useful if a function can operate on different data values each time it is called. Such values are function (input) parameters. The function specifies its inputs as parameters in the function declaration. /* Find area of a circle with radius r */ double area (double r) { return ( *r*r) ; } parameter
Sudeshna Sarkar, IIT Kharagpur 24 Arguments The function call must include a matching argument for each parameter. When the function is executed, the value of the argument is substituted for the parameter. int main (void) {... double circum;... area1 = area(circum/2.0);... } double area (double r) { return (3.14*r*r); } parameter passing