help a beginner with void function

B

bpascal123

Hi,

I first thanks people who answered earlier posts. As I have started
learning c since 3-4 months, I can only deal with simple things but I
like to understand as much as i can and when i don't find something
coherent in my learning process I try to understand why.

Below is a code from a tutorial application that doesn't work as it
should when i'm able to deal well with the next tutorial application
supposed to be more difficult (you can find its code in the bottom of
this post).

The subject here as i'm doing my best to explain, is to send a
function parameter to main or wherever it is declared without using
"return sthg" . That is to say, the function is of a void cast : like
" void Sthg(*a, b) ; " ... The tutorial gives examples of this by
using pointers.

APPLICATION 1 - basic void function (not working ) :

#include <stdio.h>

void EntRe(int *nbr)
{
printf("\n Enter integer : \n") ;
scanf("%d", nbr) ;
}

int main(void)
{
void EntRe(int *nbr) ;
int a ;
printf("\nYou've entered : %d\n", EntRe(&a) ) ;
}

Compiler message : text14.c:14: error: invalid use of void expression

===================

Application 2 : Same "recipe" but it's working :

===================

#include <stdio.h>
#define NMAX 10

int nmax2 = 0 ;

int main(void)
{
void MaxMin(int n, int *Tab, int *max, int *min) ;

int i, cnt = 0 ;
int op = 0 ;
int rt, tr ;
int Tub[NMAX] ;

for ( i = 0 ; i < NMAX ; i++ )
{
Tub = op * 10 / 8 + 6 ;
op = Tub ;
printf("\nValeur %2d : %4d\n", cnt++, Tub ) ;
nmax2 = i ;
}

MaxMin(nmax2, Tub, &rt, &tr) ;

printf("\nIl y a %d valeurs. \n", nmax2) ;
printf("\nLa valeur min est %d .\n", tr) ;
printf("\nLa valeur max est %d .\n", rt) ;
}

void MaxMin(int n, int *Tab, int *max, int *min)
{
*max = *Tab ;
*min = *Tab ;
int i ;

for ( i = 0 ; i <= nmax2 ; i++ )
{
if ( *(Tab+i) > *max )
*max = *(Tab+i) ;
if ( *(Tab+i) < *min )
*min = *(Tab+i) ;
}
}
 
K

Keith Thompson

The subject here as i'm doing my best to explain, is to send a
function parameter to main or wherever it is declared without using
"return sthg" . That is to say, the function is of a void cast : like
" void Sthg(*a, b) ; " ... The tutorial gives examples of this by
using pointers.

It's a good thing you posted your actual code, because a lot of what
you wrote above frankly doesn't make much sense.

Your function is declared to return void. There's no "cast" involved
(a cast is an operator that specifies a type conversion), so saying
it's "of a void cast" doesn't make sense.

Declaring a function to return void is how you specify that the
function doesn't return a value.

And "void Sthg(*a, b)" doesn't make much sense either.

Now let's look at your code:
APPLICATION 1 - basic void function (not working ) :

#include <stdio.h>

void EntRe(int *nbr)
{
printf("\n Enter integer : \n") ;
scanf("%d", nbr) ;
}

Ok so far. Your function takes an argument of type int*, and doesn't
return anything.
int main(void)
{
void EntRe(int *nbr) ;

The above line is unnecessary. You've already declared the function;
there's no need to repeat the declaration inside main. (It is
sometimes necessary to declare functions, but you usually don't want
to do so inside another function.)
int a ;
printf("\nYou've entered : %d\n", EntRe(&a) ) ;

And here's your problem.

The call ``EntRe(&a)'' is fine; it passes the address of a to EntRe.
But remember that EntRe doesn't return anything; here you're using it
as if it returned an int result.

The call to EntRe doesn't return a value, but it does indirectly pass
a value to its caller. It does so by assign a value to the int object
whose address you've passed to it. By passing &a to the function,
you've enabled the function to store a value in a.

Change the above line to:

EntRe(&a);
printf("\nYou've entered: %d\n", a);

