当我在EE攻读本科学位时,MATLAB要求每个函数都在自己的文件中定义,即使它是一个单行程序.
我现在正在攻读研究生学位,我必须在MATLAB中编写一个项目.这仍然是新版MATLAB的要求吗?
如果可以在文件中放置多个函数,是否有任何限制?例如,文件中的所有函数都可以从文件外部访问,还是只能从与文件同名的函数中访问?
注意:我使用的是MATLAB版本R2007b.
在gatomic.cglib 中,有几个函数声明如下所示:
gboolean
(g_atomic_int_compare_and_exchange_full) (gint *atomic,
gint oldval,
gint newval,
gint *preval)
{
return g_atomic_int_compare_and_exchange_full (atomic, oldval, newval, preval);
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下这段代码到底是做什么的吗?我对这里的几件事感到困惑:
函数名称g_atomic_int_compare_and_exchange_full位于括号内。这有什么意义呢?
该函数的主体显然只包含对函数本身的调用,因此这将永远运行并导致堆栈溢出(双关语)。
我根本无法理解这个函数声明。这里究竟发生了什么?
为什么这些例子中的第一个不起作用,但所有其他例子都不起作用?
// 1 - does not work
(function() {
setTimeout(someFunction1, 10);
var someFunction1 = function() { alert('here1'); };
})();
// 2
(function() {
setTimeout(someFunction2, 10);
function someFunction2() { alert('here2'); }
})();
// 3
(function() {
setTimeout(function() { someFunction3(); }, 10);
var someFunction3 = function() { alert('here3'); };
})();
// 4
(function() {
setTimeout(function() { someFunction4(); }, 10);
function someFunction4() { alert('here4'); }
})();
Run Code Online (Sandbox Code Playgroud) 我注意到在CoffeeScript中,如果我使用以下方法定义函数:
a = (c) -> c=1
Run Code Online (Sandbox Code Playgroud)
我只能得到函数表达式:
var a;
a = function(c) {
return c = 1;
};
Run Code Online (Sandbox Code Playgroud)
但是,我个人经常使用函数声明,例如:
function a(c) {
return c = 1;
}
Run Code Online (Sandbox Code Playgroud)
我确实使用了第一种形式,但我想知道CoffeeScript中是否有一种生成函数声明的方法.如果没有这种方式,我想知道为什么CoffeeScript会避免这样做.我不认为JSLint会声明声明的错误,只要函数声明在作用域的顶部.
这不是lambda函数问题,我知道我可以将lambda赋给变量.
允许我们声明,但不在代码中定义函数是什么意思?
例如:
#include <iostream>
int main()
{
// This is illegal
// int one(int bar) { return 13 + bar; }
// This is legal, but why would I want this?
int two(int bar);
// This gets the job done but man it's complicated
class three{
int m_iBar;
public:
three(int bar):m_iBar(13 + bar){}
operator int(){return m_iBar;}
};
std::cout << three(42) << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以我想知道的是为什么C++会允许two哪些看似无用,three哪个看起来更复杂,但却不允许one?
编辑:
从答案中可以看出,代码内声明可能能够防止命名空间污染,我希望听到的是为什么声明函数的能力已被允许,但是不允许定义函数的能力.
如何在GHCi中定义此函数的等价物(取自learnyouahaskell)?
import Data.List
numUniques :: (Eq a) => [a] -> Int
numUniques = length . nub
Run Code Online (Sandbox Code Playgroud)
如果没有类型声明,GHCi会接受函数定义,但最终会出现无用的类型:
Prelude Data.List> import Data.List
Prelude Data.List> let numUniques' = length . nub
Prelude Data.List> :t numUniques'
numUniques' :: [()] -> Int
Run Code Online (Sandbox Code Playgroud)
结果函数仅接受单位列表作为参数.
有没有办法在GHCi中提供类型声明?或者是否有另一种方法来定义这些不需要类型声明的函数?
我在GHCi指南中没有看到明显的线索,并尝试了以下表达式(无济于事):
> let numUniques' = ((length . nub) :: (Eq a) => [a] -> Int)
> :t numUniques'
numUniques' :: [()] -> Int
Run Code Online (Sandbox Code Playgroud) 这个C语法有什么用- 使用'K&R'样式函数声明?
int func (p, p2)
void* p;
int p2;
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我能够在Visual Studios 2010beta中写这个
// yes, the arguments are flipped
void f()
{
void* v = 0;
func(5, v);
}
Run Code Online (Sandbox Code Playgroud)
我不明白.这种语法有什么意义?我可以写:
int func (p, p2)
int p2;
{
return 0;
}
// and write
int func (p, p2)
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它似乎唯一指定的是它使用了多少参数和返回类型.我猜没有类型的参数有点酷,但为什么允许它和int paranName函数声明后?有点奇怪.
这还是标准的C吗?
c function kernighan-and-ritchie function-declaration function-definition
在阅读有关C ++中的函数的知识时,我被告知函数需要调用声明。例如:
#include <iostream>
int main() {
std::cout << "The result is " << sum(1, 2);
return 0;
}
int sum(int x, int y) {
return x + y;
}
Run Code Online (Sandbox Code Playgroud)
由于该函数没有声明,因此返回错误sum。
main.cpp:4:36: error: use of undeclared identifier 'sum'
std::cout << "The result is " << sum(1, 2);
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
为了解决这个问题,我将添加声明:
#include <iostream>
int sum(int x, int y); // declaration
int main() {
std::cout << "The result is " << sum(1, 2);
return 0;
}
int sum(int …Run Code Online (Sandbox Code Playgroud) c++ program-entry-point forward-declaration function-declaration
任何只包含return语句的函数都可以声明
constexpr,因此如果所有参数都是,constexpr并且只constexpr在其体内调用函数,则允许在编译时进行计算.有没有理由不宣布任何此类功能constexpr?
例:
constexpr int sum(int x, int y) { return x + y; }
constexpr i = 10;
static_assert(sum(i, 13) == 23, "sum correct");
Run Code Online (Sandbox Code Playgroud)
任何人都可以提供一个例子来声明一个函数constexpr
会造成什么伤害吗?
一些初步想法:
即使没有充分的理由宣布一个函数,constexpr我也无法想象该constexpr关键字具有过渡角色:它在代码中的缺失不需要编译时评估将允许那些不实现编译时评估的编译器仍然编译该代码(但是在使用需要它们的代码上可靠地失败constexpr).
但是我不明白:如果没有充分的理由不宣布函数constexpr,为什么标准库中的每个函数都没有被声明constexpr?(你不能说它还没有完成,因为还没有足够的时间去做,因为为所有人做这件事是明智的 - 与决定每一个功能是否成功相反constexpr.) - - 我知道N2976
故意不要求cstrs用于许多标准库类型,例如容器,因为这对于可能的实现来说太有限了.让我们从参数中排除它们并且只是想知道:一旦标准库中的一个constexpr类型实际上有一个cstr,为什么不是每个函数都在它上面声明constexpr?
在大多数情况下,你也不能说你可能不想constexpr仅仅因为你没有设想任何编译时使用而声明一个函数
:因为如果其他人退出.将使用您的代码,他们可能会看到您没有这样的用途.(当然,也可以用于类型特征类型和类似的东西.)
所以我想有一个很好的理由和一个故意不宣布功能的好例子constexpr?
("每个函数"我总是指:满足存在要求的每个函数constexpr,即被定义为单个return语句,只接受带有constexpr cstrs的类型的参数,并且只调用constexpr …
如果在C++中定义了一个新变量,则可以在初始化表达式中使用该变量的名称,例如:
int x = sizeof(x);
Run Code Online (Sandbox Code Playgroud)
那么函数参数的默认值又如何呢?是否允许通过名称引用参数?例如:
void f(int y = sizeof(y)) {}
Run Code Online (Sandbox Code Playgroud)
该函数在 Clang 中被接受,但在 GCC 中被拒绝,并出现错误:
'y' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
演示: https: //gcc.godbolt.org/z/YsvYnhjTb
这里是哪个编译器?
c++ compiler-errors language-lawyer function-declaration default-arguments