<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Recent changes to 03_Functions</title><link>https://sourceforge.net/p/cppforgreatness/wiki/03_Functions/</link><description>Recent changes to 03_Functions</description><atom:link href="https://sourceforge.net/p/cppforgreatness/wiki/03_Functions/feed" rel="self"/><language>en</language><lastBuildDate>Fri, 02 Jan 2015 22:46:20 -0000</lastBuildDate><atom:link href="https://sourceforge.net/p/cppforgreatness/wiki/03_Functions/feed" rel="self" type="application/rss+xml"/><item><title>03_Functions modified by Nitin Deshpande</title><link>https://sourceforge.net/p/cppforgreatness/wiki/03_Functions/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v37
+++ v38
@@ -361,10 +361,9 @@

 Step 2. Declare a pointer to a function. How?

-*Solution: A pointer to a function has to specify the same information as the function
+[48] A pointer to a function has to specify the same information as the function
 prototype conveys the compiler - declaration has to comply with the function
-return value and function's signature.*
-
+return value and function's signature.
 Eg: 

    T foo(T argList); // Prototype
@@ -376,20 +375,39 @@
    T
    */

-The reason behind having a pair of parentheses around *ptrfoo: i. Without
+[49] The reason behind having a pair of parentheses around *ptrfoo: i. Without
 parentheses, the function would return a pointer to type T.

    T (*ptrfoo) (T argList); // Pointer to a function
    T *ptrfoo (T argList);   // Returns a pointer of type T

-ii. Parentheses have higher precedence than asterisk.
+ii. Parentheses have higher precedence than *.
+
+[50] A function's prototype that accepts the address of the function would look
+like the following.
+
+   T first (U); // Prototype of the first function.
+   T (*pf) (U); // pointer to function first.
+   // Prototype of a function that invokes the first function.
+   // Uses the pointer to function.
+   V second (S arg, T (*pf) (U));
+   /* 
+   Now for the calling part.
+   */
+   // Assign pointer to address of the matching function
+   pf=first; // pf now points to first() function.
+   // Pass the first's address to the second function.
+   second('argument',pf); // function call
+
+        NOTE: (*pf) plays the same role as first. So, first(arg) is equivalent to
+        (*pf)(arg).[bingo!]

 Step 3. Pointer to a function to invoke the function.

-        // NOTE: (*pf) plays the same role as first. So, first(arg) is equivalent to
-        // (*pf)(arg).
-        // --------------------------------------------------------------------------
-   first(arg); // pf is a pointer to the function, (*pf)() is the function.
+       // NOTE: (*pf) plays the same role as first. So, first(arg) is equivalent to
+       // (*pf)(arg).
+       // --------------------------------------------------------------------------
+       first(arg); // pf is a pointer to the function, (*pf)() is the function.
    (*pf)(arg); // both return the same result.
    T first (U); // Prototype of the first function.
    T (*pf) (U); // pointer to function first.
@@ -401,10 +419,10 @@
 [recall]If you want a pointer pointing to a function, then replace the functionName
 with (*ptrTo_functionName).

-In the function signature (argument list), T arg[] can be reduced to T [], and
+[51] In the function signature (argument list), T arg[] can be reduced to T [], and
 can be reduced to T *.

-   // Prototypes, ALL of them perform the same function - return a
+   // Prototypes, all of them perform the same function - return a
    // pointer to type T.
    T* fun_1(T arr[],int n); // is...
    // equivalent to...
@@ -412,13 +430,78 @@
    // equivalent to...
    T* fun_3(T*,int):

-Now, to point a pointer to the functions above, see [recall].
+[52] Now, to point a pointer to the functions above, see [recall].

    T* (*ptrToFunction)(T*,int);

-Or you could use,*auto (type deduction)* &amp;lt;http: msdn.microsoft.com="" en-IN="" library="" dd293667.aspx=""&amp;gt;
-here &amp;lt;http: en.cppreference.com="" w="" cpp="" language="" auto=""&amp;gt; or
-here &amp;lt;http: herbsutter.com="" 2013="" 06="" 07="" gotw-92-solution-auto-variables-part-1=""/&amp;gt;
+[53] Or you could use,auto&amp;lt;http: msdn.microsoft.com="" en-IN="" library="" dd293667.aspx=""&amp;gt;
+here&amp;lt;http: en.cppreference.com="" w="" cpp="" language="" auto=""&amp;gt; or
+here&amp;lt;http: herbsutter.com="" 2013="" 06="" 07="" gotw-92-solution-auto-variables-part-1=""/&amp;gt;

    auto ptrToFunction=fun_1;
