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. Divideing Jewels

    Divideing Jewels 时间限制: 1 Sec  内存限制: 128 MB提交: 63  解决: 17[提交][状态] 题目描述 Mary and Rose own a collection ...

  2. 「LibreOJ β Round」ZQC 的手办

    https://loj.ac/problem/504 一类套路题. 首先这个玩意可以两个logn树套树做.... naive地,把区间内的所有数拿出来放进堆里.不断取出. 太多了. 所以开始只保留那初 ...

  3. 【已转移】【Java架构:基础技术】一篇文章搞掂:SVN

    一个例子: 公司的SVN代码中,含有target等文件夹,每次生成运行后,有很多文件打扰签入 处理方案: 1.CheckOut时,点击ChooseItems选项,不要选择这些target文件夹(有点麻 ...

  4. java 异常处理try+catch

    在整个异常处理机制中,异常在系统中进行传递,传递到程序员认为合适的位置,就捕获到该异常,然后进行逻辑处理,使得项目不会因为出现异常而崩溃.为了捕获异常并对异常进行处理,使用的捕获异常以及处理的语法格式 ...

  5. selenium:Xpath定位详解

    xpath定位在业界被戏称为元素定位的"屠龙宝刀",宝刀在手,武林我有.现在我们就来详解xpath定位方法. 一.xpath通过元素属性定位 xpath可以通过元素的属性来定位,如 ...

  6. Cocos2d-x之定时器

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 每一个游戏程序都有一个循环在不断运行,它是由导演对象来管理与维护.如果需要场景中的精灵运动起来,可以在游戏循环中使用定时器对精灵等对象进行 ...

  7. struct和class的相同点与不同点

    struct是c语言中常用来定义结构体时使用的 class是c++中用来定义类时所使用的 相同 struct(结构体)和class(类)内均可有不同个数.不同类型的数据 定义时 都必须在前面加上str ...

  8. 插件化框架解读之so 文件加载机制(四)

    阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680 提问 本文的结论是跟着 System.loadlibrary() ...

  9. Linux下实现客户端和服务器端的通信

    首先,可以将代码复制下来放到U盘里,然后挂载到Linux上 挂载步骤 找到设备->USB->你U盘的名字 挂载成功 访问U盘把代码拷贝到home文件夹下,就可以直接进行编译. client ...

  10. Elasticsearch7.3开启x-pack验证

    原文 Elasticsearch7开启x-pack验证 前言 在Elasticsearch7.3,x-pack已经作为默认的插件集成在Elasticsearch里面了,所以无需在bin/elastic ...