Functions

34
Functions Septi Ratnasari 4101412082 Arinda Lailatul Karimah 4101412109 Presented by :

description

Fungsi dalam Pemrograman Komputer

Transcript of Functions

Page 1: Functions

Functions

Septi Ratnasari 4101412082

Arinda Lailatul Karimah4101412109

Presented by :

Page 2: Functions

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.

Page 3: Functions

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

Page 4: Functions

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.

Page 5: Functions

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;

Page 6: Functions

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.

Page 7: Functions

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.

Page 8: Functions

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.

Page 9: Functions

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.

Page 10: Functions

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.

Page 11: Functions

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;

Page 12: Functions
Page 13: Functions

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;

Page 14: Functions

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.

Page 15: Functions

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.

Page 16: Functions

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:

Page 17: Functions
Page 18: Functions

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.

Page 19: Functions

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.

Page 20: Functions

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.

Page 21: Functions
Page 22: Functions

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;

Page 23: Functions

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

Page 24: Functions

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.

Page 25: Functions
Page 26: Functions

Fungsi Add

Page 27: Functions

Fungsi Max-ab

Page 28: Functions

Fungsi Genap - Ganjil

Page 29: Functions

Fungsi Midpoint

Page 30: Functions

Fungsi Phytagoras

Page 31: Functions

Fungsi LuasSegitiga

Page 32: Functions

Fungsi LuasPersegi Panjang

Page 33: Functions

Fungsi VolumeBalok

Page 34: Functions