14 Replies - 37297 Views - Last Post: 29 January 2006 - 07:22 PM Rate Topic: -----

#1 Kcbroncofan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 55
  • Joined: 28-September 05

void function

Post icon  Posted 25 January 2006 - 03:52 PM

can anyone help me with void functions. What are they? How do they work? and how to write them.

This post has been edited by Kcbroncofan: 29 January 2006 - 07:02 PM

Is This A Good Question/Topic? 0
  • +

Replies To: void function

#2 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4478
  • View blog
  • Posts: 28,395
  • Joined: 21-March 01

Re: void function

Posted 25 January 2006 - 04:02 PM

a void function is just a function that does not require a return type, it's declared as void functionname() {

you might need to clarify your question a little bit if that isn't the information you're looking for.
Was This Post Helpful? 0
  • +
  • -

#4 Voodoo Doll  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 108
  • Joined: 24-January 06

Re: void function

Posted 25 January 2006 - 04:36 PM

A void function could be one of two things...three if you want to mix and match. First, a void function could be a selfish little bugger that takes but never gives back. He'll take your arguments with a smile, but the only time you see him again is when he comes back to tell you he's done.
#include <iostream>

// Takes but never gives back
void selfish(int number)
{
  std::cout << number << '\n';
}

int main()
{
  // I'm giving him a number
  selfish(5);
  // But all I get back in return is the
  // flow of execution. What a meanie!
}


You use this kind of function when all you're interested in is the side effect but still need to give it some raw materials. The side effect is whatever the function does, like how selfish() prints the number you give it to the screen. There's really no point in getting anything back from the function because you don't think it would be of much use. The same effect can be had of functions that do return a value. cout's << operator returns a reference to the stream, which you can use for error handling and chaining. But in this case, we didn't need that feature, so we just ignored it like we would a void function.

A void function could also be a generous goody goody that always gives you stuff, but won't ever accept anything for himself. He's the kind of function that loves to give, but gets insulted if you try to give back.
#include <iostream>

// Gives but refuses to take
int generous()
{
  return 5;
}

int main()
{
  // He doesn't ask for anything
  std::cout << generous() << '\n';
  // But gives me a number. What a nice guy!
}


You can try to give him something for his generosity, but he won't have any of it and refuses to compile until you take it away. He's nice until you try to reciprocate, then he gets all snippy.
std::cout << generous(1, 2) << '\n';


This kind of function is called a generator, and you use it when the function creates something new for you to use, but since it's the source of that something, there's really no point in giving it any values to work with. An example of a generator is the rand() function that gives you a random number out of the blue.

Then you have the introverted loners who don't want anything and think everyone else is the same way.
#include <iostream>

// Completely independent
void loner()
{
  std::cout << "Too cool to be social\n";
}

int main()
{
  // He doesn't ask for anything...
  loner();
  // And doesn't give me anything...
}


Like the first void function, you're only interested in the side effect that this guy has (usually depressing people), and the side effect is completely independent of you, so you don't have anything that would help it along. This little dude works at his own pace and is completely self-contained in his own little world.

Lastly, you have the average joe who takes raw materials and pounds them into something useful, then is forced to give them refined materials to feed the gluttony of The Man.
#include <iostream>

// Just a cog in the machine
int average_grunt(int number)
{
  return number * number;
}

int main()
{
  // He's given the materials
  std::cout << average_grunt(5) << '\n';
  // And makes something for us
}


Just another piece to the puzzle, this function is a filter. He takes stuff and changes it to suit your needs, then gives it back to you so that you can use it for more important things.

This post has been edited by Voodoo Doll: 25 January 2006 - 04:39 PM

Was This Post Helpful? 2

#5 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4478
  • View blog
  • Posts: 28,395
  • Joined: 21-March 01

Re: void function

Posted 25 January 2006 - 04:51 PM

your response was much better than mine.
Was This Post Helpful? 1

#6 eLliDKraM  Icon User is offline

  • PepŤ Le Pewn
  • member icon

Reputation: 6
  • View blog
  • Posts: 6,565
  • Joined: 13-August 05

Re: void function

Posted 25 January 2006 - 06:14 PM

