Functions

function is a concept for C programming language, objective-c is entirely relies on C.
To define a function, you need provide four components: return value, function name, parameters and code block.
like this:
1
2
3
int

getRandomInteger(
int

minimum,
int

maximum) {
return

arc4random_uniform((maximum - minimum) + 1) + minimum;
}

the return value is a integer value, function name is getRandownInteger, there are two paramters: minimum and maximum, code block is enclosed by braces.

Function also can use pointer reference as return value or paramaters, like this:
1
2
3
4
5
NSString

*getRandomMake(
NSArray

*makes) {
    int

maximum = (
int)[makes
count];
    int

randomIndex = arc4random_uniform(maximum);
    return

makes[randomIndex];
}
The declaration and implementation can be sepearted, the declaration tells the compiler that these is a function and the implementation do the real work.
1
2
3
4
5
6
7
8
//
Declaration
NSString

*getRandomMake(
NSArray

*);
//
Implementation
NSString

*getRandomMake(
NSArray

*makes) {
    int

maximum = (
int)[makes
count];
    int

randomIndex = arc4random_uniform(maximum);
    return

makes[randomIndex];
}
If a method want call function , the function declaration should be defined before the method.
static word
static functions
By default, all functions have a global scope, if you want limit the function’s scope to the current file, use ‘static’ keyword:
1
2
3
4
5
6
//
Static function declaration
static

int

getRandomInteger(
int,
int);
//
Static function implementation
static

int

getRandomInteger(
int

minimum,
int

maximum) {
    return

arc4random_uniform((maximum - minimum) + 1) + minimum;
}
Note that static keyword should be use on both the function declaration and implementation.
static local variable
By defualt, the local variable in a function will be reset each time the function is called. If you use ‘static’ midifier on a local variable in a function , the variable value will be remembered.
Note that the local variable’s scope do not changed, it still only accessible inside the function itself.

objective-c: Functions and static keyword的更多相关文章

  1. static 静态域 类域 静态方法 工厂方法 he use of the static keyword to create fields and methods that belong to the class, rather than to an instance of the class 非访问修饰符

    总结: 1.无论一个类实例化多少对象,它的静态变量只有一份拷贝: 静态域属于类,而非由类构造的实例化的对象,所有类的实例对象共享静态域. class Employee { private static ...

  2. C++ auto 与 register、static keyword 浅析

    [register/auto的比較分析] #include <iostream> using namespace std; int main(){ int i,sum=0; for(i=0 ...

  3. java基础——static keyword小节

    static 用于修饰成员 修饰成员变量和成员函数 被修饰过的成员的特点:   1.随着类的载入而载入   2.优先于对象而存在   3.被全部对象所共享   4.能够直接被类名调用

  4. 5.24 Declaring Attributes of Functions【转】

    转自:https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html 5.24 Declaring Attributes o ...

  5. C++转义字符 &amp; keyword

    转义字符: 换行符 \n   水平制表符\t 纵向制表符 \v 退格符 \b 回车符 \r   进纸符 \f 报警(响铃)符 \a 反斜线 \\ 疑问号 \? 单引號 \' 双引號 \"   ...

  6. Use Reentrant Functions for Safer Signal Handling(译:使用可重入函数进行更安全的信号处理)

    Use Reentrant Functions for Safer Signal Handling 使用可重入函数进行更安全的信号处理 How and when to employ reentranc ...

  7. static 不被实例调用

    static - JavaScript | MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/ ...

  8. [Javascript] Working with Static Properties on a Class

    Classes are syntactic sugar over functions and functions are also referred to as "callable" ...

  9. asp.net MVC helper 和自定义函数@functions小结

    asp.net Razor 视图具有.cshtml后缀,可以轻松的实现c#代码和html标签的切换,大大提升了我们的开发效率.但是Razor语法还是有一些棉花糖值得我们了解一下,可以更加强劲的提升我们 ...

随机推荐

  1. mybatis批量生成

    使用了mybatis-generator后,寻找只写一个table标签就可以全部生成的方法 下载mybatis-generator-core-1.3.2-bundle.zip 解压后打开docs 发现 ...

  2. php面试专题---2、常量及数据类型考点

    php面试专题---2.常量及数据类型考点 一.总结 一句话总结: 变量为null和变量判断为false的情况需要仔细注意下 1.PHP中字符串可以使用哪三种定义方法以及各自的区别是什么? 单引号:不 ...

  3. 测开之路四十:jQuery基本用法

    从cdn引入jQuery库:https://www.bootcdn.cn/,搜索jQuery 在html里面(使用之前计算器的脚本),把复制的标签粘贴到引入js标签的前面:<script src ...

  4. iview+vue 使用中遇到的问题(表格、select、radio)

    1.iview+vue中,对表头的动态设置: iview表头若是需要动态设置,可以有两个方法,第一种: children: [ { title: '2017年', align: 'center', k ...

  5. play framework 在idea简单运行配置(mac为例)

    文章目录 play 最基本的构建 在idea中配置 配置jdk相关 配置play 运行 运行 play 最基本的构建 https://blog.csdn.net/dataiyangu/article/ ...

  6. Tex与PDF

    由Knuth Donald开发的tex.web会生成DVI文件,DVI也是Knuth自己实现的(虽然概念是其他人提出的)一种文件格式,目标是与设备无关. 通过dvips程序可以将DVI格式转化成Pos ...

  7. 简单DP入门(二) 最长上升子序列及其优化

    最长上升子序列解决问题: 有N个数,求出它最长的上升子序列并输出长度. 在题里不会讲的这么直白,这个算法往往会与其他的算法混在一起使用. 在这篇文章中不会出现其他的例题,为了让大家更好的理解,我只会对 ...

  8. SQLServer2008不允许保存更改

    sql server 2008在更改表结构的时候提示 “不允许保存更改,您所做的更改要求删除并重新创建以下表” 解决方案: 1.一般情况下:工具--选项--Designers--表设计器和数据库设计器 ...

  9. scrapy爬虫值Items

    Items有哪些知识? 1.声明 import scrapy class Product(scrapy.Item): name = scrapy.Field() price = scrapy.Fiel ...

  10. 常用css代码(scss mixin)

    溢出显示省略号 参过参数可以只是单/多行. /** * 溢出省略号 * @param {Number} 行数 */ @mixin ellipsis($rowCount: 1) { @if $rowCo ...