在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: