Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,080 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,586 people online right now. Registration is fast and FREE... Join Now!




Easy function question in C

 
Reply to this topicStart new topic

Easy function question in C

njmiano
13 Apr, 2008 - 10:05 AM
Post #1

New D.I.C Head
*

Joined: 26 Feb, 2008
Posts: 6

I am very much a beginner and am working on the first stage of a fairly long C program, so I need to create functions that I am not able to debug yet.

What I was wondering is if a function returns an int, and 0 = false, is the function able to return the value !0, or do I need to change it to 1 or something else?
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Easy Function Question In C
13 Apr, 2008 - 10:24 AM
Post #2

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,522



Thanked: 96 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
Yep, this code will compile fine:
CODE

#include <stdio.h>

//function declaration
int myFunc (int argtopass);

int main ()
{
    int c = 100; //just some value
    printf("%d",myFunc( c)); //calls the function and tests if it is 0
    return 0;
}

//function definition
int myFunc (int argtopass)
{
    if (ret == 0)
        return 0;
    else if (ret != 0)
        return !0; //will return 1
}


Basically, if you return !0, it will return 1

QUOTE

What I was wondering is if a function returns an int, and 0 = false, is the function able to return the value !0, or do I need to change it to 1 or something else?

If you want it to return true or false, why not make your function return a bool? You could do that like this:
CODE

#include <stdio.h>

bool myFunc (int argtopass);

int main ()
{
    int c = 0; //just some value
    printf("%d",myFunc©); //calls the function and tests if it is 0
    return 0;
}

//function definition
bool myFunc (int argtopass)
{
    if (ret == 0)
        return false; //0 value
    else if (ret != 0)
        return true; //1 value
}


I hope this clears it up a little for you smile.gif

This post has been edited by NickDMax: 14 Apr, 2008 - 06:03 AM
User is offlineProfile CardPM
+Quote Post

njmiano
RE: Easy Function Question In C
13 Apr, 2008 - 10:33 AM
Post #3

New D.I.C Head
*

Joined: 26 Feb, 2008
Posts: 6

Thanks, that answers the question and then some.

The reason I don't use the second suggestion is that we haven't been taught about that yet, so it will look a little fishy if I do. It is nice to learn for future purposes though.

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Easy Function Question In C
14 Apr, 2008 - 06:02 AM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
generally speaking if you function only needs to return 1 or 0 a bool works great. There are times when returning a bool is not the best idea. For example if I were writing a function "isprime(int)" I would probably have it return an int rather than a bool. Why? because I may end up multiplying the answer by some value.

So basically any time that I write a boolean function that I might be using in other mathematical expressions I tend to return an integer rather than bool. (not that you can't use the bool in mathematics expressions)

Also, it is very common to care about two conditions: function returned zero, or the function did not return zero -- and if it did not return zero I may want to use that value. So for example my isPrime function might be more useful if it returned the prime number rather than 1 if the number is prime. So I can still use the function as a boolean function, but I can also use it inside other integer expressions.

just something to think about.

QUOTE
The reason I don't use the second suggestion is that we haven't been taught about that yet, so it will look a little fishy if I do. It is nice to learn for future purposes though.


Showing that you have read ahead a little in the book or online is really not generally a problem. It gets a little fishy when you have not mastered the basics but are writing advanced template classes and the boost libraries etc.

Showing that you can pick up on and use the various data types just shows that you are curious and bright.

Heck if my teachers had accused me a of cheating every time I went WAY above and beyond the assignment I would have been screwed (I had been a programmer for a number of years before my first class).
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:38PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month