函数
案例:
public class Demo{
public static void main(String[] args) {
for(int i=0;i<30;i++){
System.out.print("=");
}
System.out.println();
======================================================================================
System.out.println("瘦小离家胖了回");
for(int i=0;i<30;i++){
System.out.print("=");
}
System.out.println();
System.out.println("乡音不改肉成堆");
for(int i=0;i<30;i++){
System.out.print("=");
}
System.out.println();
System.out.println("儿童相见不相识");
for(int i=0;i<30;i++){
System.out.print("=");
}
System.out.println();
System.out.println("笑问胖子你是谁");
for(int i=0;i<30;i++){
System.out.print("=");
}
System.out.println();
}
}
存在问题:
代码冗余 可维护性差
函数的概念
概念:实现特定功能的一段代码 可以重复使用
函数的定义
函数的声明:声明了函数的功能 需要的数据 能够给函数使用这返回的数据 名字 以及异常
语法:
public static 返回值类型 函数名(参数列表)
=========================================================================================
函数的实现:具体实现功能的代码
语法:{函数的实现代码}
函数的定义语法:
public static 返回值类型 函数名(参数列表){
函数功能实现代码;
}
函数定义的位置
位置:类中 和主函数并列
案例:
//位置1
public class Demo{
//位置2
public static void main(String[] args) {
//位置3
}
//位置4
}
//位置5
函数可以定义的位置 :位置 2 和 4
函数的调用
无参函数调用语法:函数名()
一个参数函数的调用语法:函数名(实际参数);
多个参数函数的调用语法:函数名(实际参数1,实际参数2,....实际参数n)
解决后的代码:
//位置1
public class Demo{
//位置2
public static void main(String[] args) {
printLine();
System.out.println("瘦小离家胖了回");
printLine();
System.out.println("乡音不改肉成堆");
printLine();
System.out.println("儿童相见不相识");
printLine();
System.out.println("笑问胖子你是谁");
printLine();
}
//位置4
public static void printLine(){
for(int i=0;i<30;i++){
System.out.print("=");
}
System.out.println();
}
}
//位置5
函数的参数
形式参数:写在函数的参数列表中,声明函数需要的数据类型 以及数据个数
格式:数据类型 变量名 多个形式参数之间用英文状态的逗号 , 隔开
实际参数:函数的使用者 在调用函数的时候传入的实际的数据
案例:一个形式参数的函数
代码:
//位置1
public class Demo{
//位置2
public static void main(String[] args) {
printLine(10);
System.out.println("瘦小离家胖了回");
printLine(20);
System.out.println("乡音不改肉成堆");
printLine(30);
System.out.println("儿童相见不相识");
printLine(40);
System.out.println("笑问胖子你是谁");
printLine(50);
}
//位置4
public static void printLine(int n){
for(int i=0;i< n;i++){
System.out.print("=");
}
System.out.println();
}
}
//位置5
案例2;
//位置1
public class Demo{
//位置2
public static void main(String[] args) {
printLine(10,"*");
System.out.println("瘦小离家胖了回");
printLine(20,"=");
System.out.println("乡音不改肉成堆");
printLine(30,"+");
System.out.println("儿童相见不相识");
printLine(40,"$");
System.out.println("笑问胖子你是谁");
printLine(50,"@");
}
//位置4
public static void printLine(int n , String s){
for(int i=0;i< n;i++){
//字符串
System.out.print(s);
}
System.out.println();
}
}
//位置5
定义一个函数 计算一个数的阶乘 并打印
代码:
public static void add(int a, int b) {
int sum = a+b;
System.out.println(sum);
}
public static void jc(int a) {
int jiecheng = 1;
for(int i = 1;i<=a;i++){
jiecheng*=i;
}
System.out.println(jiecheng);
}
函数的返回值
函数的返回值:规定了函数能够给函数的使用者返回什么类型的数据 基本数据类型 引用的数据类型
void :当函数没有返回值的时候定义为void
return语句:在有返回值的函数中用于返回函数计算后的结果,把结果返回给函数的使用者
注意:
1.当有返回值的函数中存在分支语句 必须保证每个分支都必须有返回语句
案例:
//位置1
public class Demo{
//位置2
public static void main(String[] args) {
int n = add(4,5);
System.out.println(n);
}
//函数需要两个int 参数a b 当a>b时 返回 a+b 反之返回 a*b
public static int add(int a, int b) {
if (a>b) {
return a+b;
} else {
return a*b;
}
}
}
//位置5
====================================================================================
2.一个有返回值的函数只能有一个返回值 不能返回多个返回值
案例:
//位置1
public class Demo{
//位置2
public static void main(String[] args) {
int n = add(4,5);
System.out.println(n);
}
public static int add(int a, int b) {
//返回 a+b
return a+b;
//返回 a*b
return a*b;//错的!!!!!!
}
}
//位置5
====================================================================================
3.当return用在无返回值函数中,作用是直接结束函数
案例:
//位置1
public class Demo{
//位置2
public static void main(String[] args) {
add(4,5);
}
//函数需要两个int 参数a b 当a>b时 返回 a+b 反之返回 a*b
public static void add(int a, int b) {
int sum = a+b;
return;
System.out.println(sum);//不可达代码
}
}
//位置5
总结
函数的好处
1.提高代码的重用性 减少冗余代码
2.使程序可以分工协作 使每个功能模块化 可利于维护
函数的嵌套调用
在一个函数中调用另一个函数
案例:
//位置1
public class Demo{
//位置2
public static void main(String[] args) {
System.out.println("主函数开始执行");
m1();
System.out.println("主函数结束");
}
public static void m1() {
System.out.println("m1开始执行");
m2();
System.out.println("m1结束");
}
public static void m2() {
System.out.println("m2开始执行");
System.out.println("m2结束");
}
}
//位置5
练习:
用函数的嵌套调用计算 4的阶乘
jc4(4) 3
计算4阶乘函数(int n) 4*计算3阶乘函数(int n)
4! = 4*3!
3! = 3*2!
2! = 2*1!
代码:
//位置1
public class Demo{
//位置2
public static void main(String[] args) {
int j = jc4(4);
System.out.println(j);
}
public static int jc4(int n) {
int jiecheng = n*jc3(n-1);
return jiecheng;
}
public static int jc3(int n) {
int jiecheng = n*jc2(n-1);
return jiecheng;
}
public static int jc2(int n) {
int jiecheng = n*jc1(n-1);
return jiecheng;
}
public static int jc1(int n) {
return 1;
}
}
//位置5
函数的递归调用
递归:函数自身调用自己
案例:
//位置1
public class Demo{
//位置2
public static void main(String[] args) {
int j = jc(4);
System.out.println(j);
}
public static int jc(int n) {
if (n==1) {
return 1;
} else {
int jiecheng = n*jc(n-1);
return jiecheng;
}
}
/*public static int jc4(int n) {
int jiecheng = n*jc3(n-1);
return jiecheng;
}
public static int jc3(int n) {
int jiecheng = n*jc2(n-1);
return jiecheng;
}
public static int jc2(int n) {
int jiecheng = n*jc1(n-1);
return jiecheng;
}*/
/*public static int jc1(1) {
return 1;
}
*/
}
//位置5
注意:
1.递归一定要有出口 否则会出现无限递归 发生栈溢出异常
2.递归不能进行太多次数 递归的深度不能太深
3.一般用递归要比循环简单 但是能用递归解决的问题都可以用循环解决 只是递归简单