-6

In this Program, I am unable to understand the declaration of the "sum" function.

Please explain what is happening while calling the sum function and while declaring the function.

#include<iostream.h>
#include <conio.h>

int sum(int(*)(int),int);
int square(int);
int cube(int);

void main()
{
    clrscr();
    cout<<sum(square,4)<<endl;
    cout<<sum(cube,4)<<endl;
    getch();
}

int sum(int(*ptr)(int k),int n)
{
     int s=0;
     for(int i=1;i<=n;i++)
     {
         s+=(*ptr)(i);
     }
     return s;
}

int square(int k)
{
int sq;
sq=k*k;
return k*k;
}

int cube(int k) 
{
    return k*k*k;
}
NiRaj Wagh
  • 33
  • 1
  • 6
  • 1
    Aside: please move the 3 function prototypes out of main, to below the `#include`s. – Weather Vane Oct 14 '17 at 19:00
  • Why declaring a function prototype inside a function body? – Raindrop7 Oct 14 '17 at 19:00
  • 1
    Mandatory critical comment on use of conio.h, iostream.h and void main. To OP, wherever you are learning this stuff from, learn it from somewhere else. –  Oct 14 '17 at 19:02
  • The whole thing is a mish-mash. Why use `conio.h`? Why the `void main()`? Why the different ways of implementing `square` and `cube`? Why compute `sq=k*k;` and then `return k*k;` ignoring `sq`? – Weather Vane Oct 14 '17 at 19:04

3 Answers3

1

The parameter is a function pointer - you should read here for an introduction, and heres a relevant SO post.

The idea is that you can pass around a function as a value - so you can make other general functions that you can give a specific function to in order to change the effect. An example would be map from functional programming.

Specifically in your case, the sum function takes this function pointer in order to sum a function of the values in the list given to it, rather than just the values themselves. This is demonstrated by passing it eg. a pointer to the square function, which will make it sum squares of the values given to sum.

hnefatl
  • 5,860
  • 2
  • 27
  • 49
0
// The method sum 
// First argument: a pointer to a function which accepts one integer 
// as argument and returns an integer
// Second argument: an integer
int sum(int(*)(int),int);

That is why when you call sum in this line:

cout<<sum(square,4)<<endl;

You pass square as the first argument and since square is like this:

int square(int k)
{
}

it satisfies the call because it is a function which accepts one integer as argument and returns int.

Then inside the sum method, it calls the function which you passed in as the first argument like this (please read my comments inline for clarity):

int sum(int(*ptr)(int k),int n)
{
     int s=0;
     for(int i=1;i<=n;i++)
     {
         // Calls the function pointer (square in this case) and sends i to
         // it as argument. It then takes the return value and adds it to s.
         s+=(*ptr)(i); 
     }
     return s;
}

Here are the different parts:

enter image description here

Here is another example to help you:

// fcnPtr is a pointer to a function that takes no arguments and returns an integer
int (*fcnPtr)();
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
0

This is the reason why some people does not like pointers in C and C++. See my explanation below in your program:-

#include<iostream.h>
# include <conio.h>
void main()
{
    clrscr();
    int sum(int(*)(int),int);// Here first parameter in `sum` is a pointer to a function which return type is int.  `int(*)(int)` here int(*) is function pointer and (int) is its parameter type. 
    int square(int);
    int cube(int);
    cout<<sum(square,4)<<endl;// Now you are passing address of function  `square` as first parameter and its parameter 4, like square(4)
    cout<<sum(cube,4)<<endl; // Here you are passing address of function `cube` as parameter and 4 as parameter to cube function
    getch();
}

int sum(int(*ptr)(int k),int n)
{
     int s=0;
     for(int i=1;i<=n;i++)
     {
         s+=(*ptr)(i); //This `ptr` is nothing but the address of function passed to sum
    //hence it execute like below
    //When you are calling function `sum` with function pointer `square` as first parameter  
        //it execute like `s = s + square(i);`
        //Now at the second call to `sum` it execute like `s = s + cube(i);`
     }
     return s;
}
Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17