View previous topic :: View next topic |
Author |
Message |
Ark2040
Joined: 22 Jul 2014 Posts: 3 Location: N/A
|
Silly parameters function problem |
Posted: Tue Jul 22, 2014 3:16 pm |
|
|
Hi guys, I would like to do any like this code, but I dont know why doesnt work ?
Code: |
#include <16f877.h>
#fuses HS,NOWDT,NOPROTECT
#use delay (clock=4000000)
#byte PORTA=0x05
#byte PORTB=0x06
#byte PORTC=0x07
#byte PORTD=0x08
int z;
void main()
{
//set_tris_a(0b00011111);
set_tris_b(0b11111111);
set_tris_c(0b11111111);
set_tris_d(0b00000000);
function(5,2);
PORTD=z;
}
void function(int x, int y){ // IT MUST RECOGNIZE NUMBERS WRITTEN UP
z=x*y;
return
} |
|
|
|
newguy
Joined: 24 Jun 2004 Posts: 1911
|
|
Posted: Tue Jul 22, 2014 3:24 pm |
|
|
Either
Code: | int8 z;
z = x * y;
return z;
|
Or
First method will definitely work; 2nd may make the compiler complain. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jul 22, 2014 3:27 pm |
|
|
This is a basic C question. You need to study this topic off the forum.
Google for it:
Quote: | How to return a value from a function in C |
Look at some of the hits that you get. Study how they do it.
http://www.macs.hw.ac.uk/~pjbk/pathways/cpp1/node162.html |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9270 Location: Greensville,Ontario
|
|
Posted: Tue Jul 22, 2014 3:30 pm |
|
|
if making a function, then it can't be 'void', must be an int as well...
it's covered in the help files...or see any number of the example programs CCS kindly supplies...
hth
jay |
|
|
Ark2040
Joined: 22 Jul 2014 Posts: 3 Location: N/A
|
Thk U |
Posted: Tue Jul 22, 2014 4:52 pm |
|
|
newguy wrote: | Either
Code: | int8 z;
z = x * y;
return z;
|
Or
First method will definitely work; 2nd may make the compiler complain. |
Yes, I knew that Newguy, but I'm trying to write inside parentheses to obtain a result so ... I can not to do that |
|
|
Ark2040
Joined: 22 Jul 2014 Posts: 3 Location: N/A
|
|
Posted: Tue Jul 22, 2014 5:55 pm |
|
|
HEY PCM PROGRAMMER I GOT IT THKS U
#include <16f877.h>
#fuses HS,NOWDT,NOPROTECT
#use delay (clock=4000000)
#byte PORTA=0x05
#byte PORTB=0x06
#byte PORTC=0x07
#byte PORTD=0x08
int8 z;
void function(int8 one, int8 two)
{
z=one+two;
}
void main()
{
//set_tris_a(0b00011111);
set_tris_b(0b11111111);
set_tris_c(0b11111111);
set_tris_d(0b00000000);
function(2,3);
PORTD=z;
} |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Jul 23, 2014 8:20 am |
|
|
It works but is very ugly code.
General rule is: Use as few global variables as possible.
In a small program like this it isn't a big problem but in a larger problem you will loose the overview of all the places where a variable is used and changed.
Why do you not want to have the function return a value? Quote: | but I'm trying to write inside parentheses to obtain a result so ... I can not to do that | This suggests your teacher wanted you to learn about macro's and that's not what you have created here... |
|
|
|