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 )
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..
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);
}
}
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;
}
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');
}
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');
}
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;
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?
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
Ok Let me explain what I want to do
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
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
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 ?
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