数据类型--变量常量--运算符表达式--语句(顺序,分支,循环)--数组--函数

函数

1、定义:能够独立完成某个功能的模块。
2、好处:1.结构更清析(编写、维护方便 )。2.代码重用。3.分工开发。
3、四要素:名称,输入(参数),输出(返回的类型),加工(函数体)

语法:
返回类型 函数名(参数类型 参数名,....)
{
函数体
}

函数调用:
[数据类型 变量名 = ]函数(参数);

函数调用时:调用的参数和函数定义的参数保持一对待:个数,类型,对应。

形参:形式参数。——函数定义的参数。
实参:实际参数。——函数调用的参数。

实参、形参传值的规律——传值,传址。

传值:参于整型、浮点、bool、char这几种内建类型在函数传递参数的时候,默认都是传值。
传值是把实参做一个副本(copy)传递给形参。
m = 30;
Add(m);
static void Add(int a)
{
a += 20;
}
传址:默认情况下,数组就是传地址,字符串也是传地址。
对于内建的整型、浮点、bool、char这些类型,如果要变成传址的话,需要在前面加ref
m = 30;
Add(ref m);
static void Add(ref int a)
{
a += 20;
}

对于传值和传址大家要记住 :
1.什么是传值,什么是传址?这个要分清楚。
2.默认情况下,哪些类型是传值?哪些类型是传址?
3.对于默认传值的类型,如何让他们变为传址?ref

以后为了防止因为传值,传址引起来的错误 ,建议大家采用返回值的形式,明确返回的数据

递归——仅做了解。
函数自己调自己。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8. class Class7
  9. {
  10. static void Main(string[] args)
  11. {
  12. Test();
  13. }
  14. static void Test(int a)
  15. {
  16. //if条件的return很重要,没有的话就永远出不来了。
  17. if (a > )
  18. {
  19. return;
  20. }
  21. a++;
  22. Console.WriteLine("正在做第" + a + "个梦");
  23. Test(a);
  24. Console.WriteLine("第" + a + "个梦醒了");
  25. }
  26. }
  27. }

常用的类:
(一)数学类:Math
1.Math.Ceiling(小数/整数):返回大于当前小数的最小整数。——天花板数
2.Math.Floor(小数/整数):返回小于当前小数的最大整数。——地板数
Console.WriteLine(Math.Ceiling(3.14)); //4
Console.WriteLine(Math.Floor(3.14)); //3
Console.WriteLine(Math.Ceiling(3.0)); //3

3.Math.Pow(2,3)求指数。相当于2的3次方
4.Math.Sqrt(16)开平方。
5.四舍五入。
Math.Round(3.63); //4
Math.Round(3.14); //3

(二)日期时间:DateTime
构造:DateTime dt = new DateTime([1990,2,5[,3,44,21]]);
DateTime dt = new DateTime(); //?
DateTime dt = new DateTime(1990, 2, 5);//?
DateTime dt = new DateTime(1990, 2, 5, 3, 44, 25);//?
当前时间:
DateTime dt = DateTime.Now;

日期时间对象的数据:
Year,Month,Day,Hour,Minite,Second,MilliSecond
DayOfWeek——星期几。DayOfYear——一年中的第几天。
Date——取期日期部份。TimeOfDay——取期时间部份。
日期时间对象的函数:
AddYears(int num)
AddMonths(int num)
AddDays(int num)
AddHours(int num)
AddMinutes(int num)
AddSeconds(int num)

日期时间型数据可以直接相减,返回两个日期之间差的天数和时间。

ToString(格式字符串)函数:把日按照某种格式显示出来。
格式字符串:
yyyy——四位数字的年份
yy——两位数字的年份
MM——两位数字的月分,不足两位添0
M——1-2位数字的月份
dd——两位数字的天,不足两位添0
d——1-2位数字的天。
hh——
h——
mm——
m——
ss——
s——
ms——毫秒。
例如:
DateTime dt = DateTime.Now;
Console.WriteLine(dt.ToString("yyyy年MM月dd日hh时mm分ss秒"));