He might be one o dem educated tipes.
Was This Post Helpful? 0
  • +
  • -

#7 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: void function

Posted 25 January 2006 - 06:41 PM

eLliDKraM, on 25 Jan, 2006 - 09:11 PM, said:

He might be one o dem educated tipes.

She, if I'm not mistaken. :)
Was This Post Helpful? 0
  • +
  • -

#8 Jessehk  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 24
  • Joined: 06-January 06

Re: void function

Posted 25 January 2006 - 06:55 PM

Very creative response Voodoo Doll :)
Was This Post Helpful? 0
  • +
  • -

#9 eLliDKraM  Icon User is offline

  • PepŤ Le Pewn
  • member icon

Reputation: 6
  • View blog
  • Posts: 6,565
  • Joined: 13-August 05

Re: void function

Posted 25 January 2006 - 07:33 PM

Amadeus, on 25 Jan, 2006 - 06:38 PM, said:

eLliDKraM, on 25 Jan, 2006 - 09:11 PM, said:

He might be one o dem educated tipes.

She, if I'm not mistaken. :)

i no so edukated
Was This Post Helpful? 0
  • +
  • -

#10 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: void function

Posted 25 January 2006 - 09:12 PM

Looks like Voodoo Doll said it all. :)
Was This Post Helpful? 0
  • +
  • -

#11 dec1pher  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 116
  • Joined: 27-January 06

Re: void function

Posted 28 January 2006 - 06:20 AM

eheh nice explanation voodo doll!
Was This Post Helpful? 0
  • +
  • -

#12 Kcbroncofan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 55
  • Joined: 28-September 05

Re: void function

Post icon  Posted 29 January 2006 - 06:41 PM

I need to write a program that reads a student's name together with their test scores. Then it should compute the average test scores for each student & assign the appropriate grade.

I need to use a void function called calculateAverage to determine the ave of the 5 test scores for each student. I need to use a loop to read and sum the scores. I need the output to be done in the function main.

Then I need to use a value-returning function calculteGrade to determine & return each student's grade. need the output to be done in the function main.

I need to then read the data from a file and send the output to a file. I can not use any global variables.
Was This Post Helpful? 1
  • +
  • -

#13 Voodoo Doll  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 108
  • Joined: 24-January 06

Re: void function

Posted 29 January 2006 - 07:04 PM

Wow, that's a lot of needs. Enough for a separate thread, I think. :) But be warned, I'm not allowed to help much unless you prove with code that you've given it a shot first. It's against the forum policy. But don't worry or be embarrassed to show your code, especially if you've been to meaner forums. I won't laugh...except at my own failings. :P
Was This Post Helpful? 0
  • +
  • -

#14 eLliDKraM  Icon User is offline

  • PepŤ Le Pewn
  • member icon

Reputation: 6
  • View blog
  • Posts: 6,565
  • Joined: 13-August 05

Re: void function

Posted 29 January 2006 - 07:13 PM

I generally accept bribes.
Was This Post Helpful? 0
  • +
  • -

#15 Kcbroncofan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 55
  • Joined: 28-September 05

Re: void function

Posted 29 January 2006 - 07:14 PM

This is what I have so far but I know it is not what I should use.

#include <iostream>
using namespace std;

char name;
double score1;
double score2;
int score3;
int score4;

int main()
{
cout << "Enter student's name: ";
cin >> name;
cout << "Enter test score 1: ";
cin >> score1;
cout << "Enter test score 2: ";
cin >> score2;
cout << "Enter test score 3: ";
cin >> score3;
cout << "Enter test score 4: ";
cin >> score4;
}
Was This Post Helpful? 0
  • +
  • -

#16 eLliDKraM  Icon User is offline

  • PepŤ Le Pewn
  • member icon

Reputation: 6
  • View blog
  • Posts: 6,565
  • Joined: 13-August 05

Re: void function

Posted 29 January 2006 - 07:22 PM

New thread. We have new threads not just for helping you but so people can look back on the archives and find solutions to their problems. They aren't going to bother to look in certain areas if you carry on irrelevant or just plain old different topics on same old threads.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1