Thread: User generated functions and interaction between them.

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    2

    User generated functions and interaction between them.

    Hello,

    I was given a task to construct a program that should look like this:

    Code:
    <...>
    <description of fun1>
    <description of fun2>
    main(){
    <giving the program the initial parameters>;
    <fun1(<parameters>);
    }
    "fun1" (function one) of type void must complete the given task and print out the end results whereas "fun2" provides the side results that are used by "fun1".

    I'm struggling to understand how those two functions should interact with each other, especially because the fun1 that requires the results of the fun2 is described before fun2. Also, with a given task, fun2 must send a whole lot of results that fun1 must check and print out whether they meet the conditions. What should be written in order to achieve that?In addition, I would be very grateful if you could tell me how the repeating answers could be eliminated.

    So far I only have this:

    Code:
    #include <stdio.h>
    
    void fun1 (int, int)
    {
        if (the "result of fun2"==S)
            {printf("%d %d", i, k);} /*The problem is, I don't know how "i" and "k" should be transfered to this function*/
        
    }
    
    
    int fun2 (int)
    {
        int i, k;
        for(i=1; i<=S; i++)
        {
            for(k=1; k<=S; k++)
            {
                return i*k
            }
        }
    }
    
    
    
    
    main ()
    {
        int S;
        printf("Enter the area of the rectangle.\n");
        scanf("%d\n", &S);
        fun1(S);    
    }
    The task is to construct a program where a user enters the area of a rectangle and the program prints out all the possible lengths and widths so their product is equal to the given area.

    If you have any questions, I'll do my best.
    Also, sorry if anything's hard to understand. English isn't my mother tongue.

    Have a nice day!
    Last edited by clubroot; 12-18-2016 at 11:27 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Well normally, you would prototype all the functions first, so the whole problem of how 'who calls who' goes away.
    Code:
    // these are prototypes
    void fun1(int area);
    int fun2(int a);
    
    // with prototypes already declared, fun1 can call fun2 even though
    // the compiler hasn't seen the code yet.
    
    void fun1( int area ) {
      // do stuff
      answer = fun2(123);
      // do more stuff
    }
    
    int fun2 ( int a ) {
      // do stuff
      return 42;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    You need to pass i and k by reference to function 2. In C all passing is by value, but passing by reference can be simulated by using pointers.

    In your case you would move the declaration of i and k to function 1. You would change function 2 to int fun2( int, int*,int* ),and the if statement becomes if( fun2( S, &i, &k ) == whatever). This function returns an int and has what is called two output parameters. It's basically telling fun2 where to find and use i and k. Instead of passing values, we are passing addresses of values.
    Last edited by Hobbit; 12-18-2016 at 04:43 PM.

  4. #4
    Registered User
    Join Date
    Dec 2016
    Posts
    2
    Thank you so much!

    What do you think? Is it fundamentally ok?

    Code:
    #include <stdio.h>
    
    
    void fun1 (int S);
    int fun2 (int S , int *i, int *k);
    
    
    void fun1 (int S) 
    {
        int i, k;
        for (i=1; i<=S; i++)
        {
            for (k=1; k<=S, k++)
            {
                if ((fun2(S, &i, &k))==1)
                {
                printf("%d %d\n", i, k);
                }
            }
        }
    }
    
    
    int fun2 (int , int *i, int *k)  
    {
        if (i*k==S)
            return 1;
        else
            return 0;
    }
    
    
    main ()
    {
        int S;
        printf("Enter the area of the rectangle.\n");
        scanf("%d\n", &S);
        fun1(S);
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    It's about right, except there is no need for i,k to be pointers in fun2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    i and k are now pointers, they are addresses.

    So that if statement in fun2 is wrong. To get to a value given a pointer you apply the dereference operator *

    if i is a pointer then *i is the value being pointed at.

  7. #7
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Yes Salem is right. Now that you have moved the loop logic to fun1 you don't need to pass by reference any more and can simply pass by value and let fun2 work on copies. My first suggestion assumed the loop logic was needed in func2 and you wanted the values in func1 to update.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie User Functions help
    By ScottieK in forum C Programming
    Replies: 3
    Last Post: 03-26-2012, 03:04 AM
  2. Replies: 2
    Last Post: 01-04-2008, 06:56 AM
  3. User defined functions
    By alexpos in forum C Programming
    Replies: 2
    Last Post: 10-23-2005, 02:53 PM
  4. New to user defined functions
    By Extropian in forum C Programming
    Replies: 4
    Last Post: 08-15-2005, 10:45 PM
  5. Help! User defined functions
    By AdrenalinFlow in forum C Programming
    Replies: 3
    Last Post: 02-22-2003, 07:36 PM

Tags for this Thread