[...]

Some other languages make a stronger distinction between subroutines
that return values and ones that don't. Pascal-like languages, for
example, use the terms "function" and "procedure". A C void function
is like a Pascal procedure.
 
B

Barry Schwarz

On Wed, 15 Jul 2009 16:05:43 -0700 (PDT), "(e-mail address removed)"

snip
Below is a code from a tutorial application that doesn't work as it
should when i'm able to deal well with the next tutorial application
supposed to be more difficult (you can find its code in the bottom of
this post).
snip

#include <stdio.h>

void EntRe(int *nbr)
{
printf("\n Enter integer : \n") ;
scanf("%d", nbr) ;
}

This function "returns" a value in the int pointed to by nbr. However,
it does this by dereferencing the pointer, not by using the return
statement mechanism.
int main(void)
{
void EntRe(int *nbr) ;

If the function definition precedes the use of the function, then a
prototype is not necessary. More precisely, the definition serves as
the prototype also.

While perfectly legal, it is somewhat unusual to place a prototype
inside a function. Prototypes are normally placed at file scope so
any function in the source file can refer to the function.
int a ;
printf("\nYou've entered : %d\n", EntRe(&a) ) ;

In order for the second argument to be legal, the expression must have
a complete type. For a function call, the type of the expression is
the return type of the function. But this function returns void which
is an incomplete type that can never be completed.

For a function that "returns" values by dereferencing pointers instead
of by the return statement, you cannot use the function call as an
expression where a value is required. You need to call EntRe in a
separate statement and then use the object a for the second parameter.

NOTE to be ignored for present: After you study the comma operator,
you could recode the call to printf as
printf("\nYou've entered : %d\n", (EntRe(&a),a) ) ;
BUT THIS STYLE SUCKS SO DON'T.
}

Compiler message : text14.c:14: error: invalid use of void expression

===================

Application 2 : Same "recipe" but it's working :

===================

#include <stdio.h>
#define NMAX 10

int nmax2 = 0 ;

int main(void)
{
void MaxMin(int n, int *Tab, int *max, int *min) ;

int i, cnt = 0 ;
int op = 0 ;
int rt, tr ;
int Tub[NMAX] ;

for ( i = 0 ; i < NMAX ; i++ )
{
Tub = op * 10 / 8 + 6 ;
op = Tub ;
printf("\nValeur %2d : %4d\n", cnt++, Tub ) ;
nmax2 = i ;
}

MaxMin(nmax2, Tub, &rt, &tr) ;

printf("\nIl y a %d valeurs. \n", nmax2) ;
printf("\nLa valeur min est %d .\n", tr) ;
printf("\nLa valeur max est %d .\n", rt) ;
}

void MaxMin(int n, int *Tab, int *max, int *min)
{
*max = *Tab ;
*min = *Tab ;
int i ;

for ( i = 0 ; i <= nmax2 ; i++ )
{
if ( *(Tab+i) > *max )
*max = *(Tab+i) ;
if ( *(Tab+i) < *min )
*min = *(Tab+i) ;
}
}


There is no similarity between this code and the previous program. At
no time is any function call, let alone a function call returning
void, used as the argument of a different function.
 
B

Ben Bacarisse

#include <stdio.h>
#define NMAX 10

int nmax2 = 0 ;

Global variables are a bad idea. You do need them for some things,
bit not here.
int main(void)
{
void MaxMin(int n, int *Tab, int *max, int *min) ;

int i, cnt = 0 ;
int op = 0 ;
int rt, tr ;
int Tub[NMAX] ;

for ( i = 0 ; i < NMAX ; i++ )
{
Tub = op * 10 / 8 + 6 ;
op = Tub ;
printf("\nValeur %2d : %4d\n", cnt++, Tub ) ;
nmax2 = i ;
}


You can just set namx2 outside the loop.
MaxMin(nmax2, Tub, &rt, &tr) ;

Better still just give the right expression as the first argument to
MaxMin. You need NMAX - 1.
printf("\nIl y a %d valeurs. \n", nmax2) ;
printf("\nLa valeur min est %d .\n", tr) ;
printf("\nLa valeur max est %d .\n", rt) ;
}

