Saturday, November 14, 2009

Function Turbo C++

Function is a block in the Turbo C + +
General form:
name_function type (parameter declarations)
(
statements;
statements;
(
type
Type the value produced by function. If not stated, the result is considered an integer function (int)
parameter declaration
List the type and variable names that will receive the value when the function is called. Each parameter is separated by a comma. If the function has no parameter list will be empty. So it only parentheses.
Declaration of parameters rather different with variable declarations. In the variable declarations, you can declare a type for some variables the same type. Example:
int a, b, c;
But in your parameter declaration must declare each type of parameter.
General form:
f (name_var1 type, type name_var2, ..., type name_varn);
Example: f (int a, int b, int c);

Return statement
Return statement said two things:
  • Return to end the course of function and return to the caller program
  • Produces a value

There are two ways to return to the caller program:
  •  At the last statement of the function found (common sign of the end function "}")
  Example:
main()
{
clrscr();
hai();
}
hai()
{
printf(”Hai.. Apa kabar..?”);
getch();
}
  • By using the return statement. Return statement can also be used without producing a value.
  Example: 
 main()
{
clrscr();
hai();
}
hai()
{
printf(”Hai.. Apa kabar..?”);
return;
printf(”\nBaik-baik saja kannn..?”);
getch();
}

 Return statement in the above program will end the function O () so that the program does not work under

Function O () on the above programs can only print Hai .. How are you ..?. If we want the function to print a different message, we must turn it into as follows:
main()
{
clrscr();
hai(”Haloo…!”);
hai(”Siapa nama kamu..?”);
}
hai(char pesan[20])
{
printf(”%s \n”,pesan);
getch();
}

In the above example the calling function  () uses the argument, namely Haloo ...! and What is the name you ..?. In the function  () must have a variable that receive these arguments. In this case the message (called parameters). Parameter properties as local variables in the function. Type parameters must match the type of argument.

To produce a value, you must declare and return the resulting value.
Example:
 main()
{
int a,b,c;
clrscr();
printf(”Masukkan nilai A : “); scanf(”%d”,&a);
printf(”Masukkan nilai B : “); scanf(”%d”,&b);
c=jumlah(a,b);
printf(”\n\nJumlah kedua bilangan tersebut adalah %4d”,c);
getch();
}

jumlah(int x, int y)
{
return x+y;
}

Function number () to the program above has 2 parameters are:
• the number of (a, b)
• the number (int x, int y)

The contents of variables a and b are sent to the function number () and accepted by the x and y. His name is not necessarily the same, but the type must be the same. In this case all of type int.

Consider the program:
c = sum (a, b)
return x + y

Function number () yield x + y and the results incorporated into the variable c. The important thing, the recipient variables and outcome should function the same type. If the type is not stated explicitly, the function produces a value of type int.

For example we will change the function of a float, then there are two ways to consider:
1. Function should be explicitly type
2. Before the function called for the first time, the compiler must be told the type of function.

How to declare a function with a type, similar to variable declarations, the type name precedes the function.
Example:
float jumlah(float x, float y);
main()
{
float a,b,c;
clrscr();
printf(”Masukkan nilai A : “); scanf(”%f”,&a);
printf(”Masukkan nilai B : “); scanf(”%f”,&b);
c=jumlah(a,b);
printf(”\n\nJumlah kedua bilangan tersebut adalah %4.1f”,c);
getch();
}

float jumlah(float x, float y)
{
return x+y;
}

Prototype FUNCTION
Declare function prototype 2 things:
1. The type of value generated function, so the compiler can generate the correct code for the type of value generated
2. Type and number of arguments used by function.

General form:
Type name_function (parameter list)

Usually placed in the initial prototype program, and must be found before the function is called for the first time. Consider row 1 of the above programs. The statement is a function prototype. Call function number () is in the program c = sum (a, b), a prototype was before calling.

WITH TYPE FUNCTION void
Function that does not produce value can be declared as type void. This can prevent accidental errors.
Example:
 void hai();
main()
{
clrscr();
hai();
getch();
}

void hai()
{
printf(”Hai… Apa kabar?”);
}

 If you do not declare the type of void, Turbo C + + considers the function produces an integer value.

LOCAL VARIABLES
Variables that are declared inside the function is called local variables. On the Turbo C + +, a variable is local to a block, so it is more narrow than a function. Actually, a function is a block and the block may have a smaller blocks again.

Local variables can only be used in the block where the variable is declared. A block begins with a (and end with a). Local variables created at the time of the block where the variable declaration is executed. If the block is completed, the local variable is discarded.

Example:
 #include
float akar (float x);
main()
{
float a,b;
clrscr();
printf(”Masukkan sebuah bilangan : “); scanf(”%f”,&a);
b=akar(a);
printf(”\n\nAkar dari bilangan tersebut adalah %6.2f”,b);
getch();
}float akar(float x)
{
float y;
if (x>=0)
{
y=sqrt(x);
return y;
}
else return 0;
}

GLOBAL VARIABLES
Global variable is known in every part of the program. Global variable to store the value in it, as long as the program starts. Global variable declared outside any function.

Example:
#include
int a;
void f1(void);
main()
{
clrscr();
printf(”Masukkan nilai A adalah “); scanf(”%d”,&a);
f1();
printf(”\n\nNilai A sesudah pemanggilan function adalah %3d”,a);
getch();
}void f1(void)
{
printf(”\n\nNilai A di dalam function sebelum dijumlah adalah %3d”,a);
a+=5;
printf(”\n\nNilai A di dalam function sesudah dijumlah adalah %3d”,a);
}

note row 2, a variable declared before the function main (). Actually, global variables may be placed at each place, before the first pemakainnya. Global variable should be placed at the beginning of the program.

Recursion
Functions in C Language can be used in a recursion, in the sense of a function can call itself. Recursion is rarely used, among others due to:
• Making it difficult to understand the function
• Only suitable for specific issues
• Requires a stack with a larger size.

Example:
#include
int faktorial(int);
main()
{
clrscr();
int x;
puts(”Mencari Nilai Faktorial”);
printf(”Masukkan sebuah bilangan bulat positif : “);
scanf(”%d”,&x);
printf(”Nilai faktorial dari %d = %d\n”, x, faktorial (x));
getch();
}int faktorial(int m)
{
if (m==1)
return(1);
else
return(m*faktorial(m-1));
}

Operator
C + + is a language rich in operators. Not everything to do with math, but pretty much related to it. Basic operators that you need to know is: +, -, *, /.

No comments:

Post a Comment

Post a Comment

 
THANK YOU FOR VISITING