Thread: Calling a function several times

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Calling a function several times

    Within one function, how does one go about making several function calls to another function, depending on a user given number?

    I tried using a while loop like this:

    Code:
    while(shift > 0){
    
      rotate_right(ch);
    
      shift--;
    }
    But that only calls it once, no matter what number u enter

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    What you need is a recursive function.... a function that calls itself.
    Double Helix STL

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    No I can't do that, part of the exercise is to make a function called rotate_right and one called rotate_left and yet another one called encode, i've made rotate_right and left so far...

    If the number is negative, encode is supposed to call rotate_left, if the number is positive then encode should call rotate_right

    It calls them fine, but it only calls them once, whereas, it needs to call them how many times the user specifies.

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I think If you declare 'shift' as static, then whatever it holds will stay in memory. But it also means the memory isn't freed when the function ends, so it might not be the best possibility.

    [edit]and theres allways globals[/edit]

  5. #5
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by mike_g View Post
    I think If you declare 'shift' as static, then whatever it holds will stay in memory. But it also means the memory isn't freed when the function ends, so it might not be the best possibility.

    [edit]and theres allways globals[/edit]

    Good point, i'll try that, thanks!

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I don't believe this is a good point. The provided snippet is alright as is but it is not possible to tell from it why rotate_right wouldn't get called shift times. Unless rotate_right goes into infinite recursion / loop or terminates the program (it means, never returns), it should work correctly.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Post some more code. This is probably a problem with the value of 'shift' before the loop is even entered.
    Callou collei we'll code the way
    Of prime numbers and pings!

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    As was said, either shift is 1 at the start of the loop or rotate_right() is being stuck somewhere.... although I can think of one more possibility. If shift is global, another function could be messing with it.

  9. #9
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Well here is the code, I can only use encode to call rotate_right, I was doing it for any amount of text, then I found I only have to do it for for 1 character that is entered. Changed the code around a bit, now it just ends after it executes the rotate_right function, I tried several ways for it to return control to the encode function, but I'm not entirely sure how to do that.

    Code:
    #include<stdio.h>
    
    int ch;
    int shift;
    
    int encode(int ch, int shift);
    int rotate_right(int ch);
    int rotate_left(int ch);
    
    int main (int argc, char **argv){
      
      int shift;
      int ch;
      printf("Enter shift: ");
      scanf("&#37;d", &shift);
      
      encode(ch,shift);
      
      return 0;
    }
    
    int encode(int ch, int shift){
      
      while(shift > 0){
        
       rotate_right(ch);
       shift--;
      }
      printf("%c", ch);  
    
      /*while(shift < 0){
        
       rotate_left(ch);
        shift++;
      }*/  
      return 0;
    }    
    
    int rotate_right(int ch){  
      
      ch = getchar();
        if((ch >= 65 && ch <= 89) || (ch >= 97 && ch <= 121)){
          ch = ch + 1;
        }
        else if(ch == 122){
          ch = ch - 25;
        }
        else if(ch == 90){
          ch = ch - 25;
        }  
    
      return (0);
    }

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Firstly get rid of the global variables. You should only declare variables at the scope where you need them. If you have variables with the same name declared at all possible scopes it just adds a whole lot of confusion.

    From the looks of it you have trouble understanding what passing parameters mean and what the return values of functions mean. ch should be input by user in main() (not rotate) and passed to encode. encode has to pass ch on to rotate_function. Both should return the character after shift, and both encode() and main() should use the return value.

    By the way: while loops also act as sort of if-s. The first loop would only run if shift is larger than 0, the second loop only if shift is smaller than 0. After any of these loops shift should be 0.
    Last edited by anon; 08-22-2007 at 12:25 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Function calling woes
    By RedZippo in forum C Programming
    Replies: 6
    Last Post: 01-09-2004, 12:39 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM