### Thread: function problem

1. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2003
Posts
18
Rep Power
0

#### function problem

i have some problem with the function of my codes..anybody can help me to solve this??
------------------------------------
Code:
```#include <stdio.h>
#include <conio.h>
int area1(int,int,int);
double area2(double,double,double);
main()
{
int choice,a,b,square;
double x,y,triangle;
clrscr();
do
{
printf("Calculate an area of : \n1. Square\n2. Triangle\n3. Circle\n");
printf("Please give your choice [1/2/3] : ");
scanf("%d",&choice);
if(choice>=4)
{
printf("Should provide 1-3 options value.\n");
}
}while(choice>=4);
if(choice==1)
{
area1(a,b,square);
}
else                                          
if(choice==2)
{
area2(x,y,triangle);
}
getche();
}

int area1(int a,int b,int square)
{
printf("Enter height and base : ");
scanf("%d%d",&a,&b);
square=a*b;      
printf("The area of triagle is %d",square);
return(area);
}

double area2(double x,double y,double triangle)
{
printf("Enter height and base : ");
scanf("%d%d",&x,&y);
triangle=0.5*x*y;
printf("The area of triagle is %d",triangle);
return(area);
}```
--------------------------------
[edited by Onslaught to add code tags for easier viewing the code]
Last edited by Onslaught; July 11th, 2003 at 09:07 AM.
2. you have to tell us the problem to be helped. what is the compiler error, what line did it occur on? one thing i noticed is you say:
Code:
`return(area);`
yet i see no variable named area in those functions. Did you mean to say return(square) or return(triangle) as those variables seem to hold what your looking for.
3. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2003
Posts
18
Rep Power
0
the problem there written
possible using definition 'a' before defintion'
like that

and also for b, x,y,square and triangle and area.

u can copy my code and then run it
4. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2003
Posts
2
Rep Power
0
i dont know if my solution works, cause i dont have a compiler, but i hope this will help you :-)

your return types of your functions don't fit together.....

first of all:
you declered your function like this:
int choice,a,b,square;
double x,y,triangle;
but you executed them like that:
area1(a,b,square);
area2(x,y,triangle);
it should be like this:
int area = area1(a,b,square)
...
but theres another problem

the parameters of your function arent initiated....

the right code would be:
[/quote]
..... //main function
if(choice==1)
{
printf("Square of area1 = %d", area1());
}
.......//main

int area1()
{
printf("Enter height and base : ");
scanf("%d%d",&a,&b);
return a*b;
}
[/quote]
5. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2003
Posts
18
Rep Power
0
ok man..i changed everything to simple,let start with simple 1.
but problem still occur, ' undefined symbol 'a' '.
----------------
#include <stdio.h>
#include <conio.h>
int area1(int,int);
main()
{
int choice;
clrscr();
do
{
printf("Calculate an area of : \n1. Square\n2. Triangle\n3. Circle\n");
printf("Please give your choice [1/2/3] : ");
scanf("%d",&choice);
if(choice>=4)
{
printf("Should provide 1-3 options value.\n");
}
}while(choice>=4);
if(choice==1)
{
printf("The area of triagle is %d",area1(a,b));
}
getche();
}

int area1(int a,int b)
{
printf("Enter height and base : ");
scanf("%d%d",&a,&b);
return a*b;
}
------------------

6. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2003
Posts
18
Rep Power
0
hei man, i changed everything again to this.declare the int inside the local declaration.
make everything become this.
-----------------------------------
#include <stdio.h>
#include <conio.h>
int area1();
main()
{
int choice;
clrscr();
do
{
printf("Calculate an area of : \n1. Square\n2. Triangle\n3. Circle\n");
printf("Please give your choice [1/2/3] : ");
scanf("%d",&choice);
if(choice>=4)
{
printf("Should provide 1-3 options value.\n");
}
}while(choice>=4);
if(choice==1)
{
printf("The area of triagle is %d",area1());
}
else
if(choice==2)
{

}
getche();
}

int area1()
{
int a,b;
printf("Enter height and base : ");
scanf("%d%d",&a,&b);
return a*b;
}
------------------------
hopefully no problem for this codes, everything run smoothly, this make me advancing a step more to proceed the triangle and circle area.

7. In the future please use an appropriate subject title. You can refer to the sticky thread at the top of this forum for more information on how to post a question.
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2003
Posts
21
Rep Power
0
Code:
```#include <stdio.h>
#include <conio.h>

int area1();
double area2();

void main()
{
int choice;

do{
clrscr();
printf("1. Square\n");
printf("2. Triangle\n");
printf("3. Circle\n");
printf("0. Exit\n");
printf("Choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("\nSquare = %d",area1());
break;
case 2:
printf("\nTriangle %f",area2());
break;
case 0:
exit(1);break;
default:
printf("\nProvide useful choice");
}
getch();
}while(choice!=0);
}

int area1()
{
int a, b;
printf("\nter height and base : ");
scanf("%d%d",&a,&b);
return(a*b);
}

double area2()
{
double x, y;
printf("\nEnter height and base : ");
scanf("%d%d",&x,&y);
return(0.5*(x*y));
}```
Last edited by DJ911; July 12th, 2003 at 06:24 AM.
9. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2003
Posts
18
Rep Power
0
hm..
for c programming, why not declaration of function make inside braces 1?

should be

area1(int a,int b)

bla bla bla

like that....
according to my univers's books, it should be like that.

10. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2003
Posts
21
Rep Power
0
hey Jinkhai
i just write an example code.
it is not universal convension
11. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2003
Posts
18
Rep Power
0
but if i put area1(int a,int b)
{}

the whole things cannot run at all..
12. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2002
Posts
272
Rep Power
22
I think the point is that neither area1 nor area2 need to have parameters passed in to them. Local variables will work just fine in this case so you can just not bother with the parameters. It's even a tiny bit faster.
13. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2003
Posts
18
Rep Power
0
hm..seems this is a better way..but my skool just teach all the **** things with declaration inside the braces. area1(int a, int b)

thanx a lot man..
gonna finish this project soon..next week monday gonna pass up to tutor.

regards,
khai