Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果没有C中的嵌套函数,如何赋予一个函数另一个函数的作用域?_Python_C_Matlab_Function_Nested - Fatal编程技术网

Python 如果没有C中的嵌套函数,如何赋予一个函数另一个函数的作用域?

Python 如果没有C中的嵌套函数,如何赋予一个函数另一个函数的作用域?,python,c,matlab,function,nested,Python,C,Matlab,Function,Nested,在Python中: #!/usr/bin/python def f1(a, b, c, d): return a + b + c + d x = 5; y = 6; z = 7; fm = lambda m: f1(m,x,y,z) print fm(4) 在Matlab中: function [retval] = f1(a, b, c, d) retval = a + b + c + d; x = 5; y = 6; z = 7; fm = @(m) f1(m,x,

在Python中:

#!/usr/bin/python

def f1(a, b, c, d):
    return a + b + c + d

x = 5;
y = 6;
z = 7;

fm = lambda m: f1(m,x,y,z)
print fm(4)
在Matlab中:

function [retval] = f1(a, b, c, d)
    retval = a + b + c + d;

x = 5;
y = 6;
z = 7;

fm = @(m) f1(m,x,y,z);
fm(4)

我知道如果不使用gcc扩展,C中没有嵌套函数。如何在C中获得与使用嵌套函数相同的功能?如何在另一个函数(如示例中)中声明变量并将其用作常量?

您可以使用预处理器宏:

#include <stdio.h>

// Global variables?
int x = 5, y= 6, z = 7;

int f1( int a, int b, int c, int d )
{
    return a + b + c + d;
}

#define fm(m) f1((m), x, y, z)

int main()
{
    fprintf( stdout, "%d\n", f1( 4, x, y, z ) );
    fprintf( stdout, "%d\n", fm( 4 ) );
}
#包括
//全局变量?
int x=5,y=6,z=7;
整数f1(整数a、整数b、整数c、整数d)
{
返回a+b+c+d;
}
#定义fm(m)f1((m),x,y,z)
int main()
{
fprintf(stdout,“%d\n”,f1(4,x,y,z));
fprintf(stdout,“%d\n”,fm(4));
}
或者如果您可以使用C++:

#include <iostream>

// Global variables?
int x = 5, y= 6, z = 7;

int f1( int a, int b, int c, int d )
{
    return a + b + c + d;
}

// Alternative is using default arguments (much cleaner too)
int easy( int a, int b = 5, int c = 6, int d = 7 )
{
    return a + b + c + d;
}

int main()
{
    std::cout << f1( 4, x, y, z ) << std::endl;
    auto fm = [](int m)->int { return f1( m, x, y, z ); };  // Use lambda function
    std::cout << fm( 4 ) << std::endl;
    std::cout << easy( 4 ) << std::endl;
}
#包括
//全局变量?
int x=5,y=6,z=7;
整数f1(整数a、整数b、整数c、整数d)
{
返回a+b+c+d;
}
//另一种选择是使用默认参数(也更简洁)
简单整数(整数a,整数b=5,整数c=6,整数d=7)
{
返回a+b+c+d;
}
int main()
{
std::cout在您的Python示例中,没有什么比“嵌套函数”更好的了-只有一个匿名函数,可以用命名函数替换。FWIW您的Python代码片段很容易用C重写:

# include <stdio.h>

int f1(int a, int b, int c, int d) {
    return a + b + c + d;
}

int x = 5;
int y = 6;
int z = 7;

int fm(int m) {
    return f1(m, x, y, z);
}

int main(int argc, char **argv) {
    printf("%d\n", fm(4));
    return 0;
}
#包括
整数f1(整数a、整数b、整数c、整数d){
返回a+b+c+d;
}
int x=5;
int y=6;
int z=7;
整数调频(整数米){
返回f1(m,x,y,z);
}
int main(int argc,字符**argv){
printf(“%d\n”,fm(4));
返回0;
}

将它们声明为全局静态变量?或将它们作为参数传递。我认为全局变量是不好的。这会起作用,但这真的是最好的方法吗?如果您的程序由模块组成,将以前嵌套的函数放在一个模块中,并对共享变量进行静态声明,将阻止它们对o可用我的代码是:Vane,USE1801359:我使用了一个答案,因为我需要一些代码格式。长话短说:你的问题没有意义,所以请你贴出一个更好的例子,我想在C,而不是C++中解决。lambda函数不是C,对吧?@ GeorgeHoupis:C!= C++天气,我没有NoTI。ce C标记。这是一个疏忽。是的,没问题,@bruno desthuilliers。放松;你会活得更久。所有的答案,包括这一个都使用全局变量。这是解决方案使用它们的一种情况吗?@user1801359:你的Python代码片段也使用全局变量。好的,Python的“全局”变量实际上是“模块全局变量”,但在上面的代码中没有任何区别。正如我所评论的:你真正的问题是什么?