1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

DeclarationFunction , parameters and argument

Discussion in 'Microcontrollers' started by Parth86, Sep 18, 2017.

  1. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    Hi
    I am having difficulty to understand the declaration of different type of function . I am confuse on selection . when do i need specific one . I want to understand use of return type , argument and parameters in c programming

    This is basic structure in c
    Code (c):

    <return-type> function_Name( <parameter-list> )
    {
       <statements>
        return <expression of type return-type>;
     }
     
    I understand meaning of this function No return Type and no Argument
    Code (c):

    void function_Name( void)
    {
        statements ;
     }
     
    What does function do ? when do we use this type of function in program ? what happen when we exit function ? (it does not return any value and does not pass any argument so what does it do )

    I understand meaning of this function No return Type and one Argument
    Code (c):

    void function_Name( unsigned i )
    {
        statements ;
     }
     
    we pass the value of i variable
    what does function do ? when do we use this type of function in program ? what happen when we exit function ?
    (it does not return any value so is it only use to pass the value of this variable )
     
    Last edited: Sep 18, 2017
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Functions can be used to do one thing or many things.

    Take this delay function returns nothing as it doesn't need to..
    Code (c):

    void delayUs(int x){
       while(x--);
    }
     
    Take this sound function it accepts nothing and returns nothing as it doesn't need to..
    Code (c):

    void Sound(void){
       LATA2_bit = 1;
       delayUs(1000);
      LATA2_bit = 0;
    }
     
    Procedural programming is a way of organising reusable code
     
  3. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    What does it function do. does it do only one thing ? does it give only delay
    Code (c):

    void Delay (void)
    {
       unsigned int i;
        for (i = 0; i < 40000; i++);
        {
       
        }
    }
     
    How many things does this function do ? i think two, it give delay and pass the value of variable i in main function?
    Code (c):

    void Delay (unsigned int i)
    {
        for (i = 0; i < 40000; i++);
        {
       
        }
    }
     
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK

    Think of it more like a "mini" Program... In your example it's pointless passing a variable as you overwrite it.

    Better like this:-
    Code (c):

    void Delay (void)
    {
       unsigned int i;
        for (i = 0; i < 40000; i++);
        {
       
        }
    }
     
    Passing parameters is only necessary if you need to start a process... In my example I pass "x" to reduce in size until zero... This way I can have variable delays..
     
  6. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    Still confuse, Look at both examples Do you see any difference? I think first function give fix delay and second example give different delay
    Example 1: No return type , no argument
    Code (c):
     void Delay (void)
    {
       unsigned int i;
        for (i = 0; i < 40000; i++);
        {
     
        }
    }
    int main ()
    {
      while (1)
       {
           Statment 1;
           Delay();
           Statment 2;
           Delay();
       
        }
    }
    Example :2 No return type , one argument
    Code (c):
    void Delay (unsigned int i)
    {
        for (i = 0; i < 40000; i++);
        {
     
        }
    }
    int main ()
    {
       while (1)
       {
           Statment 1;
           Delay(20);
           Statment 2;
           Delay(30);
       }
    }
     
     
    Last edited: Sep 18, 2017
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Look again...

    delay(20).... 20 is passed as i.. but then you change i to 0 and count to 40000.. Wastes the parameter..
     
  8. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    OK that was example for basic understanding about return type and parameters list
    I have another type of function
    this function return integer value. does it return value 0 to 255?
    Code (c):

    unsigned int Switches(void)
      {
          unsigned int ReturnValue = 0;
           if (Switch_1== Switch_Closed )
              {
                 ReturnValue = 1 ;
              }
           else if (Switch_2== Switch_Closed)
              {
                 ReturnValue = 2;
              }
          else if (Switch_2== Switch_Closed)
              {
                 ReturnValue = 3;
              }
       
              return ReturnValue;
      }
     
    how does this function work ? does it work in following manner
    if first condition true, than return "1"
    else if second condition true, than return "2"
    else if third condition true, than return "3"
    and if no condition is true than return 0

    can we write like this
    return type character , No argument
    Code (c):

    unsigned char Switches(void)
      {
           unsigned char ReturnValue = a;
           if (Switch_1== Switch_Closed )
              {
                 ReturnValue = b ;
              }
           else if (Switch_2== Switch_Closed)
              {
                 ReturnValue = c;
              }
          else if (Switch_2== Switch_Closed)
              {
                 ReturnValue = d;
              }
       
              return ReturnValue;
      }
     
     
    Last edited: Sep 18, 2017
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    A) Correct return will be 0~3..

    B) You can use variables, as long as they are defined somewhere.... a = 0, b=1 etc...

    If you want to send a,b,c etc write it like this

    ReturnValue = 'a'; this way ascii a (ox61) will be returned..
     
    • Like Like x 1
  10. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    You can have multiple returns in a function. Also, if your if statement only has one instruction, as in your above code, you can omit the curly braces - although that is not recommended for a beginner.
    So, your code could be written,
    Code (text):

    unsigned char Switches(void)
        {
           if (Switch_1== Switch_Closed)
                return('b');
           else if (Switch_2== Switch_Closed)
                return('c');
           else if (Switch_2== Switch_Closed)
                return('d');
           return('a');
        }
     
    Mike.
     
    • Like Like x 1
  11. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    OK. a switch can open or closed. I can write single function for switch
    This is function to read the input of switch 1
    Code (c):
    void Switches_1(void)
      {
           if (Switch_1== Switch_Closed )
              {
                 Switch_1== 1;
              }
              else
                       {
                           Switch_1== 0
                       }
      }
    This is function to read the input of switch 2
    Code (c):
     void Switches_2(void)
      {
           if (Switch_2== Switch_Closed )
              {
                 Switch_2== 1;
              }
              else
                       {
                           Switch_2== 0
                       }
      }
    Now I just want to write one function to read each switch in sequence ? How to do it one function ?
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    How many switches??

    I have a four key keypad..... 1,2,3 and 4.. Each key has a pullup resistor.. Here's my keypad routine

    Code (c):

    int getKey(void){
       int key = 0;
       if(!switch1)  key+=1; // key 1
       if(!switch2)  key+=2; // key 2
       if(!switch3)  key+=3; // key 3
       if(!switch4)  key+=4; // key 4

       if(key) beep(250);
       return key;
    }
     
    Now if the user presses a key the number is returned... If the user presses 2 and 3 then 5 is returned..

    Simple!
     
  13. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    I think there are difference between switch and button. I am talking about switch. I consider switch. if switch is open it return 0 and if switch is closed it return 1
    Code (c):

    unsigned int Switch(void)
        {
               {
            if (Switch_1== Switch_Closed)
                return('1');
             }
             return('0');
        }
     
    How to write single function for two switches
     
  14. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Its exactly the same... Button, switch, universal breaker.. What's the difference... As long as it returns on or off!

    Just delete the beep statement!

    Also if you need to know the condition of each, then return binary!
    retval = 0;
    if(!switch1) retval +=0x01;
    if(!switch2) retval +=0x02;
    if(!switch3) retval +=0x04;
    if(!switch4) retval +=0x08;
    return retval;
     
  15. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    Now I want to store the inputs of keypad in to array

    we declare array in c like this

    type name[number of elements];

    I have function which can read function Now I want to write function to store the input of switches in to array and than display switch value ? I don't understand how to do it?
     
  16. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Conflicting statements here!!

    When you read a button / switch allow 250ms for bounce and then store if there has been a change..
     
  17. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    Ok Let me explain what I want to do
    upload_2017-9-20_0-16-19.png
    first one is push button and second is switch. I want to use switch and I don't want to use button
    I have 8051 microcontroller, LCD 16*2 and Two switches
    Switch 1 has two state open means "0" and closed means "1"
    Switch 2 has two state open means "0" and closed means "1"
    I wrote this function to read switch 1
    Code (c):
    void Switches_1(void)
      {
           if (Switch_1== Switch_Closed )
              {
                 Switch_1== 1;
              }
              else
                       {
                           Switch_1== 0
                       }
      }
    But I am using two switches so I need to read two switches. so I was trying to write function to read both switches and after that I want to store the value of switches in to array and than display that value on LCD. read the value of input switches and store them in array
     
    Last edited: Sep 19, 2017
  18. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    AARrgh!!

    The code I gave you reads FOUR switches... The return is a single int so it can be used to display the switch conditions...
    Code (c):

    int array[4];
    int x;
    x=getkey(); // get condition of switches
    if(x&1) array[0] = 1; else array[0] = 0;
    if(x&2) array[1] = 1; else array[1] = 0;
    if(x&4) array[2] = 1; else array[2] = 0;
    if(x&8) array[3] = 1; else array[3] = 0;
     
     
  19. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    does it means (!switch1) is equivalent to (switch1 == 0) ( if switch is open )
    return _value +=0x01;
    what is meaning of this line. does it add return value of switch status ?
     
  20. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    if ( switch one is low ) then set the 1st bit of the return value.. otherwise don't!

    Remember its easier to set pullups so if the switch is on, the a zero is felt at the pin!
     
  21. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    Little bit confusing, whats the meaning of above statement in plain language ?

    Return value 0 means switch is open
    if switch1 is not open than add binary value with return value
    if switch2 is not open than add binary value with return value
    if switch3 is not open than add binary value with return value
    if switch4 is not open than add binary value with return value
    return return value
     

Share This Page