block blocks syntax. --- anonymous functions
Blocks can define anonymous functions inside a function.
BlocK- implements the maximum value of two numbers.
Function:
#import<Foundation/Foundation.h>#import "Person.h"//1. Output I love iOSvoidoutput () {printf ("I Love ios\n");}//2. Find the maximum of two numbers intMaxValue (intXinty) {returnx > y?x:y;}intMainintargcConst Char*argv[]) {@autoreleasepool {//method One: Direct printingoutput (); intmax = MaxValue (2,5); printf ("max =%d", Max); //method Two: Define pointer variables void(*p) () =NULL; P= output;//the pointer points to the corresponding function;p (); int(*p) (int,int) =NULL; P=MaxValue; intmax = P (2,5); printf ("max =%d", Max); //method Three: function renaming (printing more than one type, it is more convenient)typedefint(*pfun) (int,int);//Rename the function type to PfunPfun P1 =NULL; P1=MaxValue; intmax = P1 (2,5); printf ("max =%d", Max);
Fourth Method- --Block int (^max) (int , int ) = ^int (int A, int b) { return a > B?
A:B; };
// How to call block block int value = max (4 , 6 ); printf ( value =%d , value);
Block syntax----blocks
Block syntax---blocks