English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Neste tutorial, você aprenderá a criar funções definidas pelo usuário em linguagem C com a ajuda de exemplos.
As funções são blocos de código que executam tarefas específicas.
O C permite que você defina funções conforme necessário. Essas funções são chamadas de funções definidas pelo usuário. Por exemplo:
Suponha que você precise criar um círculo e pintá-lo com uma cor específica com base no raio e na cor. Você pode criar duas funções para resolver este problema:
Função createCircle()
Função color()
Este é um exemplo de adição de dois inteiros. Para executar esta tarefa, criamos uma função definida pelo usuário chamada addNumbers().
#include <stdio.h> int addNumbers(int a, int b); //Function prototype int main() int n1,n2,sum; printf("Digite dois números: "); scanf("%d %d",&n)1&n2); sum = addNumbers(n1, n2); // Function call printf("sum = %d",sum); return 0; int addNumbers(int a, int b) //Function definition int result; result = a+b; return result; //Return statement
Function prototypes are just declarations of the function, used to specify the name, parameters, and return type of the function. It does not contain the function body.
Function prototypes provide information to the compiler that the function can be used later in the program.
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:
The name of the function is addNumbers()
The return type of the function is int
Two parameters of type int are passed to the function
If the user-defined function is defined before the main() function, then it is not necessary to have a function prototype.
Program control is transferred to the user-defined function through a call.
functionName(argument1, argument2, ...);
In the above example, the function addNumbers(n1, n2); call.
Function definitions contain code blocks that execute specific tasks. In our example, we add two numbers and return.
Function definition syntax
returnType functionName(type1 argument1, type2 argument2, ...) //Function body
When calling a function, program control is transferred to the function definition, and the compiler begins to execute the code within the function.
In programming, parameters refer to variables passed to a function. In the above example, two variables n1and n2
The parameters a and b accept the parameters passed in the function definition. These parameters are called 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 generate an error.
if n1is of char type, then a should also be of char type. If n2If it is a floating-point type, then the variable b should also be a floating-point type.
Functions can also be called without passing any parameters.
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 (expressão);
Por exemplo:
return a; return (a+b);
O tipo de valor retornado da função deve coincidir com o tipo de retorno especificado no protótipo e na definição da função.
Acesse esta página para saber sobrePassar parâmetros e retornar valores da funçãoMais informações.