Connect with us

Function and their types

Discussion in 'Microcontrollers, Programming and IoT' started by vead, Jan 21, 2018.

Scroll to continue with content
  1. Harald Kapp

    Harald Kapp Moderator Moderator

    11,310
    2,588
    Nov 17, 2011
    What does it mean "cannot execute"? Is there a warning, an error message, ...?

    Completely correct but not a good example. If you have access to the global variable count, there's no need to evaluate the return value of the function. But it will work and is correct, I repeat.

    Calling a subroutine with a return value but no argument is a useful operation e.g. for reading data from a fixed location. For example reading the time from a real time clock could be done by a routine named time(). This routine needs no input parameter (unless you have different clocks or need to tell the routine to return the time in a certain format. The routine time() would read e.g. the current time from a real time clock and return the time e.g. as an unsigned integer meaning seconds from a defined start time (as UNIX does, for example)
     
    vead likes this.
  2. vead

    vead

    473
    14
    Nov 27, 2011
    I just type in cmd
    gcc -o hello hello.c
    hello
    it show only message "The system cannot execute the specified program"

    it deosn't show any error or warnings
     
  3. Harald Kapp

    Harald Kapp Moderator Moderator

    11,310
    2,588
    Nov 17, 2011
  4. vead

    vead

    473
    14
    Nov 27, 2011
    Yes I have compiled every program and then I posted result. generally I see this error when something is wrong in program
     
  5. vead

    vead

    473
    14
    Nov 27, 2011
    look at following program's. I have not compiled all program I am just showing you my understanding

    Function with no return value and no argument
    This program doesn't return any value and doesn't pass any argument.
    Code:
    #include<stdio.h>
    
    void number(void)
    {
        unsigned int value = 2;
        printf(" Print number : %d \n ",value);
    }  
    
    int main (void)
    {
        number();
     
       return 0;
    }
    Function with no return value and argument

    this function doesn't return any value but pass argument 4
    Code:
    #include<stdio.h>
    
    void number(unsigned int value)
    {
        printf(" Print number : %d \n ",value);
    }  
    
    int main (void)
    {
        number(4);
     
       return 0;
    }
    Function with return value and no argument
    this function return 1 but doesn't pass any argument
    Code:
    #include<stdio.h>
    
    int number(void)
    {
       unsigned int value = 3;
        printf(" Print number : %d \n ",value);
     
       return 1;
    }  
    
    int main (void)
    {
       number();
     
       return 0;
    }
    Function with return value and argument
    this function return 1 and pass argument 3
    Code:
    #include<stdio.h>
    
    int number(unsigned int value)
    {
        printf(" Print number : %d \n ",value);
     
        return 1;
    }  
    
    int main (void)
    {
        number(3);
     
       return 0;
    }
    

    does it make any sense ?
     
  6. BobK

    BobK

    7,682
    1,688
    Jan 5, 2010
    The library function rand() does exactly that. It starts the random number sequence with a seed then, esch time called, computes a new random number and returns it, while keeping the global variable hidden.

    Also it is common (and recommended) to use accessor functions in C++ to return the value of private variables without giving full access to those variables.

    Bob
     
    Harald Kapp likes this.
  7. vead

    vead

    473
    14
    Nov 27, 2011
    Hello bob

    what do you think about my last post. does it make any sense for you?
     
  8. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,482
    2,830
    Jan 21, 2010
    Add a "-v" after "gcc" (gcc -v -o hello hello.c). then post the entire contents of what is displayed (including your command line).

    You cannot progress simply ignoring something when you have an error. @BobK has given you an excellent example and you need to understand it.

    If you decide it's too hard and move on, you will get less and less help...

    (I prefer NOT having to read the links given to you. Take this advice and READ what people are telling you. If you don't understand, or can't find any help, ask for clarification. DO NOT JUST IGNORE IT)

    edit: I also recommend you call your programs something like "hello.cpp" and use the "g++" compiler instead of "gcc". For 99% of cases things will be exactly the same. Once you start using C++ constructs, things will behave better this way. That is, unless you have some reason to limit yourself to plain vanilla C.
     
    Last edited: Jan 23, 2018
  9. vead

    vead

    473
    14
    Nov 27, 2011
    Please take look at below quote message
    I didn't move on another topic. I took simple example for better understanding because I was trying from long time. so I thought I should take simple example.

    It's fine I will be stay with example given by bob


    I gone through that link's and still trying to fix the issues


    I have doubt when I write c program then I run program using gcc -o hello hello.c and when I write c++ program then I run program using g++ -o hello hello.cpp
    C:\Users\Embedded\Desktop>g++ -o hello hello.cpp

    C:\Users\Embedded\Desktop>exm
    The system cannot execute the specified program.

    Is it possible to run c program from c++ compiler like you're saying

    I am not ignoring any advice. i am just trying my level best
     
    Last edited: Jan 23, 2018
  10. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,482
    2,830
    Jan 21, 2010
    Ok, I don't see the error message.

    What is the size of hello.exe, and what happens if you try to run it?

    I'm using my phone now, so it's hard to read through the details of that log. I'll try to do it later today (in maybe 10 to 12 hours time).

    If I get there time, I'll try the same example on a Unix machine at work. I'll let you know if I find anything.
     
  11. vead

    vead

    473
    14
    Nov 27, 2011
    I think I found out the problem it's about the initialization of variable.

    bob was assigning count = 0; and I am assigning count = 1;

    Code:
    #include<stdio.h>
    
    int count = 1;
    int counter(void)
    {
        count++;
        return count;
    }
    
    int main()
    {
        while (1)
        {
            printf("%d\n", counter());
        }
    
    return 0;
    }
    upload_2018-1-23_11-59-40.png

    another program
    Code:
    #include<stdio.h>
    
    int count = 0;
    int counter(void)
    {
        count++;
        return count;
    }
    
    int main()
    {
        printf("%d\n", counter());
        
        return 0;
    }
    function return count number 1
     
    Last edited: Jan 23, 2018
  12. Harald Kapp

    Harald Kapp Moderator Moderator

    11,310
    2,588
    Nov 17, 2011
    An interesting and unexpected find.
    Would you care to test the program from post #18 with the following modification?
    Code:
    #include<stdio.h>
    
    volatile int count = 0;
    The addition of volatile tells the compiler that this variable may change (unexpectedly in the view of the compiler) and therefore may not be optimized. I don't see a reason why the compiler should optimize your program until it doesn't work, but i'd like to see you give it a try.
     
  13. vead

    vead

    473
    14
    Nov 27, 2011
    Hello
    I did it. but it give same message " The system cannot execute the specified program."
     
  14. Harald Kapp

    Harald Kapp Moderator Moderator

    11,310
    2,588
    Nov 17, 2011
    I'm out of my wits then.
     
  15. vead

    vead

    473
    14
    Nov 27, 2011
    What should I do next, Do I read another topic in programming or try to become good in this topic only. I think now I understood function and their types .

    Actually I want start work with LPC arm controller. I have downloaded datasheet but I have some doubt's. should I start with new thread
     
  16. Harald Kapp

    Harald Kapp Moderator Moderator

    11,310
    2,588
    Nov 17, 2011
    Yes, please.
     
  17. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,482
    2,830
    Jan 21, 2010
    You could look at how you can pass parameters by value and by reference.

    This will require a basic understanding of pointers
     
  18. vead

    vead

    473
    14
    Nov 27, 2011
    call by value
    Code:
    #include<stdio.h>
     
    void function(int a);
     
    int main(void)
    {
     int a=10;
     
     printf(" Before calling a = %d \n",a);
     
     function(a);
     
     printf("After calling a=%d \n",a);
     
     return 0;
    }
     
    void function(int a)
    {
     a=15;
    }
    
    Before calling a = 10
    After calling a=10
    I have noticed value of a doesn't change after calling the function. it remain same as previous value.

    call by address
    Code:
    #include<stdio.h>
     
    void function(int *a);
     
    int main()
    {
     int a=10;
     
     printf("Before calling a = %d \n",a);
     
     function(&a);
     
     printf("After calling a = %d \n",a);
     
     return 0;
    }
     
    void function(int *a)
    {
     *a=15;
    }
    Before calling a = 10
    After calling a = 15

    I have noticed value of a change after calling the function
     
  19. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,482
    2,830
    Jan 21, 2010
    Which ones can you call with a constant value?

    Do you understand what you are passing when you say "void function foo(int *a);"?

    What does the * mean. Likewise what does the & in &a mean?
     
  20. vead

    vead

    473
    14
    Nov 27, 2011
    This one because it doesn't change the value of actual argument.
    Code:
    #include<stdio.h>
     
    void function(int a); 
    int main(void)
    {
     int a=10;
     printf(" Before calling a = %d \n",a); 
     function(a);
     printf("After calling a=%d \n",a);
     return 0;
    }
    void function(int a)
    {
     a=15;
    }
    I am passing address of variable a

    & is a operator means “address of”.

    Declaration of Pointer

    data_type* pointer_variable_name;

    int* pointer;
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-