不止是日期时间型数据的ToString()函数中可以放格式化字符中。整数,小数的ToString()中也可以放格式化字符串。
小数和整数类型的格式化符号主要是有四个。
.——小数点
,——整数部份三位的分隔符
#——任意位数字,有几位显示几位
0——至少一位数字,不足则补0.

例:
#.00——必须保留两位小数。

(三)字符串
*Length:字符串的长度。

ToLower():全都转成小写
ToUpper():全都转成大写

TrimStart():
TrimEnd():
Trim():压两头的空格。

*StartsWidth("字符串"):(bool)是否以括中的字符串开头,是--返回true。
*EndsWidth("字符串"):(bool)是否以括号中的字符串结尾,是--返回true。
*Contains("字符串"):(bool)是否包括括号中的字符串。是--返回true。

*IndexOf("子串"):(int)返回子串在字符串中第一次出现的位置。
*LastIndexOf("子串"):(int)返回子串在字符串中最后一次出现的位置。
以上两函数,如果在字符串中找不到相应的子串,返回-1

*Substring(int start[,int length]):(string)截取子串。
Replace(string old,string new):(string)把字符串的old串换成new串
*Split('字符'):(string[])按照括号中的字符把字符串拆开成数组中的元素。

1.从键盘输入一个正确的身份证号,获取其生日。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Console.Write("请输入一个身份证号");
  13. string s = Convert.ToString(Console.ReadLine());
  14. Console.Write("生日是" + s.Substring(, ));
  15. }
  16. }
  17. }

2.从路径:C:\Users\Administrator\Desktop\1220\0104\aaa.txt中,获取文件名。(用两种法做)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. string s = "C:\\Users\\Administrator\\Desktop\\1220\\0104\\aaa.txt";
  13. string[] ss = s.Split('\\');
  14. for (int i = ; i < ss.Length; i++)
  15. {
  16. Console.WriteLine(ss[i]);
  17. }
  18. }
  19. }
  20. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. string s = "C:\\Users\\Administrator\\Desktop\\1220\\0104\\aaa.txt";
  13. int ss = s.LastIndexOf("\\");
  14. Console.Write("文件名是:" + s.Substring(ss + ));
  15. }
  16. }
  17. }

3.用户从键盘上输入一个邮箱。验证是否正确。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Console.Write("请输入一个邮箱:");
  13. string s = Convert.ToString(Console.ReadLine());
  14. int a = s.IndexOf(".");
  15. int a1 = s.LastIndexOf(".");
  16. int b = s.IndexOf("@");
  17. int b1 = s.LastIndexOf("@");
  18. if (a == a1)
  19. {
  20. if (a == || a == s.Length - || a1 == || a1 == s.Length - || b == || b == s.Length - || b != b1 || a - b <= || a1 - b <= )
  21. {
  22.  
  23. Console.Write("你输入的邮箱错误");
  24. return;
  25. }
  26. }
  27. else if (a1 - a != || a == || a == s.Length - || a1 == || a1 == s.Length - || b == || b == s.Length - || b != b1 || a - b <= || a1 - b <= )
  28. {
  29. Console.Write("你输入的邮箱错误");
  30. return;
  31. }
  32. Console.Write("您输入的邮箱正确");
  33. }
  34. }
  35. }

