English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Corso di base del linguaggio C

Controllo di flusso del linguaggio C

Funzione del linguaggio C

Array del linguaggio C

Puntatore del linguaggio C

Stringa del linguaggio C

Struttura del linguaggio C

File del linguaggio C

Altri C

Manuale di riferimento del linguaggio C

Funzione utente definita in C

In questo tutorial, imparerai a creare funzioni definite dall'utente nel linguaggio di programmazione C tramite esempi.

Le funzioni sono blocchi di codice che eseguono compiti specifici.

C ti permette di definire le funzioni come desideri. Queste funzioni si chiamano funzioni definite dall'utente. Ad esempio:

Supponiamo di dover creare un cerchio e colorarlo con un raggio e un colore. Puoi creare due funzioni per risolvere questo problema:

  • Funzione createCircle()

  • Funzione color()

Esempio: Funzione definita dall'utente

Questo è un esempio di aggiunta di due numeri interi. Per eseguire questa operazione, abbiamo creato una funzione definita dall'utente chiamata addNumbers().

#include <stdio.h>
int addNumbers(int a, int b); //函数原型
int main()
{
    int n1,n2,sum;
    printf("输入两个数字: ");
    scanf("%d %d",&n1,&n2);
    sum = addNumbers(n1, n2); //函数调用
    printf("sum = %d",sum);
    return 0;
}
int addNumbers(int a, int b) //函数定义
{
    int result;
    result = a+b;
    return result;                  //return statement
}

function prototype

The function prototype is just a declaration of the function, used to specify the name, parameters, and return type of the function. It does not contain the function body.

The function prototype provides information to the compiler that the function can be used later in the program.

function prototype syntax

returnType functionName(type1 argument1, type2 argument2, ...);

In the above example, the function prototype int addNumbers(int a, int b); provides the following information to the compiler:

  1. The name of the function is addNumbers()

  2. The return type of the function is int

  3. Two parameters of type int are passed to the function

If the user-defined function is defined before the main() function, a function prototype is not required.

Calling a function

Program control is transferred to the user-defined function through a call.

function call syntax

functionName(argument1, argument2, ...);

In the above example, the function call addNumbers(n1, n2); is made using the statements inside the main() function.

function definition

The function definition contains a code block that executes a specific task. In our example, it adds two numbers and returns.

function definition syntax

returnType functionName(type1 argument1, type2 argument2, ...)
{
    //function body
}

When a function is called, program control is transferred to the function definition, and the compiler begins to execute the code within the function.

Passing parameters to a function

In programming, a parameter refers to a variable passed to a function. In the above example, two variables n1 and n2 were passed during the function call.

Parameters a and b accept the parameters passed in the function definition. These parameters are called the formal parameters of the function.

The type and format of the parameters passed to the function must match the formal parameters, otherwise, the compiler will raise an error.

If n1 is of char type, a should also be of char type. If n2 is of floating-point type, then the variable b should also be of floating-point type.

Functions can also be called without passing any parameters.

return statement

The return statement terminates the execution of the function and returns a value to the calling function. After the return statement, program control is transferred to the calling function.

In the above example, the value of the result variable is returned to the main function. The sum variable in the main() function is assigned this value.

return statement syntax

return (expression);

Ad esempio,

return a;
return (a+b);

Il tipo di valore restituito dalla funzione deve corrispondere al tipo di restituzione specificato nel prototipo e nella definizione della funzione.

Visita questa pagina per informazioni suPassaggio di parametri e restituzione di valori dalla funzionePer ulteriori informazioni.