Thread: Problems using a function

  1. #1
    zach
    Guest

    Problems using a function

    Code:
    #include <stdio.h>
    #include<strings.h>
    #include<windows.h>
    
    void locate(short x, short y) ;
    void remove_scrollbar(void);
    void makeFrame(void);
    char security();
    
    int main(void)
    {
        remove_scrollbar();
        makeFrame();
        
        locate(5,4);
        char str[5] = security());
        locate(10,10);
        printf("%s", str);
        
        locate(2,27);
        printf("  Press enter to leave the program ");
        getchar();
        return 0;
    }
    
        char security(void)
        {
            char out[5];
            locate(5,2);
            printf("Enter security code: ");
            scanf("%4d", &out);
            return out;
        }
    I am wanting to use a value established in the function. Nothing but errors. Please help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You can't return an array.

    Try
    Code:
    void security(char []);
    int main(void)
    {
        char str[5];
        security(str);
        return 0;
    }
     
        void security(char str[])
        {
            printf("Enter security code: ");
            scanf("%4s", str);
        }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    zach
    Guest
    Quote Originally Posted by Salem View Post
    You can't return an array.

    Try
    Code:
    void security(char []);
    int main(void)
    {
        char str[5];
        security(str);
        return 0;
    }
     
        void security(char str[])
        {
            printf("Enter security code: ");
            scanf("%4s", str);
        }
    Thank you. However, my problem is passing a value back to main () and doing things, in main(), with the passed back value.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    That IS passing the value back to main.

    Try it, examine the value of str after calling the function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    So, you did not try the code you were given!
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    zach
    Guest
    Surprise!
    So void foo(char[]) means that the parameter given foo, becomes a static?
    I thought that void foo means there is no return value.
    What then is the use of char* foo(...) ?

  7. #7
    zach
    Guest
    Yes - I believe it was you who gave me examples with char* - I have looked at your code and also put it in with my notes for further reference. The problem at hand is creating a value in a called function, passing that value to main(0) and subsequently doing things with that value in main().

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by zach View Post
    Surprise!
    So void foo(char[]) means that the parameter given foo, becomes a static?
    ?
    False; it means you are passing by pointer or by address.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    zach
    Guest
    I have learned that one can either pass by pointer or by address, but how does parameter become a returned value, without getting static when the foo is void?

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by zach View Post
    I have learned that one can either pass by pointer or by address, but how does parameter become a returned value, without getting static when the foo is void?
    So, you do not understand pass by pointer or by address; I can not explain it to you!
    Edit: I am not able to explain it in just words; and, I am not good at images on the Internet.

    Tim S.
    Last edited by stahta01; 08-19-2019 at 09:28 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  11. #11
    zach
    Guest
    Quote Originally Posted by Salem View Post
    That IS passing the value back to main.

    Try it, examine the value of str after calling the function.
    Is the pass back occurring by means of scanf - does the response to scanf result in placement of the value in memory, leading to the value becoming available in main() also?

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What book are you using to learn C? A good book would explain the various methods to pass values to and from functions much better than what can be shown in a forum.

    Do you know that there are multiple ways to pass a variable into a function? Ie: pass by value and pass by reference (a reference is a pointer in C).

    Do you know that the function return value is not related to any parameter (unless you physically "return" a variable that is a parameter)?

  13. #13
    zach
    Guest
    Quote Originally Posted by jimblumberg View Post
    What book are you using to learn C? A good book would explain the various methods to pass values to and from functions much better than what can be shown in a forum.

    Do you know that there are multiple ways to pass a variable into a function? Ie: pass by value and pass by reference (a reference is a pointer in C).

    Do you know that the function return value is not related to any parameter (unless you physically "return" a variable that is a parameter)?
    Yes that was my premise, but it is happening differently in the example being discussed (qv), and that is exactly what is puzzling me.

  14. #14
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    Syntax error on line 16, you have an extra right parenthesis:


    charstr[5] = security()); <----
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

  15. #15
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    creating a value in a called function, passing that value to main(0) and subsequently doing things with that value in main()
    wtf ?
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function Problems....
    By C+noob in forum C++ Programming
    Replies: 9
    Last Post: 07-11-2005, 04:37 PM
  2. sin() function problems.
    By Lifedragn in forum C Programming
    Replies: 4
    Last Post: 09-28-2004, 11:16 PM
  3. function problems
    By money in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2003, 11:40 AM
  4. function problems
    By faxtoaster in forum C Programming
    Replies: 5
    Last Post: 07-13-2003, 06:25 PM
  5. function problems
    By $0.05$ in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2003, 11:03 PM

Tags for this Thread