ProDeveloperTutorial.com

教程和编程解决方案
菜单
  • Shell脚本
  • 系统设计
  • Linux系统编程
  • 4g LTE
  • 编码问题
  • C
  • C ++
  • DSA
  • GIT

C ++第8章:C ++函数

前开发者教程 2020年1月26日

在本章中,我们将研究:

  1. 函数原型声明
  2. 功能定义
  3. 函数调用
  4. 实际和形式上的争论
  5. 退货声明。
  6. 按价值致电
  7. 通过地址致电
  8. 通过参考电话
  9. 默认国彩网
  10. 内联函数
  11. 功能重载
  12. 递归函数

 

在开发软件应用程序时,较大的程序分为称为功能的较小模块。函数可帮助我们调试程序并非常轻松地维护程序。函数还可以帮助减小程序的大小。假设如果需要计算两个不同位置的变量总和,而不是编写相同的求和代码,则最好将其拆分为单独的函数。从而减小程序大小。

 

1.函数原型声明

函数原型用于声明用户定义的函数。函数原型将帮助编译器了解函数名称,其接受的国彩网以及返回类型。

因此,在稍后定义函数时,如果程序员犯了错误,则编译器将显示错误。

 

下面是函数原型示例:

 

int sum (int num_1, int num_2);

void display (int arr[]);

void函数将不返回任何内容。

注意函数原型声明末尾的分号。

 

2.功能定义

在声明函数原型之后,我们应该编写一组语句来告诉函数应该做什么。这称为定义函数。用大括号“ {}”编写的语句集称为函数主体。

 

例:

int sum (int num_1, int num_2)

{

return (num_1 + num_2);

}

 

3.函数调用

定义函数后,应调用一个函数以调用该函数。应该使用函数的名称和国彩网列表来调用它。如果函数返回一个值,则应在其前面加上一个变量。

 

例:

这里我们用两个国彩网调用“ sum”函数。由于“ sum”函数将返回一个值,因此它将存储在“ total_sum”变量中。

int total_sum = sum (1, 3);

 

4.实际和形式上的争论

在调用函数中声明的国彩网称为实际国彩网。

在被调用函数中声明的国彩网称为形式国彩网。

 

例:

int main()
{

    sum (sub_1, sub_2); // actual arguments.

}

void sum (int num_1, int num_2) // formal arguments

{

}

 

5.返回声明。

 

Return语句用于将值返回给调用函数。当编译器遇到“ return”语句时,控件将转移回调用函数。

 

句法:

return <variable>; 

or 

return;

 

6.按值致电:

 

在这里,当进行函数调用时,实际国彩网的副本将传递给形式国彩网。因此,对函数内部形式国彩网的任何更改都不会影响调用函数的实际国彩网。

 

例:

#include<iostream>
using namespace std;

void display(int a, int b);

int main()

{

    int a = 10;
    int b = 20;

    cout<<"Before calling display() value of a = "<<a <<" value of b = "<<b<<endl;

    display(a, b); // call by value or pass by value.

    cout<<"After calling display() value of a = "<<a <<" value of b = "<<b<<endl;

}


void display(int a, int b)

{

    a += 20;
    b += 40;

    cout<<"In display() value of a = "<<a <<" value of b = "<<b<<endl;
   
    return;
}

输出量:

Before calling display() value of a = 10 value of b = 20

In display() value of a = 30 value of b = 60

After calling display() value of a = 10 value of b = 20

 

7.按地址致电

在这种情况下,实际国彩网的地址使用指针传递给函数。在这里,对形式论证的任何改变都会改变形式论证的价值。

 

例:

#include<iostream>
using namespace std;


void display(int *a, int *b);


int main()
{

    int a = 10;
    int b = 20;

    cout<<"Before calling display() value of a = "<<a <<" value of b = "<<b<<endl;

    display(&a, &b); // call by address or pass by address.

    cout<<"After calling display() value of a = "<<a <<" value of b = "<<b<<endl;

}

void display(int *a, int *b)

{

    *a += 20;
    *b += 40;

    cout<<"In display() value of a = "<< *a <<" value of b = "<< *b<<endl;

    return;
}

输出:

Before calling display() value of a = 10 value of b = 20

In display() value of a = 30 value of b = 60

After calling display() value of a = 30 value of b = 60

 

8.致电查询

我们知道CPP允许我们为已经定义的变量分配一个不同的名称,这些变量称为参考变量。因此,当我们借助参考变量调用函数时,如果您在被调用函数中更改了这些变量的值,则实际值也将被更改。

 

例:

#include<iostream>
using namespace std;

void display(int &a, int &b);

int main()
{

    int a = 10;
    int b = 20;

    int &alias_a = a;
    int &alias_b = b;

    cout<<"Before calling display() value of a = "<<a <<" value of b = "<<b<<endl;

    display(alias_a, alias_b); // call by reference or pass by reference.

    cout<<"After calling display() value of a = "<<a <<" value of b = "<<b<<endl;
}


