c语言编程1+2+3
C++ Functions and Block based aptitude questions and answers - This section contains Aptitude Questions and Answers based on C++ functions and blocks.
基于C ++函数和块的能力倾向问题和答案 -本节包含基于C ++函数和块的能力倾向问题和答案。
与C ++编程相关的功能和块列表适度性问答 (List of Functions and Blocks related C++ programming Aptitude Questions and Answers)
#include <iostream>
using namespace std;
void fun1(int x)
{
cout<<x<<endl;
}
int main()
{
//function calling
fun1(10,20);
return 0;
}
Compile Time Error
Since function declaration has only one integer argument while we are passing to arguments, hence program will through compiler error.
#include <iostream>
using namespace std;
int main()
{
//function declaration
void fun(void);
//function calling
fun();
cout<<"::OK"<<endl;
return 0;
}
//function definition
void fun(void)
{
cout<<"Hello";
}
Hello::OK
We can declare any function within the main().
#include <iostream>
using namespace std;
//declaration and definition
void print_value(int x,int y=10)
{
cout<<"x:"<<x<<",y:"<<y<<endl;
}
int main()
{
//function calling
print_value(100);
return 0;
}
x:100,y:10
In the function declaration, argument y is a default parameter and while calling we are not passing second parameter hence 10 will be the value of second parameter y.
x:100,y:10
在函数声明中,参数y是默认参数,在调用时我们没有传递第二个参数,因此10将是第二个参数y的值。
#include <iostream>
using namespace std;
//declaration and definition
void fun(const int x=10)
{
cout<<"x:"<<x<<endl;
}
int main()
{
const int a=100;
cout<<"call 1:";
fun();
cout<<"call 2:";
fun(a);
return 0;
}
Call 1:x:10
Call 2:x:100
In the first calling there is no parameter, hence default value 10 will be printed and in the second calling 100 is passing as a parameter so 100 will be printed after second calling.
通话1:x:10
通话2:x:100
在第一个调用中没有参数,因此将打印默认值10,在第二个调用中传递100作为参数,因此将在第二个调用后打印100。
#include <iostream>
using namespace std;
namespace myfunctions
{
void fun1(void)
{
cout<<"Fun1"<<endl;
}
}
int main()
{
.....
return 0;
}
myfunctions::fun1();
fun1() is declared and define within the namespace and we can access a function of a namespace using Scope Resolution Operator (::).
myfunctions :: fun1();
fun1()是在名称空间中声明和定义的,我们可以使用范围解析运算符(::)访问名称空间的函数。
#include <iostream>
using namespace std;
namespace myfunctions
{
void fun1(void)
{
cout<<"Fun1"<<endl;
}
void fun1(int a)
{
cout<<a<<endl;
}
}
int main()
{
myfunctions::fun1(10.2f);
return 0;
}
10
In the namespace there are two functions with same name (it is called function overloading), we are calling fun1 with value 10.2f and this value will convert into integer (implicit conversion) hence second fun1 will be called.
10
在命名空间中,有两个具有相同名称的函数(称为函数重载),我们正在调用fun1,其值为10.2f,该值将转换为整数(隐式转换),因此将调用第二个fun1。
翻译自: https://www.includehelp.com/cpp-programming/functions-blocks-aptitude-questions-answers.aspx
c语言编程1+2+3