Functions

Post on 25-May-2015

29 views 0 download

Tags:

description

Fungsi dalam Pemrograman Komputer

transcript

Functions

Septi Ratnasari 4101412082

Arinda Lailatul Karimah4101412109

Presented by :

Description

A function is a group of statements that together perform a task.

Every Pascal program has at least one function which is the program itself, and all the most trivial programs can define additional functions.

A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.

A function is similar to a procedure.

Procedures accept data or variables when they are executed. Functions also accept data, but have the ability to return a value to the procedure or program which requests it.

Functions are used to perform mathematical tasks like factorial calculations.

A function :

1. begins with the keyword function

2. is similar in structure to a procedure

3. somewhere inside the code associated with the function, a value is assigned to the function name

4. a function is used on the righth and side of an expression

5. can only return a simple data type

The only difference from the procedure is that the function return a value at the end. Note that a procedure cannot return a value. A function start and end in a similar way to that of a procedure.

If more than one value is required to be returned by a module, we should make use of the variable parameter. A function can have parameters too. If we change the sub-program from procedure to a function, of the previous program, there will be no difference in the output of the program.

Just make sure which one is best when we can to implement a module. For example, if we don't need to return any values, a procedure is more best. However if a value should be returned after the module is executed, function should be used instead.

In Pascal, a function is defined using the function keyword. The general form of a function definition is as follows:

function name(argument(s): type1; argument(s): type2; ...): function_type;

local declarations;

begin

...

< statements >

...

name:= expression;

end;

A function definition in Pascal consists of a function header, local declarations and a function body. The function header consists of the keyword function and a name given to the function.

Here are all the parts of a function:

1. Arguments:

The argument(s) establish the linkage between the calling program and the function identifiers and also called the formal parameters.

A parameter is like a placeholder. When a function is invoked, we pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Use of such formal parameters is optional. These parameters may have standard data type, user-defined data type or subrange data type.

The formal parameters list appearing in the function statement could be simple or subscripted variables, arrays or structured variables, or subprograms.

2. Return Type:

All functions must return a value, so all functions must be assigned a type.

The function-type is the data type of the value the function returns. It may be standard, user-defined scalar or subrange type but it cannot be structured type.

3. Local declarations:

local declarations refer to the declarations for labels, constants, variables, functions and procedures, which are application to the body of function only.

4. Function Body:

The function body contains a collection of statements that define what the function does. It should always be enclosed between the reserved words begin and end. It is the part of a function where all computations are done.

There must be an assignment statement of the type - name := expression; in the function body that assigns a value to the function name. This value is returned as and when the function is executed. The last statement in the body must be an end statement.

Following is an example showing how to define a function in pascal:

(* function returning the max between two numbers *)

function max(num1, num2: integer): integer;

var

(* local variable declaration *)

result: integer;

begin

if (num1 > num2) then

result := num1

else

result := num2;

max := result;

end;

Function Declarations

A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately.

A function declaration has the following parts:

function name(argument(s): type1; argument(s): type2; ...): function_type;

For the above defined function max(), following is the function declaration:

function max(num1, num2: integer): integer;

Function declaration is required when we define a function in one source file and we call that function in another file. In such case we should declare the function at the top of the file calling the function.

Calling a Function While creating a function, we give a definition of

what the function has to do.

To use a function, we will have to call that function to perform the defined task. When a program calls a function, program control is transferred to the called function.

A called function performs defined task and when its return statement is executed or when it last end statement is reached, it returns program control back to the main program.

To call a function we simply need to pass the required parameters along with function name and if function returns a value then we can store returned value. Following is a simple example to show the usage:

Assigning a value to a function identifier

The function's block definition must include a statement that assigns a value to the function's identifier. This is how a function gets a value that it can return to the caller.

If we omit this assignment statement, or the assignment statement does not get executed as a result of conditional statements in the function's code, then the function returns an undefined or potentially random value. If during the course of debugging a program we find our functions returning erratic values, be certain that the function identifier is correctly assigned a value.

Acceptable Function Return Values

The data type that a function returns can be any of the following:

a. Any ordinal value, including Boolean, Byte, Char, Smallint, Integer, Word, Longint, and enumarated data types and user defined sub range types. 

b. Real, Single, Double, Extended and Comp data types,

c. Pointer values

d. Strings.

Functions may not return records or sets, although they may return pointers to records or sets.

Recursive Function

Functions may call themselves. Such a function is called a recursive function. A popular and simple example of a recursive function is a function that computes the factorial of a number. The factorial of a number n, is n * (n-1) * (n-2) ... until n reaches 1.

Important note: The effect of short-circuit evaluation on functions

By default, Turbo Pascal generates short-circuit evaluation code, so it is possible that a function may not be called within a particular expression. For example, consider a function defined as:

function ValueInRange ( X1 : Integer ) : Boolean;

begin

...

if X1 > 0 then

ValueInRange := True

else

ValueInRange := False;

if X1 < LowestCoordinate then

LowestCoordinate := X1;

end;

In this function, a global variable LowestCoordinate may have its value changed during the course of execution. If this function is called in an expression such as,

if (X1<>X2) and ValueInRange(X1) then ...

Function as Parameter A function may itself be passed to another function

as a parameter value. To pass a requires that a type declaration define a procedure type that matches the appropriate function header. This type becomes the parameter type used in the procedure parameter list.

Fungsi Add

Fungsi Max-ab

Fungsi Genap - Ganjil

Fungsi Midpoint

Fungsi Phytagoras

Fungsi LuasSegitiga

Fungsi LuasPersegi Panjang

Fungsi VolumeBalok