# 1 函数基本使用

函数的调用方法用C++。

主函数要在一个Class中,静态的,无返回值;

见示例

using System;

namespace CalculatorApplication
{
class NumberManipulator
{
public int FindMax(int num1, int num2)
{
/* 局部变量声明 */
int result; if (num1 > num2)
result = num1;
else
result = num2; return result;
}
}
class Test
{
static void Main(string[] args)
{
/* 局部变量定义 */
int a = 100;
int b = 200;
int ret;
NumberManipulator n = new NumberManipulator();
//调用 FindMax 方法
ret = n.FindMax(a, b);
Console.WriteLine("最大值是: {0}", ret );
Console.ReadLine(); }
}
}

  

支持递归

---

# 2 函数的输入输出

## 1 值传递

正常同C++

## 2 引用传递 ref 关键字

using System;
namespace CalculatorApplication
{
class NumberManipulator
{
public void swap(ref int x, ref int y)
{
int temp; temp = x; /* 保存 x 的值 */
x = y; /* 把 y 赋值给 x */
y = temp; /* 把 temp 赋值给 y */
} static void Main(string[] args)
{
NumberManipulator n = new NumberManipulator();
/* 局部变量定义 */
int a = 100;
int b = 200; Console.WriteLine("在交换之前,a 的值: {0}", a);
Console.WriteLine("在交换之前,b 的值: {0}", b); /* 调用函数来交换值 */
n.swap(ref a, ref b); Console.WriteLine("在交换之后,a 的值: {0}", a);
Console.WriteLine("在交换之后,b 的值: {0}", b); Console.ReadLine(); }
}
}

  

## 3 输出 out 关键字

using System;

namespace CalculatorApplication
{
class NumberManipulator
{
public void getValue(out int x )
{
int temp = 5;
x = temp;
} static void Main(string[] args)
{
NumberManipulator n = new NumberManipulator();
/* 局部变量定义 */
int a = 100; Console.WriteLine("在方法调用之前,a 的值: {0}", a); /* 调用函数来获取值 */
n.getValue(out a); Console.WriteLine("在方法调用之后,a 的值: {0}", a);
Console.ReadLine(); }
}
}

  

---

参考:

http://www.runoob.com/csharp/csharp-methods.html

最新文章

  1. JS操作cookie
  2. LeetCode39/40/22/77/17/401/78/51/46/47/79 11道回溯题(Backtracking)
  3. ECSHOP模糊分词搜索和商品列表关键字飘红功能
  4. 图论(无向图的割顶):POJ 1144 Network
  5. 使用Intent实现Activity的显式跳转
  6. c语言结构体在内存中存储,字节对齐
  7. 使用php完成常见的"文件上传"功能
  8. 11--Python 备份文件程序
  9. less学习笔记(一)
  10. Python自学笔记-Django分页器小实例
  11. Cenos 6.5上的subverion的yum配置笔记
  12. touch事件应用
  13. Java基础笔记(7)----三个修饰符
  14. 浅谈Java内存模型
  15. JavaScript--鼠标滚动改变图片大小
  16. Rust hello world !
  17. 【树状DP】星象仪
  18. Git版本控制:Github的使用之 多人协作及参与项目
  19. phonegap 的指南针 api Compass
  20. JQuery的焦点事件focus() 与按键事件keydown() 及js判断当前页面是否为顶级页面 子页面刷新将顶级页面刷新 window.top.location

热门文章

  1. 使用 Java 执行 groovy 脚本或方法
  2. 版本分支管理标准 - Git Flow
  3. 论文阅读: Infrastructure-Based Calibration of a Multi-Camera Rig
  4. 搭建MQTT服务器
  5. maven基本知识的7个提问
  6. 02、策略模式(Strategy)
  7. git的本质是资源库和版本(资源)目录的维护过程
  8. node、npm、gulp安装
  9. lumen 响应宏
  10. JS树结构转list结构