Jump to content

bool even(int n), a function can take a constnt, variable, or expression as argument

- - - - -

  • Please log in to reply
10 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi :)

1: In a test I was asked to write a function heading which when given an integer as an argument it returns 0 if it's not even and returns 1 if it's even. I believe by heading it was meant function declarator. I simply wrote: bool even(int n). Is it correct?

2: I think the below statement is true. What do you say?
A function can take a constant, variable, or an expression as an argument.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
1: It should have been int is_even(int n) simply because it returns a 0 or a 1, not a boolean value. In C, there's potentially no difference (it could just be a typedef), but in C++ they are definitely different types.

2: It's false, you cannot take an expression directly as an argument. Instead the expression is resolved into a temporary value which is then passed to the function.
Wow I changed my sig!

#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I don't think there's much difference between bool even(int n) and int is_even(int n). Beside, OP didn't state whether he was coding in C or C++ and both are valid in both languages (with exception that C would need typedef for bool).
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#4
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts

Flying Dutchman said:

I don't think there's much difference between bool even(int n) and int is_even(int n). Beside, OP didn't state whether he was coding in C or C++ and both are valid in both languages (with exception that C would need typedef for bool).

Hi

1: I'm learning C++. So, does this mean my declarator, "bool even(int n)" is correct?


2:

Quote

I think the below statement is true. What do you say?
A function can take a constant, variable, or an expression as an argument.

But I'm sure a function can take a constant and variable as an argument. Correct? I think I get the point that an expression cannot be used as an argument. Suppose there is an expression, int z = (x + y)/6, where x and y are also variables and have been declared before in the code. I can use z as the argument but it is not possible to use the right-side "(x + y)/6" directly as the argument. Correct?

Thanks for all the help.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#5
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
You can of course pass expression as argument, and like ZekeDragon said, it's resolved into a temporary variable which is passed to a function.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#6
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts

Flying Dutchman said:

I don't think there's much difference between bool even(int n) and int is_even(int n). Beside, OP didn't state whether he was coding in C or C++ and both are valid in both languages (with exception that C would need typedef for bool).
I answered the question based on what jackson was trying to do. According to the question, his program was supposed to "returns 0 if it's not even and returns 1 if it's even," which is essentially a boolean, but nonetheless the expected interface is that a numerical value will be returned, not a true/false value. Nonetheless, the grand majority (if not all) implementations of C++ I've seen treat bools pretty much identically to ints. Guess I was just being pedantic. :P

And yes, a function can accept a constant and a variable as an argument.
Wow I changed my sig!

#7
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
I would add that in my opinion 2 is also true though the argument is perfect. An expression is evaluated first into a temporary variable and that is passed to a function.

But i think the statement was more about the usage rather than what happens behind the scene.

So if we can write fun(a+b) // where fun takes an int, i think it is safe to say function can take an expression as an argument.

When functions are taught, the intention of saying constant, variable or expression is focused on the usage, not on how the compiler implements it.

Just an opinion.

#8
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thank you, Dutchman, ZekeDragon, Fayyaz.

fayyazlodhi said:

But i think the statement was more about the usage rather than what happens behind the scene.

Yes, this was more about the usage. Who cares what goes on behind the scene!:)

I have just experimented with three concepts that a constant, variable, and an expression can be used as an argument. Please check the CODE1.

I have also written a program which used bool type to tell if the number is even or not. Please check CODE2. Please have a look on the bold if condition. Why can't I use "=" instead of "=="? Please let me know.

CODE1

#include <iostream>

#include <cstdlib>


using namespace std;


float Sum(float dummy1, float dummy2);


int main()


{

    float x, y, n, sum1, sum2;


    cout << "enter x: "; cin >> x;

    cout << "enter y: "; cin >> y;

    cout << "enter n: "; cin >> n;



    sum1 = Sum( x, n);


    cout << "sum1 is: " << sum1 << endl;


    cout << "sum2 is: " << Sum(5*(x+y), 2) << endl;


    system("pause");

    return 0;


}


//-------------------------------------------------------------

// sum() definition

float Sum(float dummy1, float dummy2)

{

    float Sum;


    Sum = dummy2 * (dummy1);


    return Sum;

}

//------------------------------------------------------



CODE2

// return 0 when not even, return 1 when even


#include <iostream>

#include <cstdlib>


using namespace std;


bool even(int dummyn);


int main()


{

    int n;

    int evencheck;


    cout << "enter n: "; cin >> n;


    evencheck = even(n);


    if (evencheck == 1)

    {

        cout << "n is even" << endl;

    }


    else if (evencheck == 0)

    {

        cout << "n is not even" << endl;

    }


    system("pause");

    return 0;

}


//----------------------------------------------------

// even() definition

bool even(int dummyn)

{

    bool evenstatus;


    if ( ([B]dummyn % 2) == 0[/B])

    {

        evenstatus = true;

    }


    else

    {

        evenstatus = false;

    }


    return evenstatus;

}


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#9
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
== is comparing two values like if ( x is equal to 4)
= is assign 4 to x

so if i write


if (x=4)

 // do something


It will always be true (as long as x exists as an integer variable). Mistakenly using = where == was intended is one of the biggest / common mistakes of c programming.

A better idea is to develop habit of using if (4 == x) i.e. place constant on the left. So if you mistype you get a compiler error.

#10
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
A quick note on CODE2; if a number is not even it can only be odd, so you can reduce else if to just else.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#11
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts

fayyazlodhi said:

== is comparing two values like if ( x is equal to 4)
= is assign 4 to x

so if i write


if (x=4)

 // do something


It will always be true (as long as x exists as an integer variable). Mistakenly using = where == was intended is one of the biggest / common mistakes of c programming.

A better idea is to develop habit of using if (4 == x) i.e. place constant on the left. So if you mistype you get a compiler error.

Thanks a lot, Fayyaz. I hope I won't make this mistake again. I understand it now.

Flying Dutchman said:

A quick note on CODE2; if a number is not even it can only be odd, so you can reduce else if to just else.

Thanks, Dutchman, for pointing this out. This correction of such mistakes improves the quality of work a lot.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users