Void Function - Parameter

2 replies [Last post]
Harrison Cassar
Title: NooBot
Joined: 10/16/2013
BotPoints: 197
User offline. Last seen 8 years 15 weeks ago.

Hi this is Harrison. How exactly do you get your own function (that uses parameters) that you created to work? I have tried multiple times within many projects, but I haven't seemed to get it to work. In your reply, if you can, may you add an example program? With that, I will be able to better learn about this topic.

Thank you!

Also, the type that I am talking about is like this:

  1. void potato(int amount, int toppings, int cookingtime) {
  2. ...
  3. }

Jeremy Rand
Jeremy Rand's picture
Title: Botball Youth Advisory Council
Joined: 04/03/2009
BotPoints: 1168
User offline. Last seen 8 years 1 week ago.

Here's an example:

  1. int square(int x)
  2. {
  3. return x*x;
  4. }
  6. int main()
  7. {
  8. printf("%d squared is %d", 3, square(3)); // 3 squared is 9
  9. }

-Jeremy Rand
Senior Programmer, Team SNARC (2012-2013), Norman Advanced (2010-2011), Norman HS (2008-2009), Norman North (2005-2007), Whittier MS (2003-2004)
2012-2013 VP of Tech, 2011 President, Botball YAC (2009-2013)
Mentor, Alcott and Whittier MS

Title: NooBot
Joined: 10/28/2013
BotPoints: 286
User offline. Last seen 9 years 16 weeks ago.

And, keep in mind that you cannot change the value of the functions outside of the function declaration. To change the function outside of the function declaration from inside the body such as main, you would require the address of the variable and insert it via pointers to change it. Functions that return things use int instead of void. I would not suggest void potato(int amount, int toppings, int cookingtime) {
Instead, I would put: int potato(int amount, int toppings, int cookingtime) {
And, if the function returns things, use a return statement. Most likely, your program didn't work because you tried to return things with void. So, use int next time.

I know why you always use void for the functions. It is such a bad idea. :) The workshop slides never showed how to use int functions. So disappointed in how they teach.