When learning the callback function, some values or pointers are returned using function parameters.

Source: Internet
Author: User

Today I saw an article titled Implementation of window fade-in and fade-out effects, and I saw a callback function problem. I have never seen it before (I am a bird, haha). So I studied it ......

 

Directly paste the code, which is relatively simple ~

 

For example, some of the original function parameters are functions, and some pointers are passed in:

Int samplefunction (INT A1, int A2); <br/> int main () <br/>{< br/> typedef int (* callbackfunction) (INT, INT ); </P> <p> callbackfunction bfn = & samplefunction; <br/> int result = bfn (3, 4); <br/> printf ("% d ", result); <br/> return 0; <br/>}</P> <p> int samplefunction (INT A1, int A2) <br/>{< br/> int num = A1 + A2; <br/> return num; <br/>}

 

 

Then I checked "typedef" in Baidu encyclopedia and wrote another example:

# Include <stdio. h> <br/> // ======================== Use Pointer ============== = <br/> void fact1 (INT num, int ** KK) <br/>{< br/> * KK = & num; <br/>}< br/> // ============================ ========</P> <p> // ============================== ============< br/> void fact2 (INT num, int & KK) <br/>{< br/> KK = num; <br/>}< br/> // ============================ ========</P> <p> // ===================== use the callback function ====== ========< br/> int fact_back1 (INT num) <br/>{< br/> return num; <br/>}< br/> int fact_back2 (INT (* callback) (INT num )) <br/>{< br/> int cool = 5; <br/> return callback (cool ); <br/>}< br/> // ============================ ========</P> <p> // ================= use the callback function (2) =============< br/> int fact_back1_2 (INT num) <br/>{< br/> return num; <br/>}</P> <p> typedef int (* callback_2) (INT); </P> <p> int fact_back2_2 (callback_2 FN) <br/>{< br/> int cool = 5; <br/> return FN (cool ); <br/>}</P> <p> // ========================== ==============</P> <p> void main () <br/>{< br/> int * A; <br/> A = NULL; </P> <p> fact1 (5, & ); <br/> printf ("* A = % d/N", * A); <br/> A = NULL; </P> <p> int B; <br/> fact2 (5, B); <br/> printf ("B = % d/N", B); </P> <p> int C; <br/> C = fact_back2 (fact_back1); <br/> printf ("c = % d/N", c); </P> <p> int C_2; <br/> c_2 = fact_back2_2 (fact_back1_2); <br/> printf ("c_2 = % d/N", C_2); <br/>}

 

Reference is the simplest and most convenient.

Let's talk about the pointer. It turns out that the last parameter of many functions in d3d is a returned pointer and then used. It's strange. I tried it many times, but it was not easy to use today.

 

However, the above methods are better suited to some scenarios. Please give some advice to the cool ~ Thanks

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.