Thread: beginner function questions

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    35

    Question beginner function questions

    Hello all:
    I am writing a program for a course assignment. This is the first time I have been exposed to functions in C. Therefore; I am confused about a few things. But here is my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int isPrime(int num)
    {
        int i,mods;
        for(i=1;i<=num;i++)
                          {
                              int mod;
                              mod = num &#37; i;
                              if(mod == 0)
                                     { mods = mods+1; }
                              }
        if(mods==2)
                  {
                              return 1;
                              }
        else
            {
                              return 0;
                              }  
    }
    
    int main(void)
        {
                  int inputNum;
                  scanf("%d",&inputNum);
                  if(isPrime(&inputNum) == 1)
                                       { printf("yes"); }
                  else { printf("no"); }
    }
    This code cannot compile; it gives an error about trying to convert int* to int when i call the isPrime() function with the &inputNum integer.
    Is this something to do with pointers? I do not believe we need to use pointers in this program or... do i?
    Also; for functions; can I return true/false inside of 0/1?
    Or can I use the function to update a variable in main? For ex: I have a number i got from the user in main; but I want to manipulate it with a function and return it. How can I use a function to do that?

    Thanks in advanced!!!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    isPrime(inputNum)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First, the code is extremely confusing. I urge to to try and fix your indentation. Refer to http://cpwiki.sf.net/Indentation to learn some more about it.
    And secondly, you trying to pass the address of inputNum to a function that expects an int, not int* (pointer to int).
    With your code, you don't need a pointer, so vart's suggestion is most likely the way to go.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    As to the rest, you would need to pass the address of the variable to a function, if you want the function to be able to modify it. (Viz: printf doesn't do anything, so it just takes the variable; scanf needs to update the variable, so you have to pass it &var.)

    That means that in your function header, you wouldn't want num to be an int, but a pointer-to-int:
    Code:
    void do_something(int *num);
    Then any changes made via indirection (i.e., *num = 6) would be seen in the calling program.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Aha I get it. Sorry for the newb questions; but thanks for helping!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  2. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM