c语言定义函数和声明函数_C语言中用户定义函数的类型

c语言定义函数和声明函数_C语言中用户定义函数的类型c语言定义函数和声明函数C语言中用户定义函数的类型(TypeofUser-definedFunctionsinC)Therecanbe4differenttypesofuser-definedfunctions,theyare:可以有4种不同类型的用户定义函数,它们是:Functionwithnoargumentsandnoreturnv…

大家好,又见面了,我是你们的朋友全栈君。

c语言定义函数和声明函数

There can be 4 different types of user-defined functions, they are:

可以有4种不同类型的用户定义函数,它们是:

  1. Function with no arguments and no return value

    没有参数也没有返回值的函数

  2. Function with no arguments and a return value

    没有参数和返回值的函数

  3. Function with arguments and no return value

    有参数且无返回值的函数

  4. Function with arguments and a return value

    带参数和返回值的函数

Below, we will discuss about all these types, along with program examples.

下面,我们将讨论所有这些类型以及程序示例。

没有参数也没有返回值的函数 (Function with no arguments and no return value)

Such functions can either be used to display information or they are completely dependent on user inputs.

这些功能可以用于显示信息,也可以完全取决于用户输入。

Below is an example of a function, which takes 2 numbers as input from user, and display which is the greater number.

下面是一个函数示例,该函数以2个数字作为用户输入,并显示较大的数字。

#include<stdio.h>

void greatNum();       // function declaration

int main()
{
    greatNum();        // function call
    return 0;
}

void greatNum()        // function definition
{
    int i, j;
    printf("Enter 2 numbers that you want to compare...");
    scanf("%d%d", &i, &j);
    if(i > j) {
        printf("The greater number is: %d", i);
    }
    else {
        printf("The greater number is: %d", j);
    }
}

没有参数和返回值的函数 (Function with no arguments and a return value)

We have modified the above example to make the function greatNum() return the number which is greater amongst the 2 input numbers.

我们修改了上面的示例,以使函数greatNum()返回2个输入数字中较大的数字。

#include<stdio.h>

int greatNum();       // function declaration

int main()
{
    int result;
    result = greatNum();        // function call
    printf("The greater number is: %d", result);
    return 0;
}

int greatNum()        // function definition
{
    int i, j, greaterNum;
    printf("Enter 2 numbers that you want to compare...");
    scanf("%d%d", &i, &j);
    if(i > j) {
        greaterNum = i;
    }
    else {
        greaterNum = j;
    }
    // returning the result
    return greaterNum;
}

有参数且无返回值的函数 (Function with arguments and no return value)

We are using the same function as example again and again, to demonstrate that to solve a problem there can be many different ways.

我们一次又一次地使用与示例相同的功能,以说明解决问题的方法有很多种。

This time, we have modified the above example to make the function greatNum() take two int values as arguments, but it will not be returning anything.

这次,我们修改了上面的示例,以使函数greatNum()接受两个int值作为参数,但不会返回任何内容。

#include<stdio.h>

void greatNum(int a, int b);       // function declaration

int main()
{
    int i, j;
    printf("Enter 2 numbers that you want to compare...");
    scanf("%d%d", &i, &j);
    greatNum(i, j);        // function call
    return 0;
}

void greatNum(int x, int y)        // function definition
{
    if(x > y) {
        printf("The greater number is: %d", x);
    }
    else {
        printf("The greater number is: %d", y);
    }
}

带参数和返回值的函数 (Function with arguments and a return value)

This is the best type, as this makes the function completely independent of inputs and outputs, and only the logic is defined inside the function body.

这是最好的类型,因为这使函数完全独立于输入和输出,并且仅在函数体内定义了逻辑。

#include<stdio.h>

int greatNum(int a, int b);       // function declaration

int main()
{
    int i, j, result;
    printf("Enter 2 numbers that you want to compare...");
    scanf("%d%d", &i, &j);
    result = greatNum(i, j); // function call
    printf("The greater number is: %d", result);
    return 0;
}

int greatNum(int x, int y)        // function definition
{
    if(x > y) {
        return x;
    }
    else {
        return y;
    }
}

功能嵌套 (Nesting of Functions)

C language also allows nesting of functions i.e to use/call one function inside another function’s body. We must be careful while using nested functions, because it may lead to infinite nesting.

C语言还允许嵌套函数,即在另一个函数体内使用/调用一个函数。 使用嵌套函数时必须小心,因为它可能导致无限嵌套。

function1()
{
    // function1 body here
    
    function2();
    
    // function1 body here
}

If function2() also has a call for function1() inside it, then in that case, it will lead to an infinite nesting. They will keep calling each other and the program will never terminate.

如果function2()中也有对function1()的调用,则在这种情况下,它将导致无限嵌套。 他们将继续互相调用,程序将永远不会终止。

Not able to understand? Lets consider that inside the main() function, function1() is called and its execution starts, then inside function1(), we have a call for function2(), so the control of program will go to the function2(). But as function2() also has a call to function1() in its body, it will call function1(), which will again call function2(), and this will go on for infinite times, until you forcefully exit from program execution.

