|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
Math.h and Functions and Variables
Let me know if this function is right:
This is the 'pointer' MyFunction() /************************************************** ***************************** * FUNCTION NAME: * PURPOSE: * CALLED FROM: this file * ARGUMENTS: none * RETURNS: void ************************************************** *****************************/ void MyFunction(void) { } Also, in the User routines.h, I added MyFunction(void) to the very end. Is that needed? Next, do my variables always need to be predefined with int x, double x, etc. Are variables always open to the whole .c file or just the function? Do i need to put #include<Math.h>in the top? Man, I wish we could get back to the ye olde days of BASIC. No predefinition required, etc. Last edited by amateurrobotguy : 25-02-2005 at 20:44. |
|
#2
|
||||
|
||||
|
Re: Math.h and Functions and Variables
I think that it's a little late to learn C...
But you have the basics down. Here are the answers to your questions: Quote:
Quote:
Variables and functions always require the type. Generally, you use int, unless you run out of space in a file. Basically, variables MUST be declared right after the brackets. if there are any statements that are not variables then it will give syntax errors. Code:
int function(void) {
int var1=2;
if (var1==2) var1=3;
int var2=var1+3; // <-- Syntax error points here, because this cannot be after statements.
Code:
int function(void) {
int var1=2;
int var2;
if (var1==2) var1=3;
var2=var1+3; // <-- Syntax error points here, because this cannot be after statements.
Code:
// user_routines.h
extern int motor1_speed;
extern int motor2_speed;
// user_routines.c
...
int motor1_speed=0;
int motor2_speed=0;
...
void function(void) {
// motor1_speed can be accessed anywhere...
motor1_speed=127;
motor2_speed=127;
}
void function2(void) {
if (motor1_speed>7||motor1_speed<-7) {
int pwmval=motor1_speed+127; // Legal after any brackets.
pwm01=pwmval;
}
if (motor2_speed>7||motor2_speed<-7) {
int pwmval=motor2_speed+127; // Legal after any brackets.
pwm02=pwmval;
}
}
Quote:
Quote:
Just look at ifi_startup.c! It's all written in "inline assembly". Have fun. You technically don't need predefintion. The compiler figures it out usually. I prefer C a lot more. It's a lot more portable. I cam compile the code for the PC for example and use the RoboEmu to simulate a robot. This is not the type of thing I want to even attempt with PBASIC. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Use of Functions in Autonomous | VideoMan053 | Programming | 11 | 17-02-2005 16:27 |
| Updated: Serial Port Driver Code | Kevin Watson | Programming | 4 | 05-02-2005 18:39 |
| Overloaded functions | jgannon | Programming | 5 | 31-12-2004 11:04 |
| Do you write functions for your code? | Max Lobovsky | Programming | 26 | 11-03-2004 07:04 |
| math.h library | mightywombat | Programming | 24 | 17-01-2004 13:10 |