void MaxMin(int n, int *Tab, int *max, int *min)
{
*max = *Tab ;
*min = *Tab ;
int i ;

for ( i = 0 ; i <= nmax2 ; i++ )

Here you should use n since it was passed to this function for the
purpose (presumably) of knowing how many elements to look at. I'd use
i < n and pass NMAX rather than i <= n and NMAX - 1 just because this
is the most common form.

You can start this loop at 1 rather than 0 since you've already looked
at Tab[0].
{
if ( *(Tab+i) > *max )
*max = *(Tab+i) ;
if ( *(Tab+i) < *min )
*min = *(Tab+i) ;

Why not write Tab? Surely that is simpler and clearer?
 
B

bpascal123

#include <stdio.h>
#define NMAX 10

int nmax2 = 0 ;

int main(void)
{
        void MaxMin(int n, int *Tab, int *max, int *min) ;

        int i, cnt = 0 ;
        int op = 0 ;
        int rt, tr ;
        int Tub[NMAX] ;

        for ( i = 0     ;       i < NMAX     ;       i++ )
        {
                Tub = op * 10 / 8 + 6 ;
                op = Tub ;
                printf("\nValeur %2d : %4d\n", cnt++, Tub ) ;
                nmax2 = i ;
        }

        MaxMin(nmax2, Tub, &rt, &tr) ;

        printf("\nIl y a %d valeurs. \n", nmax2) ;
        printf("\nLa valeur min est %d .\n", tr) ;
        printf("\nLa valeur max est %d .\n", rt) ;

}

void MaxMin(int n, int *Tab, int *max, int *min)
{
        *max = *Tab ;
        *min = *Tab ;
        int i ;

        for ( i = 0     ;       i <= nmax2   ;       i++ )
        {
                if ( *(Tab+i)   >    *max )
                        *max = *(Tab+i) ;
                if ( *(Tab+i)   <       *min )
                        *min = *(Tab+i) ;
        }

}


Anyone ?

Is there a way to have nmax2 local (without any "return"
instruction ?)

Thanks,
Pascal
 
B

bpascal123

Anyone ?

Is there a way to have nmax2 local (without any "return"
instruction ?)

Thanks,
Pascal


Oops sorry Ben,

About disgarding your very interesting reply (above in this topic) for
helping me to learn C.
....anyone, just forget my last question...
I'll come back because i have some more questions about functions
parameters.

Pascal
 
B

bpascal123

Global variables are a bad idea.  You do need them for some things,
bit not here.
You can just set namx2 outside the loop.
Better still just give the right expression as the first argument to
MaxMin.  You need NMAX - 1.
Here you should use n since it was passed to this function for the
purpose (presumably) of knowing how many elements to look at.  I'd use
i < n and pass NMAX rather than i <= n and NMAX - 1 just because this
is the most common form.

You can start this loop at 1 rather than 0 since you've already looked
at Tab[0].
Why not write Tab?  Surely that is simpler and clearer?

Hi,

Why not write Tab? Surely that is simpler and clearer?


It seems the tutorial I'm following and the book I'm reading about C
is dealing with pointers at this stage followings chapters about
instructions(1°), loops(2°)... array(3°) (i didn't look at char array
yet-because i needed to learn function to post proper code here...see
below), pointers(4°) and functions(5°). But when i started posting
questions here while i was studying arrays and pointers, i got reply
that would tell me to include functions in the code i was posting
(code wich was then taken from a c tutorial and book).
But some started to argue that functions were "a must" to know before
anything else while some said that studying pointers first could help
to deal with functions...

So back to your previous reply, please find modifications i have made
following your comments. It's very a very basic code in comparison to
code i should be writing after studying C for 3-4 months (not full
time though) let say i am learning at my own pace and i don't really
know where that'll take me...

#include <stdio.h>

int main(void)
{
void MaxMin(int n, int *Tab, int *max, int *min) ;
int i, cnt = 0 ;
int op = 0 ;
int rt, tr ;
int Tub[n] ;

for ( i = 0 ; i < 10 ; i++ )
{
Tub = op * 10 / 8 + 6 ;
op = Tub ;
printf("\nValeur %2d : %4d\n", cnt++, Tub ) ;
}
int nmax2 = i ;

MaxMin(nmax2, Tub, &rt, &tr) ;

printf("\nIl y a %d valeurs.\n", nmax2) ;
printf("\nLa valeur min est %d .\n", tr) ;
printf("\nLa valeur min est %d .\n", rt) ;
}

void MaxMin(int n, int *Tab, int *max, int *min)
{
*max = *Tab ;
*min = *Tab ;
int i ;

for ( i = 1 ; i <= nmax2 ; i++ )
{
if ( *(Tab+i) > *max )
*max = *(Tab+i) ;
if ( *(Tab+i) < *min )
*min = *(Tab+i) ;
}
}


I've got some more questions but i want to try to find the answer
before making use of usenet servers.

Thanks for your help,

Pascal
 
B

Ben Bacarisse

You cut out the attribution line. It keep the record straight if you
leave them in.

Why not write Tab? Surely that is simpler and clearer?


It seems the tutorial I'm following and the book I'm reading about C
is dealing with pointers at this stage followings chapters about
instructions(1°), loops(2°)... array(3°) (i didn't look at char array
yet-because i needed to learn function to post proper code here...see
below), pointers(4°) and functions(5°). But when i started posting
questions here while i was studying arrays and pointers, i got reply
that would tell me to include functions in the code i was posting
(code wich was then taken from a c tutorial and book).
But some started to argue that functions were "a must" to know before
anything else while some said that studying pointers first could help
to deal with functions...


Do you mean that the test you are using writes *(Tab+i) after having
introduced arrays and pointers? If so that is another reason to look
for another. Can you find an alternative?
So back to your previous reply, please find modifications i have made
following your comments. It's very a very basic code in comparison to
code i should be writing after studying C for 3-4 months (not full
time though) let say i am learning at my own pace and i don't really
know where that'll take me...

#include <stdio.h>

int main(void)
{
void MaxMin(int n, int *Tab, int *max, int *min) ;
int i, cnt = 0 ;
int op = 0 ;
int rt, tr ;
int Tub[n] ;

n is not defined here. Writing 10 would work fine.
for ( i = 0 ; i < 10 ; i++ )
{
Tub = op * 10 / 8 + 6 ;
op = Tub ;
printf("\nValeur %2d : %4d\n", cnt++, Tub ) ;
}
int nmax2 = i ;

MaxMin(nmax2, Tub, &rt, &tr) ;

printf("\nIl y a %d valeurs.\n", nmax2) ;
printf("\nLa valeur min est %d .\n", tr) ;
printf("\nLa valeur min est %d .\n", rt) ;
}

void MaxMin(int n, int *Tab, int *max, int *min)
{
*max = *Tab ;
*min = *Tab ;
int i ;

for ( i = 1 ; i <= nmax2 ; i++ )


Your compiler should have said that nmax2 is not defined here. You
need use n -- that is why this function has a parameter called n.
Also you will need to change the <= to <.
 
B

bpascal123

   int Tub[n] ;
n is not defined here.  Writing 10 would work fine.
Your compiler should have said that nmax2 is not defined here.  You
need use n -- that is why this function has a parameter called n.
Also you will need to change the <= to <.

Hi,

I made necessary changes and it's working. However I remember it was
working the day i made the post. Maybe i copy/paste another version...

Thanks,
Pascal
 
B

bpascal123

   int Tub[n] ;
n is not defined here.  Writing 10 would work fine.
Your compiler should have said that nmax2 is not defined here.  You
need use n -- that is why this function has a parameter called n.
Also you will need to change the <= to <.

Hi,

I made necessary changes and it's working. However I remember it was
working the day i made the post. Maybe i copy/paste another version...

Thanks,
Pascal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top