The definition function of objective-c

Source: Internet
Author: User

Demo1.m

A basic function definition

#import<Foundation/Foundation.h>//defines a function named Max that returns a value of type int. The parameter passed in is two int type dataintMaxintXinty) {    intZ=x>y?x:y; returnZ//return Results}intMainintargcChar*argv[]) {@autoreleasepool {intA=6; intb=9; intResult=max (A, b);//Pass a A, b into the Max function, and the return value assigned to resultNSLog (@"%d", result);//value of output result    }}

Demo2.m

Places to be aware of

1. When the return value type specified by the function is different from the value of return within the function, the return value type specified by the function is whichever

2. Define the function as far as possible before the main function, although in Xcode in the main function can also, but this does not conform to the C99 standard, in other operating environments may be error-

(If you have obsessive-compulsive disorder or other reasons, be sure to put the function behind the main function and want to keep the language normative, see DEMO3.M)

3. If the function does not return a value, be sure to explicitly specify that the function return value type is void

#import<Foundation/Foundation.h>intMainintargcChar*argv[]) {@autoreleasepool {NSLog (@"by calling the function, the value returned is:%f", Demo_method (Ten,3));//the value of the output Demo_method    }}//defines a function named Demo_method that returns a value of type int. The parameter passed in is two int type dataintDemo_method (intXinty) {    floatz=x/y; NSLog (@"within the function, the value when it is not returned is:%f",(float) (z);//the value of the output Demo_method//where float is a forced data type conversion    returnZ//return Results}

Demo3.m

function declaration

Well, in fact demo3.m and DEMO2.M no difference, just before the main function to declare a custom function beforehand, let the compiler know I have a function called Demo_method. Declaration format is simple, see demo3.m the first line of code will know

Of course it can be written

int demo_method (int,int);

That is, the formal parameter list does not specify a formal parameter name, but it is OK to specify the formal parameter type directly.

intDemo_method (intXinty);intMainintargcChar*argv[]) {@autoreleasepool {NSLog (@"The Max is:%d.\n", Max (8,9)); NSLog (@"%@", Sayhi (@"Kitty")); NSLog (@"by calling the function, the value returned is:%d", Demo_method (Ten,3));//the value of the output Demo_method    }    return 0;}intDemo_method (intXinty) {    floatZ= (float) x/y; NSLog (@"within the function, the value when it is not returned is:%f", z);//the value of the output Demo_method    returnZ//return Results}

The definition function of objective-c

Related Article

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.