C#整理7——函数的更多相关文章

  1. 《理解 ES6》阅读整理:函数(Functions)(八)Tail Call Optimization

    尾调用优化(Tail Call Optimization) 尾调用是指函数的最后一条语句是函数调用,比如下面的代码: function doSomething() { return doSomethi ...

  2. 《理解 ES6》阅读整理:函数(Functions)(七)Block-Level Functions

    块级函数(Block-Level Functions) 在ES3及以前,在块内声明一个函数会报语法错误,但是所有的浏览器都支持块级函数.不幸的是,每个浏览器在支持块级函数方面都有一些细微的不同的行为. ...

  3. 《理解 ES6》阅读整理:函数(Functions)(六)Purpose of Functions

    明确函数的双重作用(Clarifying the Dual Purpose of Functions) 在ES5及更早的ES版本中,函数调用时是否使用new会有不同的作用.当使用new时,函数内的th ...

  4. 《理解 ES6》阅读整理:函数(Functions)(五)Name Property

    名字属性(The name Property) 在JavaScript中识别函数是有挑战性的,因为你可以使用各种方式来定义一个函数.匿名函数表达式的流行使用导致函数调试困难,在栈信息中难以找出函数名. ...

  5. 《理解 ES6》阅读整理:函数(Functions)(四)Arrow Functions

    箭头函数(Arrow Functions) 就像名字所说那样,箭头函数使用箭头(=>)来定义函数.与传统函数相比,箭头函数在多个地方表现不一样. 箭头函数语法(Arrow Function Sy ...

  6. 《理解 ES6》阅读整理:函数(Functions)(三)Function Constructor & Spread Operator

    增强的Function构造函数(Increased Capabilities of the Function Constructor) 在Javascript中Function构造函数可以让你创建一个 ...

  7. 《理解 ES6》阅读整理:函数(Functions)(二)Unnamed Parameters

    使用未命名参数(Working with Unnamed Parameters) JavaScript并不限制传递给函数的实参个数,你可以总是传递比形参个数多或者少的实参.在ES6中当向函数传递比形参 ...

  8. 《理解 ES6》阅读整理:函数(Functions)(一)Default Parameter Values

    对于任何语言来说,函数都是一个重要的组成部分.在ES6以前,从JavaScript被创建以来,函数一直没有大的改动,留下了一堆的问题和很微妙的行为,导致在JavaScript中使用函数时很容易出现错误 ...

  9. 14、C#基础整理(函数)

    函数 1.概念:是一个带有输入参数.输出参数.返回值的代码块. 2.写法: 修饰符  返回值类型  函数名(输入参数,输入参数) { 方法段 return 返回值; } 3.注释: (1)输入参数格式 ...

随机推荐

  1. paip.sql2k,sql2005,sql2008,sql2008 r2,SQL2012以及EXPRESS版本的区别

    paip.sql2k,sql2005,sql2008,sql2008 r2,SQL2012以及EXPRESS版本的区别 作者Attilax ,  EMAIL:1466519819@qq.com  来源 ...

  2. global中拦截404错误的实现方法

    1. void Application_Error(object sender, EventArgs e) { if(Context != null) { HttpContext ctx = Http ...

  3. 【web开发--js学习】functionName 如果是一个属性值,函数将不会被调用

    <html> <head> <meta http-equiv="Content-Type" Content="text/html; char ...

  4. [Linked List]Copy List with Random Pointer

    Total Accepted: 53943 Total Submissions: 209664 Difficulty: Hard A linked list is given such that ea ...

  5. volatile 和const 变量的使用

    一.volatile定义: 一个定义为volatile的变量是说这变量可能会被意想不到的被改变,这样,有了volatile变量后,就提醒编译器就不会去假设这个变量的值了.精确地说就是,编译中的优化器在 ...

  6. input type file onchange上传文件的过程中,遇到同一个文件二次上传无效的问题。

    不要采用删除当前input[type=file]这个节点,然后再重新创建dom这种方案,这样是不合理的.解释如下:input[type=file]使用的是onchange去做,onchange监听的为 ...

  7. ACboy needs your help again!--hdu1702

    ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  8. ASCII码排序,hdu-2000

    Problem Description 输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符.   Input 输入数据有多组,每组占一行,有三个字符组成,之间无空格.   Output ...

  9. 一道C语言面试题:得到整数的M进制表示字符串

    题目:输入整数n和M,输出n在M进制下的表示字符串.如n=3000,M=16,输出16进制下3000的表示字符串,为“BB8” 来源:某500强企业面试题目 思路:对n取模M,将得到的数字压入栈中,再 ...

  10. Inno Setup 安装inf文件的一个例子

    原文 http://zwkufo.blog.163.com/blog/static/2588251201063033524889/ ; INF安装例子; [Setup]; 注意: AppId 的值是唯 ...