在jupyter notebook中写C/C++,最大的好处就是不用写main()函数,直接调用写好的函数即可执行。
#include<stdio.h>
int sum(int a,int b){
printf("a=%d,b=%d",a,b);
return a+b;
}
当运行函数时后面可以不用加;
,像python一样直接输出函数的返回值;
sum(2,3)
a=2,b=3
5
如果加了;
,函数仍然可以正常运行,只是不会像上面一样直接输出结果。
sum(2,3);
a=2,b=3
在一个单元格里只能定义一个函数,若定义两个函数就会报错,
int sum1(int a,int b){
return a+b;
}
int sum2(int a,int b){
return a+b;
}
[1minput_line_49:5:23: [0m[0;1;31merror: [0m[1mfunction definition is not allowed here[0m
int sum2(int a,int b){
[0;1;32m ^
[0m
Interpreter Error: