php学习笔记之 函数声明(二)_php基础
/*
* 1.内部函数:PHP可以在函数内部再声明函数
* 目的就是在函数内部调用
* 用来帮助外部函数完成一些子功能
*
* 2.递归函数:在自己内部调用自己的函数名
*
* 3.重用函数
*
* require:用于静态包含
* include:用于动态包含
* require_once:用于静态包含,只包含一次
* include_once:用于动态包含,只包含一次
*
* 4.一些系统函数的使用
* 资源=opendir("目录名")
* readdir(资源)
*
*
*/
//内部函数
function score($php,$java,$dotnet)
{
function php($php)
{
if($php>60)
return "及格";
else
return "不及格";
}
function java($java)
{
if($java>60)
return "及格";
else
return "不及格";
}
function dotnet($dotnet)
{
if($dotnet>60)
return "及格";
else
return "不及格";
}
$total=$php+$java+$dotnet;
$agv=$total/3;
echo "你的php成绩是{$php}分,".php($php)."
";
echo "你的java成绩是{$java}分,".java($java)."
";
echo "你的dotnet成绩是{$dotnet}分,".dotnet($dotnet)."
";
echo "你的总分是:{$total}
";
echo "你的平均分是:{$agv}
";
}
score(50,90,70);
//递归函数
function demo($num)
{
echo $num."
";
if($num>0)
demo($num-1);
else
echo "--------------------------------
";
echo $num."
";
}
demo(10);
function total($dirname,&$dirnum,&$filename)
{
$dir=opendir($dirname);
readdir($dir)."
";
readdir($dir)."
";
while($filename=readdir($dir))
{
$newfile=$dirname."/".$filename;
echo $filename."
";
if(is_dir($filename
}
}
$dirnum=0;
$filenum=0;
total("c:/windows",$dirnum,$filenum);
echo "目录总数:".$dirnum."
";
echo "文件总数:".$filenum."
";
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Default parameters in C++ provide the ability to specify default values for function parameters, thereby enhancing code readability, simplicity, and flexibility. Declare default parameters: Add the "=" symbol after the parameter in the function declaration, followed by the default value. Usage: When the function is called, if optional parameters are not provided, the default values will be used. Practical case: A function that calculates the sum of two numbers. One parameter is required and the other is optional and has a default value of 0. Advantages: Enhanced readability, increased flexibility, reduced boilerplate code. Note: It can only be specified in the declaration, it must be at the end, and the types must be compatible.

In C++, the order of function declarations and definitions affects the compilation and linking process. The most common is that the declaration comes first and the definition comes after; you can also use "forwarddeclaration" to place the definition before the declaration; if both exist at the same time, the compiler will ignore the declaration and only use the definition.

A function declaration informs the compiler of the existence of the function and does not contain the implementation, which is used for type checking. The function definition provides the actual implementation, including the function body. Key distinguishing features include: purpose, location, role. Understanding the differences is crucial to writing efficient and maintainable C++ code.

Function declaration and definition are necessary in C++. Function declaration specifies the return type, name and parameters of the function, while function definition contains the function body and implementation. First declare the function and then use it in your program passing the required parameters. Use the return statement to return a value from a function.

C++ compilation error: function call does not match function declaration, how to solve it? When developing C++ programs, you will inevitably encounter some compilation errors. One of the common errors is that the function call does not match the function declaration. This kind of error widely exists among C++ programmers. Due to not paying attention to the correctness of function declaration, it leads to compilation problems, which ultimately wastes time and energy to fix the problem and affects development efficiency. Ways to avoid this mistake require following some norms and standard practices, let’s take a look at them below. What is a function call versus a function declaration?
![[[nodiscard]] in C++ function declarations: Demystifying the consequences of ignoring return values](https://img.php.cn/upload/article/000/465/014/171455868319393.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
The [[nodiscard]] attribute indicates that the return value of the function must not be ignored, otherwise it will cause a compiler warning or error to prevent the following consequences: uninitialized exceptions, memory leaks, and incorrect calculation results.

The C++ function declaration syntax is: returnTypefunctionName(parameterType1parameterName1,...,parameterTypeNparameterNameN);, where returnType is the return type, functionName is the function name, parameterType is the parameter type, and parameterName is the parameter name, which must end with a semicolon.

A function declaration tells the compiler that a function exists without providing a function body. The steps are as follows: specify the function return type (void if there is no return value) define the function name and declare the function parameters (optional, including data type and identifier) plus semicolon
