subprograms:
procedures ==> void function (no return statements)
function ==> non void function (return statement will be there)
keywords: function, procedure, is, begin, end, return, in, out
//C functions
#include <stdio.h> //printf() function declaration
int add(int a, int b); //function declaration - user defined function
int subtract(int a, int b);
int maximum (int x, int y);
void welcome(void);
void display(int x);
int main()
{
int a,b,r;
a = 10;
b = 20;
welcome();
display(a);
r = maximum(a,b); //function call - user defined function
printf("r=%d\n", r); //pre-defined function
return 0;
}
int add(int a, int b) //function definition - user defined function
{
int r;
r = a + b;
return r;
}
int subtract(int a, int b) //function definition - user defined function
{
int r;
r = a - b;
return r;
}
int maximum (int x, int y)
{
if(x>y)
{
return x;
}
else
{
return y;
}
}
void welcome(void)
{
printf("welcome to hyderabad!\n");
}
void display(int x)
{
printf("x is %d\n", x);
}
//ada functions
with Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Text_IO;
use Ada.Integer_Text_IO;
procedure Hello is
function add (a : integer; b : integer) return integer is
r : integer;
begin
r := a + b;
return r;
end add;
function subtract (a : integer; b : integer) return integer is
r : integer;
begin
r := a - b;
return r;
end subtract;
function maximum (x : integer; y : integer) return integer is
begin
if x>y then
return x;
else
return y;
end if;
end maximum;
procedure welcome is
begin
Put_Line("welcome to hyderabad!");
end welcome;
procedure display (x: integer) is
begin
Put("x is ");put(x);new_line;
end display;
a,b,r: integer;
begin
a := 10;
b := 20;
welcome;
display(a);
r := maximum(a,b);
Put("r=");put(r);new_line;
end Hello;
//function call in ada - positional association and named association
//parameter passing modes: in, out, in out
with Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Text_IO;
use Ada.Integer_Text_IO;
procedure Hello is
function maximum (x : integer; y : integer) return integer is
begin
if x>y then
return x;
else
return y;
end if;
end maximum;
a,b,r: integer;
begin
a := 10;
b := 20;
--r := maximum(a,b); //positional association
r := maximum(y => b, x => a); --named association
Put("r=");put(r);new_line;
end Hello;
//recursion
#include<stdio.h>
int factorial(int n);
int main(void)
{
int n,fact;
n=4;
fact = factorial(n);
printf("factorial of %d is %d\n", n, fact);
return 0;
}
int factorial(int n)
{
if(n==0 || n==1)
return 1;
else
return n*factorial(n-1);
}
/*
int factorial(int n) //iteration
{
int i, f=1;
for(i=n; i>0; i--)
{
f = f*i;
}
return f;
}
*/
//ada
with Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Text_IO;
use Ada.Integer_Text_IO;
procedure Hello is
n : integer;
fact : integer := 1;
begin
n := 4;
for count in reverse 1 .. n loop
fact := fact * count;
end loop;
put("factorial of ");put(n);put(" is ");put(fact);
end Hello;
//iteration - ada
with Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Text_IO;
use Ada.Integer_Text_IO;
procedure Hello is
function factorial (n: in integer) return integer is
f : integer := 1;
begin
for count in reverse 1 .. n loop
f := f * count;
end loop;
return f;
end factorial;
n : integer;
fact : integer;
begin
n := 5;
fact := factorial(n);
put("factorial of ");put(n);put(" is ");put(fact);
end Hello;
//recursion
with Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Text_IO;
use Ada.Integer_Text_IO;
procedure Hello is
function factorial (n: in integer) return integer is
begin
if n=0 or n=1 then
return 1;
else
return n*factorial(n-1);
end if;
end factorial;
n : integer;
fact : integer;
begin
n := 5;
fact := factorial(n);
put("factorial of ");put(n);put(" is ");put(fact);
end Hello;