标签: function-definition

什么 - >在Python函数定义中意味着什么?

在查看Python 3.3语法规范时,我最近发现了一些有趣的东西:

funcdef: 'def' NAME parameters ['->' test] ':' suite
Run Code Online (Sandbox Code Playgroud)

Python 2中没有可选的"箭头"块,我在Python 3中找不到任何有关其含义的信息.事实证明这是正确的Python并且它被解释器接受:

def f(x) -> 123:
    return x
Run Code Online (Sandbox Code Playgroud)

我认为这可能是某种先决条件语法,但是:

  • 我不能x在这里测试,它仍未定义,
  • 无论我在箭头之后放置什么(例如2 < 1),它都不会影响功能行为.

任何习惯这种语法的人都可以解释一下吗?

python annotations python-3.x function-definition

383
推荐指数
9
解决办法
14万
查看次数

函数声明与原型的替代(K&R)C语法

这个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

69
推荐指数
3
解决办法
1万
查看次数

在Haskell中有多个where语句的方法吗?

我试图在一个函数中写3-4 where语句但我得到错误并且无法做到,我试图做类似的事情:

foo x=
| x == foo1 = 5
| x == foo2 =3
| x == foo3 =1
| otherwise =2 
where foo1= samplefunct1 x
      foo2= samplefunct2 x
      foo3= samplefunct3 x
Run Code Online (Sandbox Code Playgroud)

我知道代码有点无用,但我只是写了这个来举例说明我的意思.

有没有人可以帮助我?提前致谢.

syntax haskell where-clause guard-clause function-definition

15
推荐指数
3
解决办法
2万
查看次数

错误 lnk2005 已在 .obj 中定义

关于这个错误有很多问题。但它们只与一个变量有关。

测试.h

namespace World
{
    enum Objects
    {
        TERRAIN = 1,
        BOX = 2,
        SPHERE = 4,
        CAPSULE = 8

    };  

    void WorldObjects2(unsigned int mask)
    {
      .......
    }
}

void test();
Run Code Online (Sandbox Code Playgroud)

测试.cpp

#include "test.h"

void test()
{
    .......
}
Run Code Online (Sandbox Code Playgroud)

主程序

#include "test.h"
int main()
{
    test();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我在 Visual stduio 2013 上运行这些代码时,它会引发错误。它说error LNK2005: "void __cdecl World::WorldObjects2(unsigned int)" (?WorldObjects2@World@@YAXI@Z) already defined in main.obj。我该如何纠正这个错误?

c++ namespaces compiler-errors visual-studio function-definition

6
推荐指数
1
解决办法
1万
查看次数

分段错误 chkstk_ms C++

我需要有关以下计数排序实现的帮助。是不是因为x的值太大了?我遇到分段错误。gdb 是这样说的:

Program received signal SIGSEGV, Segmentation fault.
___chkstk_ms () at /usr/src/debug/gcc-5.4.0- 1/libgcc/config/i386/cygwin.S:146
146     /usr/src/debug/gcc-5.4.0-1/libgcc/config/i386/cygwin.S: No such file or directory.
Run Code Online (Sandbox Code Playgroud)

这是代码片段,

void radix_sort::sort_array(int array[], int n)
{
    int arrayB[n];

    auto k = *std::max_element(&array[0], &array[n - 1]);
    auto m = *std::min_element(&array[0], &array[n - 1]);

    long int x = k - m + 1;
    int arrayC[x];

    for (auto i = 0; i < n; i++)
        arrayC[array[i] - m]++;

    for (long int i = 1; i < x; i++)
        arrayC[i] = arrayC[i] + …
Run Code Online (Sandbox Code Playgroud)

c++ sorting c++11 counting-sort function-definition

6
推荐指数
1
解决办法
3283
查看次数

C 中内置函数的函数定义

我们stdio.h在 C 程序中包含头文件以使用内置库函数。我曾经认为这些头文件包含我们可能在程序中使用的内置函数的函数定义。但很快就发现并非如此。

当我们打开这些头文件(例如 stdio.h)时,它只有函数原型,我在那里看不到任何函数定义。我看到这样的事情:

00133 int     _EXFUN(printf, (const char *, ...));
00134 int     _EXFUN(scanf, (const char *, ...));
00135 int     _EXFUN(sscanf, (const char *, const char *, ...));
00136 int     _EXFUN(vfprintf, (FILE *, const char *, __VALIST));
00137 int     _EXFUN(vprintf, (const char *, __VALIST));
00138 int     _EXFUN(vsprintf, (char *, const char *, __VALIST));
00139 int     _EXFUN(vsnprintf, (char *, size_t, const char *, __VALIST));
00140 int     _EXFUN(fgetc, (FILE *));
00141 char *  _EXFUN(fgets, (char *, int, FILE *)); …
Run Code Online (Sandbox Code Playgroud)

c gcc built-in function-definition

5
推荐指数
1
解决办法
2416
查看次数

我应该使用def,cdef还是cpdef定义我的Cython函数以获得最佳性能?

假设我想要最佳性能,我怎么知道在定义Cython函数时是使用def,cdef还是cpdef?

function definition cython function-definition

5
推荐指数
1
解决办法
562
查看次数

Python3 函数定义、箭头和冒号

我找到了以下python函数定义:

def reverseString(self, s: 'List[str]') -> 'None':
Run Code Online (Sandbox Code Playgroud)

我不太明白'List[str]'-> 'None'

我发现箭头是一个函数注释,但我找不到任何对 List[str] 有用且易于理解的东西。

它只是一个注释吗?还是强制参数s的类型必须是字符串数组?

python python-3.x function-definition

5
推荐指数
1
解决办法
4312
查看次数

具有 0 个参数的函数 - void vs void*?

我知道你可以像这样声明一个没有任何参数的函数:

void test()
{
    cout << "Hello world!!" << endl;
}
Run Code Online (Sandbox Code Playgroud)

但我也见过

void test(void)
{
    cout << "Hello world!!" << endl;
}
Run Code Online (Sandbox Code Playgroud)

void test(void*)
{
    cout << "Hello world!!" << endl;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是: usingvoidvoid*here 有什么区别?

c++ syntax void function-declaration function-definition

5
推荐指数
2
解决办法
240
查看次数

为什么对 C 函数的无效使用可以正常编译而没有任何警告?

与 C 相比,我更喜欢 C++,但这个简单的代码示例让我大吃一惊:

int foo() {
    return 3;
}

int main() {
    int x;
    foo(&x);
    return x;
}
Run Code Online (Sandbox Code Playgroud)

https://godbolt.org/z/4ov9nTzjM

没有警告,没有链接问题,但此代码仍然无效。

当一些初学者用具有这个奇怪问题的代码提出问题时,我偶然发现了这一点。有人在其他网站(非英语网站)上提出了问题,我想向他提供更好的解释为什么它可以编译(并了解一些有关 C 的知识)。如果有人需要的话,他的原始代码。

我怀疑这个sis在某种程度上与隐式函数声明有关,但我不完全确定。为什么编译器不会抱怨foo使用参数调用?

更新:

这是一个程序集

foo:
        push    rbp
        mov     rbp, rsp
        mov     eax, 3
        pop     rbp
        ret
main:
        push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        lea     rax, [rbp-4]
        mov     rdi, rax
        mov     eax, 0
        call    foo
        mov     eax, DWORD PTR [rbp-4]
        leave
        ret
Run Code Online (Sandbox Code Playgroud)

如您所见,x仍未初始化。

c function-call function-declaration function-definition

5
推荐指数
1
解决办法
596
查看次数