Jump to content

Trouble with return statement

Spev
Go to solution Solved by miles7191,

The ? is used as an inline if statement. So if t == nullptr then return -1 else t->height. 

Can anyone help me understand the following code? I don't understand the return statement. Typically I am just used to returning a single variable. I'm unfamiliar with the question mark and semicolon here.    

 

    int height( AvlNode *t ) const
    {
		return t == nullptr ? -1 : t->height;
    }

 

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]

Link to comment
Share on other sites

Link to post
Share on other sites

The ? is used as an inline if statement. So if t == nullptr then return -1 else t->height. 

Link to comment
Share on other sites

Link to post
Share on other sites

Its a if/else shorthand using ternary operators

 

(statement) ? (statement true value) : (statement false value)

 

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, miles7191 said:

The ? is used as an inline if statement. So if t == nullptr then return -1 else t->height. 

Oh ok. I googled inline if statement and got some more info after that. I've never seen it before. Pretty simple but looked really bizarre the first time seeing it. Thanks for the clarification.

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×