Algorithm & programming part IV

Hhmm……., in this part we’re gonna talk about procedure and function. Procedure and function is used to make program can be easier to be read and understood. Not only that, procedure and function can be used to make program easier to be analyzed weather there is mistake or not. So, when there is long codes of program, it can be separated into small parts as procedure or function. And, procedure and function will make you easy to find out error and fix it (error handling). Well, actually there are differences between procedure and function. Procedure doesn’t has return value. However, function has it. Not only that, pascal has procedure and function. But, C doesn’t have it . On the other side, function in C able to creat return value or not. Depend on the type of function. Pascal has parameter for procedure and function and C is either.

Below is some examples the use of function and procedure by parameter or not

example program without parameter :

PASCAL

program table;
uses crt;

procedure appearance;
begin
writeln('this program is made for procedure without parameter');
writeln('this program only contains of appearance and main program');
writeln('this program will use two procedures');
writeln('this 1st procedure for apperance ');
writeln('the 2nd procedure for main program ');
writeln('start ');
writeln('press ENTER ');
readln;
end;

procedure main;
var
a:byte;
begin
for a:=1 to 10 do
writeln('ACHMAD FAUZI');
end;

var
a:byte;
begin
appearance;
main;
end.

C

#include <stdio.h>
#include <string.h>

void appearance();
void primary();

int main(){
appearance();
primary();
}
void appearance(){
printf("this program is made for procedure without parameter\n");
printf("this program only contains of appearance and main program\n");
printf("this program will use two procedures\n");
printf("this 1st procedure for apperance \n");
printf("the 2nd procedure for main program\n");
printf("start\n");
printf("press ENTER \n");
getchar();
}
void primary(){
int a;
for (a=1;a<=10;a++)
printf("ACHMAD FAUZI\n");
}

example program by using input parameter :

Pascal

PASCAL
program sum;
uses crt;
var
a:integer;

procedure add(x:integer);
var
b,c,d:integer;
begin
d:=0;
 for b:=1 to x do
 begin
 write('index[',b,']=');read(c);
 d:=d+c;
 end;
writeln('total = ',d);
end;

begin
write('please input a number =');read(a);
add(a);
end.

C

#include<stdio.h>

void out();
int main(){
out();
}

void out(){
int a,b,c,d;
printf("input a number = ");scanf("%d",&a);
d=0;
 for (b=1;b<=a;b++){
 printf("index[%d] :",b);scanf("%d",&c);
 d+=c;
 }
printf("total = %d\n",d);
};

example program by using output parameter :

PASCAL

program average_by_output_parameter;
uses crt;
var
b,c,d,e:integer;
a:real;
procedure ave(var a:real);
begin
e:=0;
write('input a number = ');read(b);
 for c:=1 to b do
 begin
 write('index[',c,']=');read(d);
 e:=e+d;
 end;
a:=e/b;
end;

begin
ave(a);
writeln('average = ',a:2:2);
end.


C

#include<stdio.h>

int out (int b,int d);


void main(){
int b,d;
float a;
a=out (b,d);
printf("average = %2.2f\n",a);
}


int out (int b,int d){
int c,e;
float a;
e=0;

printf("input a number  =");scanf("%d",&b);
 for (c=1;c<=b;c++){
 printf("index[%d] = ",c);scanf("%d",&d);
 e+=d;
 }
a=e/b;
return (a);
}


example program by using input and output parameter :

PASCAL

program average_by_output_and_output_parameter;
uses crt;
var
b:integer;
a:real;

procedure ave(b:integer ;var a:real);
var
c,d,e:integer;
begin
e:=0;
 for c:=1 to b do
 begin
 write('index[',c,']=');read(d);
 e:=e+d;
 end;
a:=e/b;
end;

begin
write('input a number = ');read(b);
ave(b,a);
writeln('average = ',a:2:2);
end.

C

#include <stdio.h>

int jalan(int a);

void main(){
int a;
float e;
printf("input a number = ");scanf("%d",&a);
e=jalan(a);
printf("average = %2.2f\n",e);
}

int jalan(int a){
int b,c,d;
float e;
d=0;
 for (b=1;b<=a;b++){
 printf("index[%d]",b);scanf("%d",&c);
 d+=c;
 }
e=d/a;
return (e);
} 10.c


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: