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. LR之分析

    1.Errors(错误统计) 每秒错误数:数值越小越好,通过这个图可以知道,当负载增加的时候,定位何时系统在负载下开始不稳定甚至出错. 2.Transaction(事务) average transa ...

  2. Cent OS (三)vi文本编辑操作

    序号 命令 命令含义 1 echo            2 vi/vim    编辑 3 cat     cat 命令用于连接文件并打印到标准输出设备上. 4 more   分屏显示文本内容 5 l ...

  3. linux执行时间段内日志关键字搜索

    sed -n '/起始时间/,/结束时间/p' 日志文件 | grep '关键字' 查询文件debug.log在2019-11-18 08:00:00~2019-11-18 08:21:00时间段内e ...

  4. flask实现异步任务

    最近在开发同步mysql数据到redis的接口,因为数据同步涉及各种增删查改,如果用同步实现,可能回造成连接超时.堵塞,所以,使用python实现异步任务. 代码实现from flask import ...

  5. QTP使用dictionary 对象

    1. 创建即使用Dictionary对象 ' 创建Dictionary对象Set Dic = CreateObject("Scripting.Dictionary")' 添加Dic ...

  6. Python3调用hessian

    领导派了个任务,实现服务器日志文件调用hessian接口保存到数据库 研究了半天python调用hessian的办法 首先使用hessian for python的链接: http://hessian ...

  7. ZR-19CSP-S赛前冲刺

    ZR-19CSP-S赛前冲刺 1 ZR-19CSP-S赛前冲刺 2 ZR-19CSP-S赛前冲刺 3 ZR-19CSP-S赛前冲刺 4 ZR-19CSP-S赛前冲刺 5 ZR-19CSP-S赛前冲刺 ...

  8. XX-net 3.11.9 登陆Google等出现没有开启cookie的问题

    糟糕!您的浏览器似乎禁用了 Cookie.请务必启用 Cookie 或尝试打开一个新的浏览器窗口. 出现这个问题解决方法: 1.配置好X-tunnel,即登录账号2.打开谷歌浏览器或者你用的浏览器,设 ...

  9. Java使用Jsoup简单解析页面

    jsoup 是一款 Java 的 HTML 解析器,可直接解析某个 URL 地址.HTML 文本内容.它提供了一套非常省力的 API,可通过 DOM,CSS 以及类似于 jQuery 的操作方法来取出 ...

  10. Java8 LocalDate操作时间和日期的API

    时间项目中的涉及到的时间处理非常多,犹豫SimpleDateFormat的不安全性以及Calendar等类在计算时比较复杂, 往往我们都会使用工具类来封装较多的日期处理函数, 但是JDK8中新增了操作 ...