Thread: Calling a variable from an other function and using it in an other fuction

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    10

    Calling a variable from an other function and using it in an other fuction

    Hello,
    I am a bit confused over the variables and the fuction declarations in C. I am coming from a 3 year python trip and it feels a bit odd but i really like it. This is my code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <unistd.h>
    void starting_screen(void);
    void selection_loader(int option);
    
    int main()
    {
      starting_screen();
      printf("~~~~~~~~~~~~~~~~~~Choose option~~~~~~~~~~~~~~~~~~~~~~");
      int option;
      scanf("%d", &option);
      starting_screen();
      selection_loader(option);
      return 0;
    }
    
    void starting_screen(void)
    {
                                 //starting screen
          printf("\n");
      printf("\n");
      printf("\n");
      printf("1.Mini text editor");
      printf("\n");
      printf("2.Insert text");
      printf("\n");
      printf("3.Import dictionary");
      printf("\n");
      printf("4.Edit text");
      printf("\n");
      printf("5.write and save text");
      printf("\n");
      printf("6.Calculate statistic data");
      printf("\n");
      printf("7.Escape this hell");
      printf("\n");
    }
    
    int selection_loader(option)
    {
      if (option == 7) {
        exit(0);
      }
    
    }
    I want the selection_loader function to see the option variable and act accordingly the if statement. I am 100% sure that my function declaration is wrong. Thanks!
    Last edited by Salem; 04-14-2019 at 08:29 PM. Reason: Removed crayola

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your function declaration is fine. It is your function definition that is wrong: you left out the parameter type and changed the return type to int when you originally declared it to be void.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    10
    Thank you for the reply. May i ask,the fuction definition is above the code ,where you say that for eg is a void and the declaration is in the main program correct? Cause i am bit confused when you said you left out the parameter type and changed the return type to int when you originally declared it to be void.

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    10
    Code:
    void selection_loader(int option)
    {
      if (option == 2) {
        exit(0);
      }
    
      if (option == 7) {
        printf("Quit successfull");
        exit(0);
      }
    
    }
    MOD: Please stop posting code with weird fonts and colours
    Last edited by Salem; 04-15-2019 at 11:19 AM. Reason: Fixed crayola

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by frequencynip
    May i ask,the fuction definition is above the code ,where you say that for eg is a void and the declaration is in the main program correct?
    This is a function declaration, more precisely a forward declaration serving as a function prototype:
    Code:
    void selection_loader(int option);
    This is the corresponding function definition from your original code:
    Code:
    int selection_loader(option)
    {
      if (option == 7) {
        exit(0);
      }
    
    }
    This is a function call in the main function:
    Code:
    selection_loader(option);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    10
    Thank you for your help. Also the fonts are copy-pasted from VSCODE. I will paste them to notepad++ and repost them here. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-25-2016, 05:34 AM
  2. Calling function based on passing variable.......
    By pattisahusiwa in forum C++ Programming
    Replies: 3
    Last Post: 12-20-2011, 03:02 AM
  3. Calling a function named in a variable.
    By JohnAnon in forum C++ Programming
    Replies: 25
    Last Post: 04-01-2011, 12:38 AM
  4. Help Finding the mode, calling fuction
    By CaliJoe in forum C++ Programming
    Replies: 5
    Last Post: 04-09-2009, 08:50 PM
  5. Replies: 5
    Last Post: 01-13-2006, 12:00 AM

Tags for this Thread