no passing arguments but return char type

Thread Starter

Parth786

Joined Jun 19, 2017
642
Hi
I am looking the practice example of function with no passing argument but but return char type in embedded programming. I know this type of function doesn't pass any argument but it return character.

we write this function like this
C:
char function (void)
{
    char variable_name;
  {
    /* Program code */
  }
   return value;
}
I understand the purpose of this type of function but I am looking some real example of this in embedded programming. Generally I want to know when we use this type of function in embedded programming. I searched little bit on google and i found example like keypad reading function. mean's read the keypad and return value that character key.

What could be simple example of that type of function in embedded programming ?
 
Last edited:

WBahn

Joined Mar 31, 2012
30,060
Hi
I am looking the practice example of function with no passing argument but but return char type in embedded programming. I know this type of function doesn't pass any argument but it return character.

we write this function like this
C:
char function (void)
{
    char variable_name;
  {
    /* Program code */
  }
   return value;
}
I understand the purpose of this type of function but I am looking some real example of this in embedded programming. Generally I want to know when we use this type of function in embedded programming. I searched little bit on google and i found example like keypad reading function. mean's read the keypad and return value that character key.

What could be simple example of that type of function in embedded programming ?
What could be a more straight-forward example than getting a character from a keyboard?

How about reading a character from an input buffer?

Or reading a byte of state from a input port?
 

Brian Griffin

Joined May 17, 2013
64
These depend on how do you plan the program. There is no clear answer to the problem - what kind of functions and what kind of data returned is heavily dependent on how you write the overall program.

In most cases, why not practise on a normal C compiler first? You sound like you may need to practise on the fundamentals/rudiments of the language. The answer is correct if the program works as you have expected.

A textbook with some exercises do help too - attempt them and refer to the answers later to compare. Read the examples and then reattempt them.

The forum is not an answer dispensing machine, and it can only guide you to finding or to overcome the difficulties. Becoming too reliant on it will become very counter productive.

Just my two cents.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
In most cases, why not practise on a normal C compiler first? You sound like you may need to practise on the fundamentals/rudiments of the language. The answer is correct if the program works as you have expected.

A textbook with some exercises do help too - attempt them and refer to the answers later to compare. Read the examples and then reattempt them.
In response to your question I quickly wrote a program
C:
#include<stdio.h>

char character1 = 'x';
char character2 = 'y';

unsigned char eual_letter (void);

int main(void)
{
        printf(" Return type character is %c \n", eual_letter());
}

unsigned char eual_letter (void)
{ 
    if ( character1 == character2 )
      return character1;
    else
      return character2;
}
Program output
Return type character is y

what do you think, Is not it enough to show that I can write this type of function in c programming ?

The forum is not an answer dispensing machine, and it can only guide you to finding or to overcome the difficulties. Becoming too reliant on it will become very counter productive..
I got it right so I asked question here and I don't see anything wrong about it. I didn't demand that any how give me answer.
 
Last edited:

WBahn

Joined Mar 31, 2012
30,060
It also demonstrates that you still a long way from being able to understand program logic.

Ask yourself under what possible situation your program will NOT output whatever is in character2.

Being able to write such a function and being able to tell when your program needs such a function are two different things.
 

MrChips

Joined Oct 2, 2009
30,807

Thread Starter

Parth786

Joined Jun 19, 2017
642
Don't we program LCD, UART, USB, on a PC also?
Stop mentioning embedded programming as if it is different from any other kind of programming.
I will not mention embedded programming so just tell me reason why they both are same. Do you think there is no difference between simple c and embedded c programming?

Ask yourself under what possible situation your program will NOT output whatever is in character2.
That was my question, under what possible situation that type of function will work
 

WBahn

Joined Mar 31, 2012
30,060
I will not mention embedded programming so just tell me reason why they both are not different. Do you think there is no difference between simple c and embedded c programming?
Look at the difference between a hosted and a nonhosted implementation.

That was my question, under what possible situation that type of function will work
Whatever situation where you need a function that doesn't take any arguments and that returns a value of type char!

But that's not what I asked you. Let me make it simpler. Keep character2 as 'y'. What value of character1 will result in your program printing out anything other than 'y'?

If your function ALWAYS prints out character2 and has no side effects, does it serve any purpose?
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
But that's not what I asked you. Let me make it simpler. Keep character2 as 'y'. What value of character1 will result in your program printing out anything other than 'y'?

If your function ALWAYS prints out character2 and has no side effects, does it serve any purpose?
That program is just checking tow condition
true condition : if both character's match then return value of first character

C:
#include<stdio.h>

unsigned char eual_letter (void);
int main(void)
{
        printf(" Return type character is %c \n", eual_letter());
}
unsigned char eual_letter (void)
{
   char character1 = 'x';
   char character2 = 'x';
  if (  character1 ==   character2 )
      return  character1;
    else
      return  character2;
}
Return type character is x

false condition : both character's doesn't match then return value of second character
C:
#include<stdio.h>

unsigned char eual_letter (void);
int main(void)
{
        printf(" Return type character is %c \n", eual_letter());
}
unsigned char eual_letter (void)
{
   char character1 = 'x';
   char character2 = 'y';
  if (  character1 ==   character2 )
      return  character1;
    else
      return  character2;
}
Return type character is y

I know this program doesn't have any specific meaning. I just wrote it to show that I can write

Whatever situation where you need a function that doesn't take any arguments and that returns a value of type char!
So if my answer look's correct then I also want to make simple question

What are situation where you need a function that doesn't take any arguments and that returns a value of type char in embedded system?
 
Last edited:

WBahn

