Thread: is it ok to write code like this?

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    18

    is it ok to write code like this?

    Code:
    #include <stdio.h>
    
    
    
    
    float eur2nok(float euro);
    
    
    int main(){
    
    
        float euro;
        float nok;
    
    
        printf("les inn euro: ");
        scanf("%f" , &euro);
    
    
        nok=eur2nok(euro);
        
        printf("%.2f euro = %.2f nok\n", euro, nok);
        
        return 0;
        
    }
    
    
    float eur2nok(float euro){
    
    
        float nok;
        
        nok=euro*9.66;
        
        return nok;
        
    }
    I'm declaring eruo and nok in both functions and sending euro from main into euro in second function. Something tells me this is wrong and that I should do it another way.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    551
    Yep. It is fine! And a good practice to isolate functionalities this way.
    The fact you are using the same names for local variables in different functions isn't of any concern.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    18
    Quote Originally Posted by flp1969 View Post
    Yep. It is fine! And a good practice to isolate functionalities this way.
    The fact you are using the same names for local variables in different functions isn't of any concern.
    Good to hear! Is this the standard way of doing it even when writing bigger code that has many functions?

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    551
    Quote Originally Posted by mangekyou View Post
    Good to hear! Is this the standard way of doing it even when writing bigger code that has many functions?
    Not the "standard", but it is a very good practice... If your code uses a function only one time in the same .c file, it is recommended you declare it as "static". This way the compiler has a chance of making the function "inline" automatically and don't export to linker. Example:

    Code:
    // Make it visible only for this file...
    static int f(int a, int b);
    
    // extern by default.
    int my_exported_func( int a, int b )
    { return f(a,b); } // same as 'return a+b;'
    
    // This will be inline and nobody else will know about it!
    int f(int a, int b) { return a+b; }
    If you don't declare f() as static, the compiler will create inline code for the call, but will create and export the function as well.

  5. #5
    I'm a computer snoopfrogg's Avatar
    Join Date
    Feb 2019
    Posts
    29
    Quote Originally Posted by flp1969 View Post
    Not the "standard", but it is a very good practice... If your code uses a function only one time in the same .c file, it is recommended you declare it as "static". This way the compiler has a chance of making the function "inline" automatically and don't export to linker. Example:

    Code:
    // Make it visible only for this file...
    static int f(int a, int b);
    
    // extern by default.
    int my_exported_func( int a, int b )
    { return f(a,b); } // same as 'return a+b;'
    
    // This will be inline and nobody else will know about it!
    int f(int a, int b) { return a+b; }
    If you don't declare f() as static, the compiler will create inline code for the call, but will create and export the function as well.
    Are inline functions always recursive?
    "One of the best programming skills you can have is knowing when to walk away for awhile."

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,254
    flp1969's example doesn't involve recursion.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    18
    Quote Originally Posted by flp1969 View Post
    Not the "standard", but it is a very good practice... If your code uses a function only one time in the same .c file, it is recommended you declare it as "static". This way the compiler has a chance of making the function "inline" automatically and don't export to linker. Example:

    Code:
    // Make it visible only for this file...
    static int f(int a, int b);
    
    // extern by default.
    int my_exported_func( int a, int b )
    { return f(a,b); } // same as 'return a+b;'
    
    // This will be inline and nobody else will know about it!
    int f(int a, int b) { return a+b; }
    If you don't declare f() as static, the compiler will create inline code for the call, but will create and export the function as well.
    Thanks. I need to look more into it, functions are difficult

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    If your code uses a function only one time in the same .c file, it is recommended you declare it as "static". This way the compiler has a chance of making the function "inline" automatically and don't export to linker
    That sounds very implementation dependent.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,254
    Well, if the function is a helper function rather than part of a library interface, you should declare it static to avoid name collision, even if you use it more than once, and while it's true that the precise mechanism is implementation dependent, that would mean no exporting of the function. I don't think static linkage is likely to have any effect on a compiler's decision to inline, other than the fact that a function with static linkage would have its definition known and therefore be a candidate for inlining.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Feb 2019
    Posts
    551
    Quote Originally Posted by Click_here View Post
    That sounds very implementation dependent.
    I said: "This way the compiler has a chance of making the function 'inline'...". I didn't say it will...
    Yes. Is implementation dependent and the compiler can opt to make the routine not inline...

  11. #11
    I'm a computer snoopfrogg's Avatar
    Join Date
    Feb 2019
    Posts
    29
    Quote Originally Posted by laserlight View Post
    flp1969's example doesn't involve recursion.
    I just realized the function doesn't call itself... lol, my b.
    "One of the best programming skills you can have is knowing when to walk away for awhile."

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,254
    Furthermore, except for tail recursion optimisation, recursive functions cannot be inlined... not unless you have a compiler that is able to turn general recursion into iteration by the use of an explicit stack, but that is beyond both the current capability and the mandate of compilers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program that can write its own code in C++
    By Vacrin in forum C++ Programming
    Replies: 23
    Last Post: 10-17-2012, 09:31 AM
  2. What are you using to write your code?
    By Sjvlsdsd in forum General Discussions
    Replies: 21
    Last Post: 07-23-2011, 07:20 AM
  3. I don't want to write a code I don't understand ..!!
    By bchaib in forum C Programming
    Replies: 28
    Last Post: 12-19-2007, 04:29 AM
  4. How do I write more efficient code?
    By minesweeper in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 11:08 AM

Tags for this Thread