iOS development-object-c variable parameter functions

Source: Internet
Author: User
Tags variadic

Brief introduction

A variadic function is a function that has variable parameters, that is, a function can receive multiple parameters. Sometimes we will encounter some arithmetic problems need to use, such as to calculate the sum of incoming parameters, string connection or other operation process, we also often use in OC, the most classic is NSLog (C for printf), it can specify the format of the output, formatted output content.

Now let's take a look at the definition of common functions.

Common functions

Generally we define the function is to specify how many parameters, if there are three parameters, we can write this way.

-(void) Exampleobj: (NSString *) obj obj2: (NSString *) obj2 obj3: (NSString *) obj3{    }

So there is a problem, if we need to calculate the value, for example, we need to calculate the 5 or 10 value of the sum of the operation, then we in the function need to define 5 or 10 parameters? If there are 100 values to calculate ....

Variable parameter functions

Now, we can do this by defining variadic functions, such as the sum of the values of the parameters we need to pass in now, and the number of parameters to be passed is not fixed. The method of defining variadic functions is simple, we add ", ..." after the parameter, for example.

-(void) Exampleobj: (NSString *) obj obj2: (NSString *) obj2 obj3: (NSString *) obj3, ... {    }

Now, let's go back to the problem, which is mainly used to calculate the sum of the values that the user passes in.

-(int) sum: (int) num, ... {    int result = num;     int Objnum;        Va_list arg_list;    Va_start (arg_list, num);      while int )) {        + = objnum;    }    Va_end (arg_list);     return result;}

Va_list: A type of information required to hold macro Va_start, Va_arg, and Va_end. In order to access parameters in a variable-length argument list, you must declare an object of type va_list.

Va_start: A macro used before accessing a parameter in a variable-length argument list, which initializes the object declared with Va_list and initializes the result for use by the macro va_arg and va_end;

Va_arg: Expands the macro into an expression that has the value and type of the next parameter in the variable-length argument list. Each call to Va_arg modifies the object declared with Va_list so that the object points to the next parameter in the argument list.

Va_end: This macro enables the program to return normally from a variable-length argument list with a function referenced by the macro va_start.

How to use

The use of the method is also very simple, in the variable parameter, all parameters are separated by commas.

NSLog (@ "%d", [self sum:1, nil]);

Precautions

At the time of use, the variable parameter is appended with a nil value, which means the end of the representation. Just like Uialertview initialization, it doesn't know how many buttons you have at first, you're free to add buttons to Riga, and end up with a nil, so the two are the same.

Watch out.

1) When we want to create a variadic function, we must put the ellipsis (...) Put it behind the argument list and only have one of these, because we can't define two variable parameters.

2) When we need to read a mutable parameter list, we must first specify a variable va_list and then use the macro Va_start, Va_arg, va_end to get it.

Reference:

C Variable parameter function implementation--http://blog.csdn.net/weiwangchao_/article/details/4857567

Wikipedia. variable function--http://zh.wikipedia.org/wiki/variable number of variables

Blog Garveycalvin

Blog Source: http://www.cnblogs.com/GarveyCalvin/

This article copyright belongs to the author and the blog Garden altogether, welcome reprint, but must retain this paragraph statement, and gives the original link, thanks cooperation!

iOS development-object-c variable parameter functions

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.