CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Silly parameters function problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ark2040



Joined: 22 Jul 2014
Posts: 3
Location: N/A

View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger

Silly parameters function problem
PostPosted: Tue Jul 22, 2014 3:16 pm     Reply with quote

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: 1907

View user's profile Send private message

PostPosted: Tue Jul 22, 2014 3:24 pm     Reply with quote

Either

Code:
int8 z;

z = x * y;

return z;


Or

Code:
return x * y;


First method will definitely work; 2nd may make the compiler complain.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jul 22, 2014 3:27 pm     Reply with quote

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: 9210
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Jul 22, 2014 3:30 pm     Reply with quote

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

View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger

Thk U
PostPosted: Tue Jul 22, 2014 4:52 pm     Reply with quote

newguy wrote:
Either

Code:
int8 z;

z = x * y;

return z;


Or

Code:
return x * y;


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 Sad
Ark2040



Joined: 22 Jul 2014
Posts: 3
Location: N/A

View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger

PostPosted: Tue Jul 22, 2014 5:55 pm     Reply with quote

PCM programmer wrote:
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


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

View user's profile Send private message

PostPosted: Wed Jul 23, 2014 8:20 am     Reply with quote

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...
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group