c函数大全_C函数

c函数大全

A function is a block of code that performs a particular task.

函数是执行特定任务的代码块。

There are many situations where we might need to write same line of code for more than once in a program. This may lead to unnecessary repetition of code, bugs and even becomes boring for the programmer. So, C language provides an approach in which you can declare and define a group of statements once in the form of a function and it can be called and used whenever required.

在许多情况下,我们可能需要在一个程序中多次编写同一行代码。 这可能导致不必要的代码重复,错误,甚至使程序员感到无聊。 因此,C语言提供了一种方法,您可以以函数的形式声明和定义一组语句,并且可以在需要时调用和使用它。

These functions defined by the user are also know as User-defined Functions

用户定义的这些功能也称为用户定义功能

C functions can be classified into two categories,

C函数可以分为两类,

  1. Library functions

    库功能

  2. User-defined functions

    用户定义的功能

types of functions in C

Library functions are those functions which are already defined in C library, example printf(), scanf(), strcat() etc. You just need to include appropriate header files to use these functions. These are already declared and defined in C libraries.

库函数是那些已经在C库中定义的函数,例如printf()scanf()strcat()等。您只需要包括适当的头文件即可使用这些函数。 这些已经在C库中声明和定义。

A User-defined functions on the other hand, are those functions which are defined by the user at the time of writing program. These functions are made for code reusability and for saving time and space.

另一方面, 用户定义的功能是由用户在编写程序时定义的功能。 这些功能旨在实现代码的可重用性并节省时间和空间。

使用功能的好处 (Benefits of Using Functions)

  1. It provides modularity to your program's structure.

    它为程序的结构提供了模块化。

  2. It makes your code reusable. You just have to call the function by its name to use it, wherever required.

    它使您的代码可重用。 您只需在需要时按其名称调用该函数即可使用它。

  3. In case of large programs with thousands of code lines, debugging and editing becomes easier if you use functions.

    对于具有数千行代码的大型程序,如果使用函数,调试和编辑将变得更加容易。

  4. It makes the program more readable and easy to understand.

    它使程序更具可读性和易于理解。

功能声明 (Function Declaration)

General syntax for function declaration is,

函数声明的一般语法是

returntype functionName(type1 parameter1, type2 parameter2,...);

Like any variable or an array, a function must also be declared before its used. Function declaration informs the compiler about the function name, parameters is accept, and its return type. The actual body of the function can be defined separately. It's also called as Function Prototyping. Function declaration consists of 4 parts.

像任何变量或数组一样,函数也必须在使用前声明。 函数声明将函数名称,接受的参数及其返回类型通知编译器。 函数的实际主体可以单独定义。 它也被称为功能原型 。 函数声明包括4个部分。

  • returntype

    返回类型

  • function name

    功能名称

  • parameter list

    参数表

  • terminating semicolon

    终止分号

返回类型 (returntype)

When a function is declared to perform some sort of calculation or any operation and is expected to provide with some result at the end, in such cases, a return statement is added at the end of function body. Return type specifies the type of value(int, float, char, double) that function is expected to return to the program which called the function.

当声明一个函数执行某种计算或任何操作并期望在最后提供一些结果时,在这种情况下,将在函数体的末尾添加一个return语句。 返回类型指定函数应返回到调用该函数的程序的值的类型( intfloatchardouble )。

Note: In case your function doesn't return any value, the return type would be void.

注意:如果您的函数不返回任何值,则返回类型为void

functionName (functionName)

Function name is an identifier and it specifies the name of the function. The function name is any valid C identifier and therefore must follow the same naming rules like other variables in C language.

函数名称是一个标识符 ,它指定函数的名称。 函数名称是任何有效的C标识符,因此必须像C语言中的其他变量一样遵循相同的命名规则。

参数表 (parameter list)

The parameter list declares the type and number of arguments that the function expects when it is called. Also, the parameters in the parameter list receives the argument values when the function is called. They are often referred as formal parameters.

参数列表声明了函数被调用时期望的参数类型和数量。 此外,调用函数时,参数列表中的参数会接收参数值。 它们通常被称为形式参数

实例时间 (Time for an Example)

Let's write a simple program with a main() function, and a user defined function to multiply two numbers, which will be called from the main() function.

让我们编写一个带有main()函数的简单程序,以及一个用户定义的函数以将两个数字相乘,这将从main()函数中调用。

