Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fungsi Risanuri Hidayat, Ir., M.Sc.. Functions C usually consist of two things: instance variables and functions. All C programs consist of one or more.

Similar presentations


Presentation on theme: "Fungsi Risanuri Hidayat, Ir., M.Sc.. Functions C usually consist of two things: instance variables and functions. All C programs consist of one or more."— Presentation transcript:

1 Fungsi Risanuri Hidayat, Ir., M.Sc.

2 Functions C usually consist of two things: instance variables and functions. All C programs consist of one or more functions. Functions are the building blocks of a program. All functions are at the same level — there is no nesting. One (and only one) function must be called main.

3 general form This is the general form of a function: type name(parameter-list) { // body of function return value; } type specifies the type of data returned by the function. If the function does not return a value, its return type must be void. The name of the function is specified by name. The parameter-list is a sequence of type and identifier pairs separated by commas. Parameters are essentially variables that receive the value of the arguments passed to the function when it is called. value is the value returned.

4 return All functions can return a value, including main. Functions can return arithmetic values ( int, float etc.), structures, unions, pointers or void. If the return type is specified as being void, then no value is returned by the function. Functions cannot return a function or an array.

5 Parameters All functions (including main) can accept parameters. void volume(double x, double y, double z) { printf(“%f \n”, x*y*z); } The parameter list must be specified, and their types must be declared. The parameters to the function (the expressions given in the function call) are passed by value only. If,the parameter list contains the single word void, then the function does not take any parameters.

6 arguments It is common to call the variables specified in the function definition parameters and the expressions given in a function call arguments. For example, in the following call of volume, the expressions 10, 20, and 15 are the arguments to the function. The values of the two expressions will be copied into the parameters x, y, and z. Sometimes the terms formal argument and actual argument are used instead; the formal argument being the variable given in the function definition, the actual argument being the expression given in the function call. volume(10, 20, 15);

7 Example // Metode example (Metode.c). #include void volume(double x, double y, double z) { printf(" %lf \n",x*y*z); } main() { double vol; double width = 10; double height = 20; double depth = 15; printf("Volume is "); volume(width, height, depth); }

8 Example 2: Returning value // (metode01.c) #include double volume(double x, double y, double z) { double v; v =x*y*z; return v; } main() { double vol; double width = 10; double height = 20; double depth = 15; vol = volume(width, height, depth); printf("Volume is "); printf(" %lf \n",vol); }

9 Function overloading C tidak mendukung Function overloading

10 Type Conversion When a function is called, C looks for a match between the arguments used to call the function and the function's parameters. However, this match may not always be exact. In some cases C can automatically convert the type of arguments to the type of parameters.

11 Type Conversion // Demonstrate function overloading (overload01.c). #include double test(double a) { printf("double a: %lf \n", a); return a*a; } main() { int x=10; double result; // call all versions of test() result = test(x); printf("Result of test(123.2): %lf \n", result); } //main

12 recursion C supports recursion. Recursion is the process of defining something in terms of itself. recursion is the attribute that allows a function to call itself. A function that calls itself is said to be recursive

13 recursion // Demo recursion (recursion01.c) #include int fact(int n) { int result; if(n==1) return 1; result = fact(n-1) * n; return result; } main() { printf("Factorial of 3 is %d \n", fact(3)); printf("Factorial of 4 is %d \n", fact(4)); printf("Factorial of 5 is %d \n", fact(5)); } //main

14 Standard Header Standard Header Files Prototypes of the library functions are given in several standard header files. For example, stdio.h contains prototypes for printf, scanf, putchar and getchar. Other standard header files are: –assert.h assertions –ctype.h character class tests –float.h system limits for floating point types –limits.h system limits for integral types –math.h mathematical functions –setjmp.h non-local jumps –signal.h signals and error handling

15 Standard Header –stdarg.h variable length parameter lists –stdlib.h utility functions; number conversions, memory allocation, exit and system, Quick Sort –string.h string functions –time.h date and time functions To include these standard header files in your code, use the preprocessor directive #include and place angle brackets around the name of the file, e.g. #include


Download ppt "Fungsi Risanuri Hidayat, Ir., M.Sc.. Functions C usually consist of two things: instance variables and functions. All C programs consist of one or more."

Similar presentations


Ads by Google