Fungsi - Gadjah Mada University

Download Report

Transcript Fungsi - Gadjah Mada University

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 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.

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.

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.

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.

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);

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);

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);

Function overloading

C tidak mendukung Function overloading

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.

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

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

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

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 ctype.h float.h limits.h math.h setjmp.h signal.h assertions character class tests system limits for floating point types system limits for integral types mathematical functions non-local jumps signals and error handling

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