#Parameters for functions

32 messages · Page 1 of 1 (latest)

latent fable
#

If I am calling a function with parameters, can I directly pass in parameters without defining them ie. I have a function run(int x)

and I call run(1), am I allowed to do this? or do I have to define an int variable first, or does the compiler attempt to type cast.

small turretBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

strange granite
#

you are allowed

#

the way value parameters work is that x will copy what's put in the brackets to itself

#

for pointers this is different: you have to use temporary initialization (i think thats what its called) or take the address of another variable

#

;compile

#include <stdio.h>
int AddOne(int x) {
  return x + 1;
}

int main() {
  printf("%i\n", AddOne(5));
}```
quasi swiftBOT
#
Program Output
6
shell fulcrum
#

for pointers this is different
Not really, no.

You could just as well do:

#

;compile

#include <stdio.h>

void foo(int *ptr) { 
    printf("%p\n", ptr);
}

int main() {
    foo((int *) 0x40000000);
    foo((int[]) { 1, 2, 3 });
}
quasi swiftBOT
#
Program Output
0x40000000
0x7ffd4aa9a234
strange granite
#

0x40000000
what's that even point to

latent fable
#

hm, so i could do run(hi)

#

?

strange granite
shell fulcrum
clear dirge
#

e.g. I've got a device where setting a bit at address 0x00FF5E0218 will trigger a soft reset, so I've got this function:

void soft_reset(void) {
  (uint8_t*)0x00FF5E0218 |= 0x10;
}
strange granite
#

that's oddly specific

clear dirge
#

because that's what the manual says

strange granite
#

are you at least allowed to assign a const value to remidn yourself what's there

clear dirge
#

of course

strange granite
#

also what does (void) do

shell fulcrum
#

Or just a macro

clear dirge
#

it means no arguments

strange granite
#

there's no parameter there so what's the point

strange granite
clear dirge
#

that means unspecified arguments, not no arguments

shell fulcrum
#

;compile

void foo() { };
void bar(void) { };

int main() {
    foo(3, 2, 1);
    bar(1, 2, 3);
}
quasi swiftBOT
#
Compiler Output
<source>: In function 'main':
<source>:6:5: error: too many arguments to function 'bar'
    6 |     bar(1, 2, 3);
      |     ^~~
<source>:2:6: note: declared here
    2 | void bar(void) { };
      |      ^~~
Build failed
shell fulcrum
#

Note how it doesn't complain about the foo