Author Topic: Programming Megathread  (Read 107639 times)

So I'm trying to make a program that calculates the average of five numbers by sending them to a function called SUM to calculate the sum, then return to MAIN to calculate the average. I'm having trouble figuring out how to return to MAIN though. Here's what I have so far:
http://codepaste.net/me8fpa

This is my first program using functions so point out any other syntax issues there might be.
« Last Edit: May 03, 2016, 10:50:41 AM by Ravencroft· »

First off,  return isn't a function,  so you don't do return(SUM);
It's just return SUM;

Then you need a call to that function after the inputs: float sum = SUM(a,b,c,d,e);

Also, you're missing an ending curly brace at the end of main

First off,  return isn't a function,  so you don't do return(SUM);
It's just return SUM;

Then you need a call to that function after the inputs: float sum = SUM(a,b,c,d,e);

Also, you're missing an ending curly brace at the end of main

That's odd. My lecture notes say to have parentheses around the return variable. There's even parentheses on it on the example code snippets.
And yeah I know I'm missing a brace. I tried removing it thinking the main function had to be wrapped around the sum function. So how exactly would I continue with the main function after returning? Do I put the code in the first main function brackets or start a new main function after the sum function(talking about the part where the average is calculated and output to the user)?

« Last Edit: May 03, 2016, 12:46:52 PM by Ravencroft· »

That's odd. My lecture notes say to have parentheses around the return variable. There's even parentheses on it on the example code snippets.
And yeah I know I'm missing a brace. I tried removing it thinking the main function had to be wrapped around the sum function. So how exactly would I continue with the main function after returning? Do I put the code in the first main function brackets or start a new main function after the sum function(talking about the part where the average is calculated and output to the user)?


The main issue is that you've left the SUM function inside the main function, when it's supposed to be outside the main function. Like so:

Code: [Select]
#include <stuff>

using namespace std;

float SUM(float a, float b,  float c, float d, float e)
{
   return a + b + c + d + e;
}

int main(void)
{
   // Your code defining values and collecting their data

   float answer = SUM(a, b, c, d, e);
   cout << "The answer is " << float << endl;
}


That's odd. My lecture notes say to have parentheses around the return variable. There's even parentheses on it on the example code snippets.
I guess parenthesis are usually allowed and usually works. But it's not really standard, and not required. Probably better not to, as some IDEs/compilers/languages may not allow it

I guess parenthesis are usually allowed and usually works. But it's not really standard, and not required. Probably better not to, as some IDEs/compilers/languages may not allow it
I'm pretty sure return parenthesis are C style

When I was young and new to coding in C, the documentation/tutorial stuff I was reading had me use parenthesis. In C, C++ and C# they all allow parenthesis after return for grouping items together. Nowadays I'll only use them if I need to cast my math into the return type.

anyone know how to disable the auto formatting in visual studio? i bought it today

you what? are you aware there's a free version with no restrictions?

https://www.visualstudio.com/products/visual-studio-community-vs

Nowadays I'll only use them if I need to cast my math into the return type.
why?
return 1 + 1 will work just as well as return(1 + 1)
I don't really care what you use, but you should keep it consistent...

edit: wait, that's not what you mean
I don't know what the syntax is for type casting in any of those languages, but I don't think it will require you to use parentheses either
« Last Edit: May 03, 2016, 03:57:42 PM by Foxscotch »

why?
return 1 + 1 will work just as well as return(1 + 1)
I don't really care what you use, but you should keep it consistent...
In several instances I've had to convert input parameters into a different type. This is an example from an old project of mine that demonstrates it perfectly:

Code: [Select]
   /// <summary>
    /// Returns a single Float value based on Inspector-style values. Minimum value is "0" and maximum value is "255".
    /// </summary>
    /// <param name="s">The single number to convert.</param>
    /// <returns></returns>
    public static float ConvertInspectorColours(int s)
    {
        return (float)s / 255.0f;
    }

EDIT: I couldn't find the example where I've done return (type)(formula);, but there was an example where I was working ints and floats and it was necessary. I'll keep looking for it.
EDIT: Actually, rather disappointed at that function that I didn't do any input checking to handle cases below 0 and above 255...
« Last Edit: May 03, 2016, 04:07:31 PM by McJob »

that's different
what everyone is talking about right now is if you did it like this
return((float)s / 255.0f);
not just whether or not there are parentheses somewhere in return statement

anyway, I think using parentheses for the return statement makes it look like a function, so I don't like using them (or seeing them)

also, that comment looks like a huge hassle :<
I like sphinx's autodoc format for python
"""
Description of whatever you're documenting.

:param int size: Description of size parameter
:return: Description of return value
:rtype: str
"""

I don't really know why there's a separate rtype role, instead of just like, ":return str: Description" or something. but whatevs

Ok I was able to figure the program out. I was approaching it all wrong, as I thought the SUM function had to go in the middle of MAIN when it had to be after MAIN. It also took me a bit of time to figure out how the variables recognize eachother as copies.

Final result:
http://codepaste.net/n4ob5w

You didn't actually use the SUM function anywhere, though?

You didn't actually use the SUM function anywhere, though?
How do I fix that?