void display(int &a, int &b)
{

    a += 20;
    b += 40;

    cout<<"In display() value of a = "<< a <<" value of b = "<< b<<endl;

    return;
}

输出:

Before calling display() value of a = 10 value of b = 20

In display() value of a = 30 value of b = 60

After calling display() value of a = 30 value of b = 60

 

9.默认国彩网

C ++允许程序员在函数声明期间向函数国彩网添加默认值。因此,当使用较少数量的国彩网调用该函数时,将使用默认值。

当您通过添加新国彩网来更新旧函数而又不破坏使用较少国彩网调用的旧函数时,此功能很有用。

默认值应从右到左开始。默认值不能直接分配给中间的国彩网。

 

例:

#include<iostream>
using namespace std;

void display(int a, int b = 10);

int main()
{

    int a = 10;

    cout<<"Before calling display() value of a = "<<a <<endl;
 
    display(a);

return 0;

}


void display(int a, int b )
{

    cout<<"In display() value of a = "<< a <<" value of b = "<< b<<endl;

return;

}

输出:

Before calling display() value of a = 10

In display() value of a = 10 value of b = 10

 

10.内联函数

进行函数调用时,控件从调用函数传递到被调用函数。从函数返回时,反之亦然。如果程序员希望避免这种情况,那么他应该使用“内联”函数。

当函数声明为内联时,编译器将复制函数主体并将其粘贴到进行函数调用的位置。从而避免了控制权的通过。

如果内联函数很大,则编译器会将其视为普通函数。

 

例:

#include<iostream>
using namespace std;

void display(int a, int b);

int main()

{

    int a = 10;

    int b = 30;

    display(a, b);

return 0;

}

inline void display(int a, int b )
{
    cout<<"In display() value of a = "<< a <<" value of b = "<< b<<endl;
    return;
}

输出:

In display() value of a = 10 value of b = 30

 

11.函数重载

C ++允许程序员定义具有相同名称的函数,但国彩网列表应不同。

函数重载示例:

 

#include<iostream>
using namespace std;

void sum(int a, int b);
void sum(float a, float b);

int main()
{

    int a = 10;
    int b = 30;

    float c = 34.56;
    float d = 45.67;

    sum(a, b);
    sum(c, d);
return 0;

}

void sum(int a, int b )
{
   cout<<"In sum of INT, the result is = "<<(a+b)<<endl;
   return;
}

void sum(float a, float b )
{

   cout<<"In sum of FLOAT, the result is = "<<(a+b)<<endl;
   return;

}

 

输出:

In sum of INT, the result is = 40

In sum of FLOAT, the result is = 80.23

 

12.递归函数

调用自身的函数称为递归函数。要停止递归,应该有一个测试案例,递归停止。

在下面的示例中,我们使用递归找出数字的阶乘:

 

#include<iostream>
using namespace std;

int factorial(int a);

int main()
{

    int a = 5;
   
    int result = factorial(a);

    cout<<"The factorial of "<<a <<" is = "<<result<<endl;

return 0;

}

int factorial(int a)
{

    int result = 0;
    
    if (a == 1)

        return 1;
    else

        result = a * factorial(a - 1);

return result;

}

 

输出:

The factorial of 5 is = 120

 

 

该网站上可用的教程列表:

C编程20+章C ++编程80+章
100多个编码问题数据结构和算法85+章
系统设计20+章Shell脚本编写12章
4g LTE 60+章节最常见的编码问题
5G NR 50+章Linux系统编程20+章
分享
电子邮件
鸣叫
领英
Reddit
绊倒
Pinterest的
上一篇文章
下一篇

关于作者

前开发者教程

每天我们都会讨论竞争性编程问题,请加入我们的网站:   电报频道

ProDeveloperTutorial.com

教程和编程解决方案
版权© 2020 ProDeveloperTutorial.com
从以下课程获得热门课程: 教育性的
<col id="Os0CNCA" class="Offyrf3"><datalist id="omp3z9H" class="okaaRoS"></datalist></col>






        <optgroup id="DB9Qbg8"><rp id="d4UP0Tc" class="dlfzEop"></rp></optgroup>
        <blockquote id="GrktpXi"><summary id="quYPmvx" class="q4oEijz"><kbd id="op1CyxE" class="oF4QUbs"></kbd></summary></blockquote>


        <ol id="tVXtGhV" class="tVrao66"></ol>



            <keygen id="edQ0A5X" class="ejXG4GG"><button id="EJC1FSG"><rp id="BLhn27R"><pre class="H24swhd"></pre></rp></button></keygen>
            <s class="Uisz8nv"><bdi class="vTMgNyF"></bdi></s>