• 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.

DeclarationFunction , parameters and argument

Status
Not open for further replies.

Parth86

Member
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
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
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
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:

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Functions can be used to do one thing or many things.

Take this delay function returns nothing as it doesn't need to..
C:
void delayUs(int x){
   while(x--);
}
Take this sound function it accepts nothing and returns nothing as it doesn't need to..
C:
void Sound(void){
   LATA2_bit = 1;
   delayUs(1000);
  LATA2_bit = 0;
}
Procedural programming is a way of organising reusable code
 

Parth86

Member
Functions can be used to do one thing or many things.
What does it function do. does it do only one thing ? does it give only delay
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?
C:
void Delay (unsigned int i)
{
    for (i = 0; i < 40000; i++);
    {
    
    }
}
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Think of it more like a "mini" Program... In your example it's pointless passing a variable as you overwrite it.

Better like this:-
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..
 

Parth86

Member
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
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
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:

Parth86

Member
Look again...

delay(20).... 20 is passed as i.. but then you change i to 0 and count to 40000.. Wastes the parameter..
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?
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
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:

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
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..
 

Pommie

Well-Known Member
Most Helpful Member
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:
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.
 

Parth86

Member
OK. a switch can open or closed. I can write single function for switch
This is function to read the input of switch 1
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
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 ?
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
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

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!
 

Parth86

Member
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
Now if the user presses a key the number is returned... If the user presses 2 and 3 then 5 is returned..
Simple!
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
C:
unsigned int Switch(void)
    {
           {
        if (Switch_1== Switch_Closed)
            return('1');
         }
         return('0');
    }
How to write single function for two switches
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
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;
 

Parth86

Member
Its exactly the same... Button, switch, universal breaker.. What's the difference... As long as it returns on or off!
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?
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
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
Now I want to store the inputs of keypad in to array
Conflicting statements here!!

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

Parth86

Member
Conflicting statements here!!

When you read a button / switch allow 250ms for bounce and then store if there has been a change..
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
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:

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
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...
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;
 

Parth86

Member
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;
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
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top