-
+   /* Recall, the function name minus the
+      parentheses returns the address of the function. 
+      So, ptrToFunction stores the address, hence becomes
+      a pointer to the function fun_1..*/
+
+[54] In C++11, *auto* is a type 'deducer'. It sends directives to the compiler to deduce the
+variable type from an initialisation expression. I believe in the previous
+standards, auto was a storage class specifier[?].
+
+   T* pd;
+   auto x=pd; // x is now T*
+   auto* y=pd // y: T*
+   int g();
+   auto pf=g; // pf: pointer to function g
+   const auto&amp;amp; rf=g(); // rf:const int&amp;amp;
+
+[55] *auto* cannot deduce array types.
+   
+   T someArray[2];
+   auto anotherArray[2]=someArray; // Returns an error.
+   /*
+   someArray evaluates to a pointer, which does not match the array
+   type.
+   */
+
+[56] *auto* cannot be used in function parameters.
+   
+   int someFunction(auto x=4.00);
+   // parameter cannot be declared 'auto'
+
+[57] When you’re new to auto, the key thing to remember is that you really are declaring your own new local variable. 
+i.e., What is on the left is my new variable, and what is on the right is just its initial value: 
+[Source:http://herbsutter.com/2013/06/07/gotw-92-solution-auto-variables-part-1/]
+
+   auto my_new_variable = its_initial_value;
+
+Another way to look at it is that whenever you see the keyword *auto*, it
+means that you take the type on the RHS, but strip of top-level
+const/volatile.
+
+   int val=10;
+   auto a=val; // a: int
+   auto&amp;amp; b=val;// b: int&amp;amp;
+   const auto c=val; // const int
+   const auto&amp;amp; d=val; // const int&amp;amp;
+   // -----------------------------
+   // Remember to remove the top level const or &amp;amp;  or &amp;amp;&amp;amp; to obtain
+   // the basic type.
+   int&amp;amp; ir=val;
+   auto e=ir; // e: int, ir is just another name for val.
+   int* ip=&amp;amp;val; // ip is a pointer to int.
+   auto f=ip; // f is a int*.
+   const int ci=val;
+   auto g=ci; // g is int, and not const int. g is a separate variable.
+   const int&amp;amp; cir=val;
+   auto h=cir; // h is int.
+   const int* cip= &amp;amp;val;
+   auto i=val; // i is const int*
+   // Above is not a top level const.
+   // cip is not const, rather *cip is const. (In other words, it is a
+   // pointer to const int.)
+   int* const ipc=&amp;amp;val; // const pointer to int
+   auto j=ipc; // j:int*, and not const. 
+   const int* const cipc=&amp;amp;val; // const pointer to const int.
+   auto k=cipc; // k:const int*
+
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Nitin Deshpande</dc:creator><pubDate>Fri, 02 Jan 2015 22:46:20 -0000</pubDate><guid>https://sourceforge.net04d173867d70480ca35c40d560201880f33a52eb</guid></item><item><title>03_Functions modified by Nitin Deshpande</title><link>https://sourceforge.net/p/cppforgreatness/wiki/03_Functions/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v36
+++ v37
@@ -416,7 +416,7 @@

    T* (*ptrToFunction)(T*,int);

-Or you could use,auto &amp;lt;http: msdn.microsoft.com="" en-IN="" library="" dd293667.aspx=""&amp;gt;
+Or you could use,*auto (type deduction)* &amp;lt;http: msdn.microsoft.com="" en-IN="" library="" dd293667.aspx=""&amp;gt;
 here &amp;lt;http: en.cppreference.com="" w="" cpp="" language="" auto=""&amp;gt; or
 here &amp;lt;http: herbsutter.com="" 2013="" 06="" 07="" gotw-92-solution-auto-variables-part-1=""/&amp;gt;

&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Nitin Deshpande</dc:creator><pubDate>Fri, 02 Jan 2015 14:34:24 -0000</pubDate><guid>https://sourceforge.net8c0bf33e06deecc4a861c3762519401a09fd03d6</guid></item><item><title>03_Functions modified by Nitin Deshpande</title><link>https://sourceforge.net/p/cppforgreatness/wiki/03_Functions/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v35
+++ v36
@@ -404,7 +404,7 @@
 In the function signature (argument list), T arg[] can be reduced to T [], and
 can be reduced to T *.

-   // Prototypes, all of them perform the same function - return a
+   // Prototypes, ALL of them perform the same function - return a
    // pointer to type T.
    T* fun_1(T arr[],int n); // is...
    // equivalent to...
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Nitin Deshpande</dc:creator><pubDate>Fri, 02 Jan 2015 14:33:17 -0000</pubDate><guid>https://sourceforge.netad392a1242dc7c52a3e9424b7273976c2b3adebe</guid></item><item><title>03_Functions modified by Nitin Deshpande</title><link>https://sourceforge.net/p/cppforgreatness/wiki/03_Functions/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Nitin Deshpande</dc:creator><pubDate>Fri, 02 Jan 2015 14:32:26 -0000</pubDate><guid>https://sourceforge.net17827845dac9d66f99bfb3ed9783a7368562a484</guid></item><item><title>03_Functions modified by Nitin Deshpande</title><link>https://sourceforge.net/p/cppforgreatness/wiki/03_Functions/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v33
+++ v34
@@ -396,3 +396,29 @@
    pf=first; // pf now points to first() function.
    T varA=first(arg);
    T varB=(*pf)(arg); // varA and varB both output the same result.
+
+**Variations on the Theme of Function Pointers**
+[recall]If you want a pointer pointing to a function, then replace the functionName
+with (*ptrTo_functionName).
+
+In the function signature (argument list), T arg[] can be reduced to T [], and
+can be reduced to T *.
+
+   // Prototypes, all of them perform the same function - return a
+   // pointer to type T.
+   T* fun_1(T arr[],int n); // is...
+   // equivalent to...
+   T* fun_2(T [],int); // is... 
+   // equivalent to...
+   T* fun_3(T*,int):
+
+Now, to point a pointer to the functions above, see [recall].
+   
+   T* (*ptrToFunction)(T*,int);
+
+Or you could use,auto &amp;lt;http: msdn.microsoft.com="" en-IN="" library="" dd293667.aspx=""&amp;gt;
+here &amp;lt;http: en.cppreference.com="" w="" cpp="" language="" auto=""&amp;gt; or
+here &amp;lt;http: herbsutter.com="" 2013="" 06="" 07="" gotw-92-solution-auto-variables-part-1=""/&amp;gt;
+
+   auto ptrToFunction=fun_1;
+
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Nitin Deshpande</dc:creator><pubDate>Fri, 02 Jan 2015 14:31:41 -0000</pubDate><guid>https://sourceforge.net536b92eca0e2aa27f922993dccd92ce174de59e4</guid></item><item><title>03_Functions modified by Nitin Deshpande</title><link>https://sourceforge.net/p/cppforgreatness/wiki/03_Functions/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v32
+++ v33
@@ -382,15 +382,17 @@
    T (*ptrfoo) (T argList); // Pointer to a function
    T *ptrfoo (T argList);   // Returns a pointer of type T

-ii. Parentheses have higher precedence than *(asterisk).
+ii. Parentheses have higher precedence than asterisk.

 Step 3. Pointer to a function to invoke the function.

-NOTE: (*pf) plays the same role as first. So, first(arg) is equivalent to
-(*pf)(arg).
-
-   first(arg);
+        // NOTE: (*pf) plays the same role as first. So, first(arg) is equivalent to
+        // (*pf)(arg).
+        // --------------------------------------------------------------------------
+   first(arg); // pf is a pointer to the function, (*pf)() is the function.
    (*pf)(arg); // both return the same result.
-
-3. Pointer to a function to invoke the function.
-pf is a pointer to the function, (*pf)() is the function.
+   T first (U); // Prototype of the first function.
+   T (*pf) (U); // pointer to function first.
+   pf=first; // pf now points to first() function.
+   T varA=first(arg);
+   T varB=(*pf)(arg); // varA and varB both output the same result.
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Nitin Deshpande</dc:creator><pubDate>Fri, 02 Jan 2015 02:22:35 -0000</pubDate><guid>https://sourceforge.netcdca248db42cddef76ccc8936ce3bfc3aaefa6d5</guid></item><item><title>03_Functions modified by Nitin Deshpande</title><link>https://sourceforge.net/p/cppforgreatness/wiki/03_Functions/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v31
+++ v32
@@ -390,7 +390,7 @@
 (*pf)(arg).

    first(arg);
-   (*pf)(arg); // both returns the same result.
+   (*pf)(arg); // both return the same result.

 3. Pointer to a function to invoke the function.
 pf is a pointer to the function, (*pf)() is the function.
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Nitin Deshpande</dc:creator><pubDate>Wed, 31 Dec 2014 16:41:28 -0000</pubDate><guid>https://sourceforge.net4c97165564f3019fee5afa6f2f492f94ffd22fcc</guid></item><item><title>03_Functions modified by Nitin Deshpande</title><link>https://sourceforge.net/p/cppforgreatness/wiki/03_Functions/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v30
+++ v31
@@ -385,3 +385,12 @@
 ii. Parentheses have higher precedence than *(asterisk).

 Step 3. Pointer to a function to invoke the function.
+
+NOTE: (*pf) plays the same role as first. So, first(arg) is equivalent to
+(*pf)(arg).
+
+   first(arg);
+   (*pf)(arg); // both returns the same result.
+
+3. Pointer to a function to invoke the function.
+pf is a pointer to the function, (*pf)() is the function.
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Nitin Deshpande</dc:creator><pubDate>Wed, 31 Dec 2014 16:40:46 -0000</pubDate><guid>https://sourceforge.netf7b3ac7c10e0e18e9ddc621e721b078ce2bdbad1</guid></item><item><title>03_Functions modified by Nitin Deshpande</title><link>https://sourceforge.net/p/cppforgreatness/wiki/03_Functions/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v29
+++ v30
@@ -345,7 +345,7 @@
 In order to implement the plan of passing pointers to functions, in order to
 facilitate functions to accept functions as arguments.

-1. Get the address of the function. How? 
+Step 1. Get the address of the function. How? 

 *Solution: Use the function name by without using the trailing parentheses -
 this provides the address of the function.
@@ -359,7 +359,7 @@
 first(second()): The first() function first invokes the second() function,
 then the return value of second() is passed to the first() function.*

-2. Declare a pointer to a function. How?
+Step 2. Declare a pointer to a function. How?

 *Solution: A pointer to a function has to specify the same information as the function
 prototype conveys the compiler - declaration has to comply with the function
@@ -384,4 +384,4 @@

 ii. Parentheses have higher precedence than *(asterisk).

-3. Pointer to a function to invoke the function.
+Step 3. Pointer to a function to invoke the function.
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Nitin Deshpande</dc:creator><pubDate>Wed, 31 Dec 2014 15:41:27 -0000</pubDate><guid>https://sourceforge.net00d4c48f2f84f0251aa08fa0641f2241044d30c3</guid></item><item><title>03_Functions modified by Nitin Deshpande</title><link>https://sourceforge.net/p/cppforgreatness/wiki/03_Functions/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v28
+++ v29
@@ -336,27 +336,35 @@
 [46] Functions, like data, has address. A function's address is usually a
 memory address where the machine language code of the function begins. 
 Why get the address of the function?
-Solution: It may prove useful if a function wants to run another function. By
+
+*Solution: It may prove useful if a function wants to run another function. By
 passing the address, the first function finds the second function, and runs
-it.
+it.*

 [47] *Basics*:
 In order to implement the plan of passing pointers to functions, in order to
 facilitate functions to accept functions as arguments. 
+
 1. Get the address of the function. How? 
-Solution: Use the function name by without using the trailing parentheses -
+
+*Solution: Use the function name by without using the trailing parentheses -
 this provides the address of the function.
 Eg: foo() passes the return value of the function. foo returns the address of
-the function.
-*Quiz: Difference between first(second) and first(second()).....
-first(second): The first() function invokes the second() function from
+the function.*
+
+Quiz: Difference between first(second) and first(second()).....
+
+*Solution: first(second): The first() function invokes the second() function from
 within the first() function.
 first(second()): The first() function first invokes the second() function,
-then the return value of second() is passed to the first() function.** 
+then the return value of second() is passed to the first() function.*
+
 2. Declare a pointer to a function. How?
-A pointer to a function has to specify the same information as the function
+
+*Solution: A pointer to a function has to specify the same information as the function
 prototype conveys the compiler - declaration has to comply with the function
-return value and function's signature.
+return value and function's signature.*
+
 Eg: 

    T foo(T argList); // Prototype
@@ -374,4 +382,6 @@
    T (*ptrfoo) (T argList); // Pointer to a function
    T *ptrfoo (T argList);   // Returns a pointer of type T

-ii. Parentheses have higher precedence than *.
+ii. Parentheses have higher precedence than *(asterisk).
+
+3. Pointer to a function to invoke the function.
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Nitin Deshpande</dc:creator><pubDate>Wed, 31 Dec 2014 15:40:13 -0000</pubDate><guid>https://sourceforge.netf593141e17683ea48b6b95ea3e8b98dea4e71f69</guid></item></channel></rss>