mitä eroa on kun joskus funktion return lausekkeessa palautettava arvo on suluissa ja toisinaan taas ei, pelkkä muotoilu vai jotain käytännön eroa?
esim.
...
return 1;
tai
...
return (1);
Ymmärrän kyllä että esim.
bool checkEq(int a, int b)
return (a == b);
sulut ovat tarpeen, lähinnä nuo numerot ja muuttujat suluissa hämäävät.
esim.
...
return 1;
tai
...
return (1);
Ymmärrän kyllä että esim.
bool checkEq(int a, int b)
return (a == b);
sulut ovat tarpeen, lähinnä nuo numerot ja muuttujat suluissa hämäävät.
Vastaukset 10
Vanhimmat
Jos ei sitten jostain syystä halua niitä käyttää.
.. on funktio.
eiköhän se ole paluuarvon siirto rekisteriin ja yksi kappale rts komentoja.
"Return Type
The return type of a function establishes the size and type of the value returned by the function and corresponds to the type-specifier in the syntax below:
Syntax
function-definition :
declaration-specifiers opt attribute-seq opt declarator declaration-list opt
compound-statement /* attribute-seq is Microsoft Specific */
declaration-specifiers :
storage-class-specifier declaration-specifiers opt
type-specifier declaration-specifiers opt
type-qualifier declaration-specifiers opt
type-specifier :
void
char
short
int
long
float
double
signed
unsigned
struct-or-union-specifier
enum-specifier
typedef-name
The type-specifier can specify any fundamental, structure, or union type. If you do not include type-specifier, the return type int is assumed.
The return type given in the function definition must match the return type in declarations of the function elsewhere in the program. A function returns a value when a return statement containing an expression is executed. The expression is evaluated, converted to the return value type if necessary, and returned to the point at which the function was called. If a function is declared with return type void, a return statement containing an expression generates a warning and the expression is not evaluated.
The following examples illustrate function return values.
typedef struct
{
char name[20];
int id;
long class;
} STUDENT;
/* Return type is STUDENT: */
STUDENT sortstu( STUDENT a, STUDENT b )
{
return ( (a.id < b.id) ? a : b );
}
This example defines the STUDENT type with a typedef declaration and defines the function sortstu to have STUDENT return type. The function selects and returns one of its two structure arguments. In subsequent calls to the function, the compiler checks to make sure the argument types are STUDENT.
Note Efficiency would be enhanced by passing pointers to the structure, rather than the entire structure.
char *smallstr( char s1[], char s2[] )
{
int i;
i = 0;
while ( s1[i] != '\0' && s2[i] != '\0' )
i ;
if ( s1[i] == '\0' )
return ( s1 );
else
return ( s2 );
}
This example defines a function returning a pointer to an array of characters. The function takes two character arrays (strings) as arguments and returns a pointer to the shorter of the two strings. A pointer to an array points to the first of the array elements and has its type; thus, the return type of the function is a pointer to type char.
You need not declare functions with int return type before you call them, although prototypes are recommended so that correct type checking for arguments and return values is enabled."
Return-lauseke ei _vaadi_ sulkuja, ei edes lauseessa
return a==b;
Kääntäjän kannalta sulkujen käyttö ei muuta tilannetta miksikään. Sulkuja on syytä käyttää lähinnä selkeyden vuoksi, ohjelman luettavuuden parantamiseksi.
Returnin sulut olivat mukana jo K&R:n ensimmäisessä painoksessa... Varmasti hyvästä syystä.
Jos siis ajatellaan että sulkuja tulisi käyttää vain ja ainoastaan silloin kuin kieli sitä vaatii tai halutaan muuttaa esim. laskentajärjestystä, niin mitä ilo silloin on esim. return (100); lausekkeesta?
Ei tuo mielestäni asiaa mitenkään selvennä.
.. pilkut ja pisteetkin ovat tarpeettomia.
"sulkuja tulisi käyttää vain ja ainoastaan silloin kuin kieli sitä vaatii"
Kuka tollasii puhuu?
Esimerkiksi monimutkaisissa kaavoissa on joskus hyvä käyttää ylimääräisiä sulkuja ihan vain luettavuuden parantamiseksi.
kumpi on mielestäsi selkeämpää
1) return b*b-4*a*c;
vai
2) return (b*b-4*a*c);
Ei väliä käyttääkö sulkuja vai ei, mutta kun käytät sulkuja koodi on selkeämpi ja muutkin saavat selvä koodista
lisäksi on kieliopillisesti oikein käyttää sulkuja.
sulkujen käyttö on sama kuin
esim 1)int *pEdit;
vai
int* Edit;
2)if(a==b)
vai
if (a==b)
Toivottovasti ymmärsit mitä tarkoitin.
kumpi on mielestäsi selkeämpää
1) return b*b-4*a*c;
vai
2) return (b*b-4*a*c);
Ei väliä käyttääkö sulkuja vai ei, mutta kun käytät sulkuja koodi on selkeämpi ja muutkin saavat selvä koodista