Thread: passing function to a function

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    30

    passing function to a function

    I have written a function which reads Data from a file and calculates the distance between each point.
    Code:
    int read(float Wert1, float Wert2, char *filename);
    int read(float Wert1,float Wert2, char *filename)
    {
    
    
    FILE *infile;
    infile = fopen(filename,"r");
    
    
    if (infile==NULL)
        {perror("Error:No file found");
        }
    int i;
    float x0,x1,quadrx,y0,y1,quadry,betr;
    for(i=0;fscanf(infile,"%f %f",&Wert1,&Wert2)!=EOF;i++)
        {
        
            
            if(i>0)
            {
            x0 = x1-Wert1;
            quadrx = x0*x0;
            y0 = y1-Wert2;
            quadry = y0*y0;
            betr = sqrt(quadrx+quadry);
            printf("X1-Wert: %f\n X2-Wert: %f\n Y1-Wert: %f\n Y2-Wert %f\n",x1,Wert1,y1,Wert2);    
            printf("Abstand: %f\n",betr); 
            }
            else(printf("Anfangswert X1 %lf\n Anfangswert Y1 %lf\n",Wert1,Wert2));
            
            x1 = Wert1;        
            y1 = Wert2;
        }
    int size = i;
    return size ;
    }
    I now want to write a different function which needs the distance. Since I used floating numbers instead of an array (which was on purpose) I now want to pass my function read to another function so that all values can be used there. Is there any way of doing that in c?

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    all programming languages that I know have the capability to pass a function into it.
    Code:
    int myFristFunction(int a, int b)
    {
       return ( a+b);
    }
    
    int mySecondFunction(int a, int b)
    {
       return (a * b);
    }
    
    int myThridFunction( int a, int b)
    {
    
      return ( a / b);
    
    }
    
    //call them
    int answer;
    answer =  myThridFunction(myFirstFunction(2,3), mySecondFunction(4,5));

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    30
    So myThirdFunction would return 5/20 in your example right?

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Gamma_123 View Post
    So myThirdFunction would return 5/20 in your example right?
    it is not the matter of what it will return, it is the showing of passing the return values from other functions into another function to replace data needed by another means. in other words instead of doing it this way.
    Code:
    int myFristFunction(int a, int b)
    
    {
       return ( a+b);
    }
     
    int mySecondFunction(int a, int b)
    
    {
    
       return (a * b);
    }
     
    int myThridFunction( int a, int b)
    {
     
      return ( a / b);
     
    }
     
    //call them
    int answer1 = myFristFunction(3,40);
    int answer2 = mySecondFunction(32,22);
    int answer3 =  myThridFunction(answer1, answer2);
    like you asked, "
    passing function to a function


    because that is why it is done.
    Last edited by userxbw; 11-25-2017 at 01:22 PM.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    here is another way you can use your functions as I see the function you posted passes in floats, and returns int. so I do not know what you're planing on doing with these.
    Code:
     x1 = Wert1;         y1 = Wert2;
    a little something to show you how to pass more than one value back out of your functions by other means then retrun which can only return one value.

    Code:
    #include <stdio.h>
    
    
    void passingValuesIntoME(double *a, double *b)
    {
         
        *a *= 3;
        *b *= 2;
    }
    
    double gettingResults(double a, double b)
    {
            return (a * b);
    }
    
    int main(void)
    {
        
    double num1 = 2.3, num2 = 5.5, answer; 
    
    
    printf("before function num1 = %.2f num2 = %.2f\n", num1,num2);
    //returns value via the parameters 
    passingValuesIntoME(&num1,&num2);
    
    printf("after function num1 = %.2f num2 = %.2f\n", num1,num2);
    
    //pass the returned values into another function
    answer = gettingResults(num1,num2);
    
    printf("gettingResults returned %.2f\n", answer);
    
    return 0;
        
    }
    the results are this
    Code:
    userx@slackwhere:~/bin
    $ ./passing_values_functions
    before function num1 = 2.30 num2 = 5.50
    after function num1 = 6.90 num2 = 11.00
    gettingResults returned 75.90
    Last edited by userxbw; 11-25-2017 at 01:19 PM.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Example of what passing a function normally means in C; should normally only be learned/taught after covering pointers.
    qsort(3): sort array - Linux man page

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Nov 2017
    Posts
    30
    Thanks userxbw, I think I got it now!

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Lets hope so... enjoy !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing ponter to function to another function
    By dan3460 in forum C Programming
    Replies: 2
    Last Post: 01-20-2017, 08:46 PM
  2. Passing the address of a function to a function.
    By SqueezyPeas in forum C Programming
    Replies: 2
    Last Post: 09-17-2015, 07:42 AM
  3. Passing variable from function - main - function
    By ulti-killer in forum C Programming
    Replies: 2
    Last Post: 11-01-2012, 12:14 PM
  4. help with passing a pointer to a function to another function
    By csepraveenkumar in forum C Programming
    Replies: 5
    Last Post: 03-20-2011, 01:13 PM
  5. Replies: 1
    Last Post: 09-04-2007, 10:00 PM

Tags for this Thread