听不懂? 让我们考虑一下在main()函数内部,调用了function1()并开始执行,然后在function1()内部,我们对function2()进行了调用,因此程序的控制权将移交给function2()。 但是由于function2()在其主体中也有对function1()的调用,它将调用function1(),后者将再次调用function2(),这将持续无数次,直到您强制退出程序执行为止。

什么是递归? (What is Recursion?)

Recursion is a special way of nesting functions, where a function calls itself inside it. We must have certain conditions in the function to break out of the recursion, otherwise recursion will occur infinite times.

递归是嵌套函数的一种特殊方式,其中函数在其中调用自身。 函数必须具有一定的条件才能中断递归,否则递归将无限次发生。

function1()
{   
    // function1 body
    function1();
    // function1 body
}

示例:使用递归的阶乘 (Example: Factorial of a number using Recursion)

#include<stdio.h>

int factorial(int x);       //declaring the function

void main()
{
    int a, b;
    
    printf("Enter a number...");
    scanf("%d", &a);
    b = factorial(a);       //calling the function named factorial
    printf("%d", b);
}

int factorial(int x) //defining the function
{
    int r = 1;
    if(x == 1) 
        return 1;
    else 
        r = x*factorial(x-1);       //recursion, since the function calls itself
    
    return r;
}

Similarly, there are many more applications of recursion in C language. Go to the programs section, to find out more programs using recursion.

同样,在C语言中还有许多递归应用。 进入程序部分,使用递归查找更多程序。

翻译自: https://www.studytonight.com/c/type-of-functions-and-recursion.php

c语言定义函数和声明函数

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/150653.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 激光slam理论与实践_SLAM算法

    激光slam理论与实践_SLAM算法激光SLAM笔记(1)——激光SLAM框架和基本数学理论1、SLAM分类1.1、基于传感器的分类1.2、基于后端的分类2、激光SLAM算法(基于优化的算法)2.1、激光SLAM算法的流程2.2、激光SLAM常用算法2.3、激光SLAM在实际环境中的问题3、激光SLAM算法介绍3.1、2D激光SLAM3.2、3D激光SLAM1、SLAM分类1.1、基于传感器的分类1.2、基于后端的分类 …

    2022年8月23日
    5
  • 计算机技术与阅读,PISA2018阅读素养计算机化自适应测试的技术与方法探析

    计算机技术与阅读,PISA2018阅读素养计算机化自适应测试的技术与方法探析原标题:PISA2018阅读素养计算机化自适应测试的技术与方法探析摘要:PISA2018阅读素养的计算机化自适应测试采用核心阶段、阶段1和阶段2的3阶段自适应测试,题库设定有245道题目,组成45个测试单元,并将其组合成若干题组,用于不同阶段的测试。在路径设计上,为避免位置效应问题,除核心阶段→阶段1→阶段2的标准路径之外,还采用核心阶段→阶段2→阶段1的替代路径。PISA2018阅读素养计算机…

    2022年6月5日
    25
  • 分布式锁—-数据库和redis实现分布式锁

    分布式锁—-数据库和redis实现分布式锁

    2021年8月3日
    72
  • 自学数据挖掘十大算法之AdaBoost「建议收藏」

    自学数据挖掘十大算法之AdaBoost「建议收藏」Adaboost简介:Adaboost(adaptiveboosting)是一种迭代算法,其核心思想是针对同一个训练集训练不同的分类器(弱分类器),然后把这些弱分类器集合起来,构成一个更强的最终分类器(强分类器)。其算法本身是通过改变数据分布来实现的,它根据每次训练集之中每个样本的分类是否正确,以及上次的总体分类的准确率,来确定每个样本的权值。该算法其实是一个简单的弱分类算法提升过程

    2022年5月4日
    41
  • 旅行者 问题_航空公司在浪费金钱,这就是旅行者的意义所在「建议收藏」

    旅行者 问题_航空公司在浪费金钱,这就是旅行者的意义所在「建议收藏」旅行者问题(WanttoreceiveBuy/Sell/Holdinyourinbox?Signuphere.)(是否希望在收件箱中收到购买/出售/持有?在这里注册。)WelcometoBuy/Sell/Hold,Marker’sweeklynewsletterthat’s100%businessintelligenceand0%invest…

    2022年7月13日
    14
  • html中三角向下符号,使用css实现三角符号效果[通俗易懂]

    html中三角向下符号,使用css实现三角符号效果[通俗易懂]关于使用css制作三角符号,网上有很多的例子了,在这里只是为了详细的向各位解释一下三角符号的原理下图,是一个长宽为100px,边框宽度为100px的一个元素,由此可见,在css中上下左右的边框相交处并不是一个直线,所以,可以根据此属性进行编写三角符号那么如何使用css的该属性来实现三角符号的效果呢,代码如下:html代码css代码div:after{position:absolute;width…

    2025年5月28日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号