Joined Mar 31, 2012
30,060
You didn't do what I said. On top of that, you now put the definitions of character1 and character2 inside the function. Why?

I said to fix character2 at being 'y'. Now try to change character1 to some value that results in anything other than 'y' being returned.

Consider the following function added to your original code:

C:
#include<stdio.h>

char character1 = 'x';
char character2 = 'y';

unsigned char eual_letter (void);
unsigned char eual_letter2 (void);

int main(void)
{
        printf(" Return type character is %c \n", eual_letter());
        printf(" Return type character is %c \n", eual_letter2());}

unsigned char eual_letter (void)
{
    if ( character1 == character2 )
      return character1;
    else
      return character2;
}

unsigned char eual_letter2 (void)
{
      return character2;
}
Can you find ANY value that you can set character1 and character2 to such that the two lines that are printed out aren't identical?

If not, then what purpose does eual_letter() serve?
 

spinnaker

Joined Oct 29, 2009
7,830
That program is just checking tow condition
true condition : if both character's match then return value of first character

C:
#include<stdio.h>

unsigned char eual_letter (void);
int main(void)
{
        printf(" Return type character is %c \n", eual_letter());
}
unsigned char eual_letter (void)
{
   char character1 = 'x';
   char character2 = 'x';
  if (  character1 ==   character2 )
      return  character1;
    else
      return  character2;
}
Return type character is x

false condition : both character's doesn't match then return value of second character
C:
#include<stdio.h>

unsigned char eual_letter (void);
int main(void)
{
        printf(" Return type character is %c \n", eual_letter());
}
unsigned char eual_letter (void)
{
   char character1 = 'x';
   char character2 = 'y';
  if (  character1 ==   character2 )
      return  character1;
    else
      return  character2;
}
Return type character is y

I know this program doesn't have any specific meaning. I just wrote it to show that I can write


So if my answer look's correct then I also want to make simple question

What are situation where you need a function that doesn't take any arguments and that returns a value of type char in embedded system?

The returned type is not y. The return type is an unsigned char. The type of the variable that you are returning does not match the return type as the function is defined. Your compiler should generate a warning.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
I said to fix character2 at being 'y'. Now try to change character1 to some value that results in anything other than 'y' being returned.

Consider the following function added to your original code:
Sorry for my poor English. I am not sure that I fully understand your question But I think you are asking for following changes
C:
#include<stdio.h>
#define  character2  'y'

char character1 = 'z';
unsigned char eual_letter (void);
unsigned char eual_letter2 (void);
int main(void)
{
        printf(" Return type character is %c \n", eual_letter());
        printf(" Return type character is %c \n", eual_letter2());}

unsigned char eual_letter (void)
{
    if ( character1 == character2 )
      return character1;
    else
      return character2;
}
unsigned char eual_letter2 (void)
{
      return character2;
}
Return type character is y
Return type character is y
 

WBahn

Joined Mar 31, 2012
30,060
I'm trying to get you to comprehend that the function you wrote, eual_letter() does the same thing as the function I provided, eual_letter2(). But notice that mine doesn't care about what the value of character1 is. If the two functions do do the same thing, that means that YOUR function doesn't care about what the value of character1 is, either. So why is it there? One of two possibilities -- it DOESN'T need to be there, in which case your understanding of the problem is probably flawed and you should explore why you thought it should be there, or if DOES matter and the fact that your function's behavior is independent of it indicated that you have a logic error in your program.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
DOES matter and the fact that your function's behavior is independent of it indicated that you have a logic error in your program.
I have modified my program for batter understanding

This program return true if the condition is true else this return false
C:
#include<stdio.h>

#define true  '1'
#define false '0'

#define character2  'z'

char character1;

unsigned char eual_letter (void);

int main(void)
{
        printf(" Return type character is %c \n", eual_letter());
}
unsigned char eual_letter (void)
{

    if ( character1 == character2 )
      return true;

    else
      return false;
}
Does it make any sense for you ?
 

WBahn

Joined Mar 31, 2012
30,060
Nope.

What do you expect your printf() statement to print out if your function returns true? What do you expect it to print out if your function returns false?

Do you understand what the "%c" format specifier does?
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Do you understand what the "%c" format specifier does?
"%c" format specifier print only character. '0' and '1' are the character

What do you expect your printf() statement to print out if your function returns true?
What do you expect it to print out if your function returns false?
if function return true then print the character '1' on screen
if the function return false print the character '0' on screen
 

WBahn

Joined Mar 31, 2012
30,060
"%c" format specifier print only character. '0' and '1' are the character


if function return true then print the character '1' on screen
if the function return false print the character '0' on screen
It's exactly as I suspected. You have no idea what the %c specifier does -- you just insist on thinking that it is going to do what you would like it to do.

The "%c" specifier takes an integer value (typically an 8-bit value that is promoted automatically to an int) and essentially uses that value to look up a graphical image in a font table in order to draw a pretty picture on the screen. The font table that is used most of the time is ASCII. If you want to print the character that looks like a '0' on the screen, then you need to send the printf() function the ASCII code for the character '0', which happens to be 48.

The ASCII values 0 and 1 are control codes and the behavior of printf() when you try to print them as character codes using ASCII is undefined.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
It's exactly as I suspected. You have no idea what the %c specifier does
.
I am going crazy. look for %c specifier https://www.w3schools.in/c-tutorial/format-specifiers/

I used %c specifier because i need to represent character in function. What do you think about '0' and '1' are they not character ?

I have shown you my best program in post #16.

Now I want to know how can you make it batter because I have been tried my all best for it.I want to see your program
 
Last edited:
Top