#include<stdio.h>

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

int main() 
{
    int i, j, result;
    printf("Please enter 2 numbers you want to multiply...");
    scanf("%d%d", &i, &j);
    
    result = multiply(i, j);        // function call
    printf("The result of muliplication is: %d", result);
    
    return 0;
}

int multiply(int a, int b)
{
    return (a*b);       // function defintion, this can be done in one line
}

函数定义语法 (Function definition Syntax)

Just like in the example above, the general syntax of function definition is,

就像上面的例子一样,函数定义的一般语法是:

returntype functionName(type1 parameter1, type2 parameter2,...)
{
    // function body goes here
}

The first line returntype functionName(type1 parameter1, type2 parameter2,...) is known as function header and the statement(s) within curly braces is called function body.

第一行returntype functionName(type1 parameter1,type2 parameter2,...)被称为函数标头 ,花括号内的语句称为function body

Note: While defining a function, there is no semicolon(;) after the parenthesis in the function header, unlike while declaring the function or calling the function.

注意:定义函数时,函数标头中括号后没有分号( ; ),这与声明函数或调用函数不同。

功能体 (functionbody)

The function body contains the declarations and the statements(algorithm) necessary for performing the required task. The body is enclosed within curly braces { ... } and consists of three parts.

函数主体包含执行所需任务所需的声明和语句(算法)。 主体包含在花括号{ ... } ,并由三部分组成。

  • local variable declaration(if required).

    局部变量声明(如果需要)。

  • function statements to perform the task inside the function.

    函数语句以执行函数内部的任务。

  • a return statement to return the result evaluated by the function(if return type is void, then no return statement is required).

    一个返回语句,返回由函数求值的结果(如果return类型为void ,则不需要return语句)。

调用函数 (Calling a function)

When a function is called, control of the program gets transferred to the function.

调用函数时,程序的控制权将转移到该函数。

functionName(argument1, argument2,...);

In the example above, the statement multiply(i, j); inside the main() function is function call.

在上面的示例中,语句multiply(i, j); main()函数内部是函数调用。

将参数传递给函数 (Passing Arguments to a function)

Arguments are the values specified during the function call, for which the formal parameters are declared while defining the function.

参数是函数调用期间指定的值,在定义函数时为其声明形式参数。

passing arguments to functions in C

It is possible to have a function with parameters but no return type. It is not necessary, that if a function accepts parameter(s), it must return a result too.

可能有一个带有参数但没有返回类型的函数。 如果函数接受参数,则没有必要也必须返回结果。

Passing argument to function in C

While declaring the function, we have declared two parameters a and b of type int. Therefore, while calling that function, we need to pass two arguments, else we will get compilation error. And the two arguments passed should be received in the function definition, which means that the function header in the function definition should have the two parameters to hold the argument values. These received arguments are also known as formal parameters. The name of the variables while declaring, calling and defining a function can be different.

在声明函数时,我们声明了两个类型为int参数ab 。 因此,在调用该函数时,我们需要传递两个参数,否则会得到编译错误。 并且传递的两个参数应该在函数定义中接收,这意味着函数定义中的函数头应该具有两个参数来保存参数值。 这些收到的参数也称为形式参数 。 在声明,调用和定义函数时,变量的名称可以不同。

从函数返回值 (Returning a value from function)

A function may or may not return a result. But if it does, we must use the return statement to output the result. return statement also ends the function execution, hence it must be the last statement of any function. If you write any statement after the return statement, it won't be executed.

函数可能会也可能不会返回结果。 但是,如果这样做,则必须使用return语句输出结果。 return语句也结束了函数的执行,因此它必须是任何函数的最后一条语句。 如果在return语句之后编写任何语句,则不会执行该语句。

Return statement in C functions

The datatype of the value returned using the return statement should be same as the return type mentioned at function declaration and definition. If any of it mismatches, you will get compilation error.

使用return语句返回的值的数据类型应与函数声明和定义中提到的返回类型相同。 如果其中任何一个不匹配,您将得到编译错误。

In the next tutorial, we will learn about the different types of user defined functions in C language and the concept of Nesting of functions which is used in recursion.

在下一个教程中,我们将学习C语言中用户定义函数的不同类型,以及递归中使用的函数嵌套的概念。

翻译自: https://www.studytonight.com/c/user-defined-functions-in-c.php

c函数大全

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值