Writing Function Definitions (Checking Correctness)

Just a few short questions. I'm writing two function definitions, but I'm not quite clear as to what that means. The first one is writing a function definition called even that takes one argument (as an int) and returns a bool value. The fiction returns true if one argument is an even number, otherwise it returns false.

The second is a function for isDigit that takes an argument (as a char) and returns a bool value. It returns true if the argument is a decimal digit, and false if not.

What does it mean by a function definition? How do I go about writing these? Thanks for any help.
Last edited on
What I ended up with the first one is:
1
2
3
4
5
6
7
8
bool even(int num){
    if(num % 2 == 0){
         return true;
    }
    else{
         return false;
    }
}

Does this make sense?
My answer for the second is:
1
2
3
4
5
6
7
8
9
bool isDigit(char num)
{
    if(floor(num) != num){
         return true;
    }
    else{
         return false;
    }
}

Sound right?
You don't need the excessive if/else in this case - for instance, with the first one, you can simply
return num % 2 == 0;, and similar for the second.

Your solution to the second one doesn't seem right - floor will convert floats/doubles to integers by chopping off the decimal place. I think you mean to compare if the character is between the character values '0' and '9'.
Last edited on
How about you test these with actual data. Did you do that already?
Topic archived. No new replies allowed.