About functions A function „ Is a way to group code into a unit „ Is code that can be called repeatedly „ ChiddilfCan hide details from t he ma in program Functions in „ E.g. A function call you have already used: ¾ printf(“Hello World\n”);

parameter parameter statements return value parameter Based on slides © McGraw-Hill Modified by Diana Palsetia

CIT 593 1 CIT 593 2

Motivation for Functions Function Syntax Function Break up a complex problem into simpler sub return-type method-name (parameters) problems, which you can solve separately {

„ E.g. Chocolate cake dessert statements Function ¾ Baking a cake & preparing the Icing Body } Write once and reuse Example 1: „ This is an application of the DRY principle (“Don’t double area(double radius) { Repeat Yourself) const int PI = 3.14; return radius * radius * PI; } Methods are also known as procedures, Example 2: , methods double average(int a, int b) { int c = (a + b) / 2.0; return c; }

CIT 593 3 CIT 593 4

1 Function Syntax (contd..) Calling Function When calling a function, the argument goes between the To execute instructions grouped in the method: parentheses () „ There may be multiple arguments „ We call the method by its name and provide any ¾ Each argument is variable of particular type inputs ¾ The arguments are use to do some work „ E. g. area(3. 5) „ There may be no arguments „ One function can all another function to do part of it ¾ Some old C indicated 0 parameters by keyword void with () E.g. int myFunction (void) work – modularity „ If the function is the same file, it can be simply called „ arguments are also called parameters „ If the function is another file, then we must include Functions usually have a (such as int) where the compiler can find its definition. E.g. „ They return a value of that type to the calling function #include ¾ Keyword return before the value to indicate that ¾ Usually the function prototype is written file ending in .h „ If no return value, a function is of type void and the return keyword is not required ¾ The function implementation (prototype + body) is written in file ending in .c

CIT 593 5 CIT 593 6

main Function Example Example 2: Example 1: //In circle.c Main is special function //In circle.c #include #include „ of C application #include #include „ Gets things started double area(double radius); //declare prototype double area((){double radius){ „ There can be several C files, but one of them must const double PI = 3.14; contain main int main(){ double area = radius * radius * PI; double radius = 0.0; return area; „ All functions are called from main printf(“Enter value for radius\n”); } scanf(“%lf”, &radius); „ return-type is int printf(“%lf”,area(radius)); int main(){ ¾ return 0: indicating successful termination return EXIT_SUCCESS; double radius = 0.0; } ¾ return non-zero: indicating unsuccessful termination printf(“Enter value for radius\n”); ¾ Can use macros EXIT_ SUCCESS an d EXIT_ FAILURE f rom scanf(“%lf”, &ra dius ); double area(double radius){ stdlib.h printf(“%lf”,area(radius)); const double PI = 3.14; return EXIT_SUCCESS; double area = radius * radius * PI; } return area; }

CIT 593 7 CIT 593 8

2 Local vs. Global Example Scope means the region of program in which an entity is #include known (or alive) int itsGlobal = 0; Not the same as itsLocal „ Mainly concerned with variables and methods int main(){ „ Which parts of the program can access them? int itsLocal = 1; /* local to main */ ¾ { } (block) & files define the region printf("Global %d Local %d\n", itsGlobal, itsLocal); { Variables int itsLocal = 2; /* local to this block */ itsGlobal = 4; /* change global variable */ „ Can be global, local or extern (Later) printf("Global %d Local %d\n", itsGlobal, itsLocal); } Methods printf("Global %d Local %d\n", itsGlobal, itsLocal); return 0; „ Are by default public } ¾ No keyword like public or private exist „ Can also be static Output: ¾ This is not the same as Java Global 0 Local 1 Global 4 Local 2 Global 4 Local 1 CIT 593 9 CIT 593 10

Pass by Value Arguments can be sent by reference By default, copies of arguments are sent to a function Call by reference sends a pointer to the function #include Ouput: void foo(int x); int main(){ In main: x = 0 The function can then point back to the original int x=0;x = 0; In foo: x = 0 printf("In main: x = %d\n",x); varibliable foo(x); In foo: x = 5 „ The one located in the calling function printf("In main: x = %d\n",x); In main: x = 0 „ The called function can follow the pointer to change } the original variable back in the calling function void foo(int x){ void foo(int * ptr) printf("In foo: x = %d\n",x); x = 5; foo(&x); printf("In foo: x = %d\n",x); „ More on pointer later }

CIT 593 11 CIT 593 12

3 Storage Class of a Variable Static Variable Example „ Indicates how the C compiler allocates storage //In static.c Output: #include „ Whether or not the variable loses its value when the nStatic = 0 block that contains it has completed execution void showstat(int curr); nStatic = 1 nStatic = 3 Two kinds: itint mai i(){n() { int i; nStatic = 6 Static for (i = 0; i < 5; i++ ){ „ Retain their values between invocations showstat(i); nStatic = 10 „ Global variables are static storage class } return 0; Automatic } „ Lose their values when the their block terminates voodsid sh ow sa(stat( int curr ){) { „ Local variables are automatic storage class by default static int nStatic = 0; „ Local variable can be made static by placing the keyword static nStatic = nStatic + curr; before the variable printf("nStatic = %d\n",nStatic); ¾ E.g. static int local }

CIT 593 13 CIT 593 14

Using static with functions Non- functions

#include Are functions that are not part of the C standard /* test is_alphanumeric function */ static int is_alphanumeric library (char c){ static int is_alphanumeric(char c); if (((int) c >= 48) && Ideally functions will be in separate file from the file int main(){ means “callable only in this file” ((int) c <= 57)) that contains main method return 1; char whichchar; Format printf(“Please enter a char: "); /* ask user */ if (((int) c >= 65) && .h file (header file) contains the function prototype scanf("%c", &whichchar); ((int) c <= 122)) .c file contains the complete function implementation if (is_alphanumeric(whichchar)) return 1; printf("%c is alphanumeric\n",whichchar); return 0; Calling function in a different file, need to include else } header file printf(" %c is NOT alphanumeric\n",whichchar); #include “circle.h ” return 0; ¾ Note this is different from standard library include e.g. } /* end main */

static functions are functions that are only visible to other functions in the same file

CIT 593 15 CIT 593 16

4 Evaluation Order special case in C

What does this do? void func(int x, int y) { printf(“%d %d\n”, x, y); } int main() { int x = 3; func(x, x++); … } Answer: undefined! „ Displays either “333 3” or “434 3” „ Why? C does not define order of evaluation in such a case „ Depends on compiler writer „ Suggestion: don’t modify variable in parameter list

CIT 593 17

5