C 语言中用户定义函数的类型

原文:https://www.studytonight.com/c/type-of-functions-and-recursion.php

用户自定义的功能可以有 4 种不同的类型,它们是:

  1. 没有参数和返回值的函数
  2. 没有参数和返回值的函数
  3. 带参数且无返回值的函数
  4. 带有参数和返回值的函数

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


没有参数和返回值的函数

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

下面是一个函数的例子,它从用户那里得到 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);
    }
}

没有参数和返回值的函数

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

#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;
}

带参数且无返回值的函数

我们一次又一次地使用同一个函数作为例子,来证明解决一个问题可以有许多不同的方法。

这一次,我们修改了上面的例子,让函数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);
    }
}

带有参数和返回值的函数

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

#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;
    }
}

函数的嵌套

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

 function1()
{
    // function1 body here

    function2();

    // function1 body here
}

如果 function2()内部也有对 function1()的调用,那么在这种情况下,它将导致无限嵌套。他们会一直互相呼叫,程序永远不会终止。

不能够理解?让我们考虑一下在main()函数内部,函数 1()被调用,它的执行开始,然后在函数 1()内部,我们调用函数 2(),所以程序的控制将转到函数 2()。但是由于 function2()的主体中也有对 function1()的调用,所以它将调用 function1(),而 function 1()将再次调用 function2(),并且这将持续无限次,直到您强制退出程序执行。


什么是递归?

递归是嵌套函数的一种特殊方式,函数在其中调用自己。我们必须在函数中有一定的条件才能打破递归,否则递归会发生无限次。

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

示例:使用递归对数字进行阶乘

#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;
}

同样,递归在 C 语言中还有更多的应用。转到程序部分,了解更多使用递归的程序。

既然我们已经了解了数据结构中的栈,您也可以查看以下主题: