70 likes | 199 Vues
This article dives into the concept of functions in C programming, highlighting their structure and usage. We will explore the `main()` function and user-defined functions, showcasing how to calculate the average of two numbers. You'll learn the significance of functions, including variable scope, return values, and how to implement and call functions effectively. Examples provided will reinforce your understanding, enabling you to write more modular and organized C code.
E N D
Funções em C #include <stdio.h> #include <stdlib.h> int main(void) { linha(); printf("\xDB Um programa em C \xDB\n"); linha(); system("pause"); } linha(void) { int j; for (j=1; j<=20; j++) printf("\xDB"); printf("\n"); }
Funções em C • Tem estrutura semelhante a função main(); • São chamadas (usadas) da mesma forma que usamos funções de C (printf(), scaf(), gets(), getche(), etc) • Retorna valor através do comando return(); • Termina função com comando return;
#include <stdio.h> #include <stdlib.h> float average(float a, float b); void linha(void); int main(void) { linha(); printf("\xDB Um programa em C \xDB\n"); linha(); /*****Cáculo da média entre dois números**************/ printf("\n%f",average(10.0,5.0)); system("pause"); } void linha(void) { int j; for (j=1; j<=20; j++) printf("\xDB"); printf("\n"); } float average(float a, float b) { float ave; ave = (a + b) / 2; return ave; }
float average(int size, float list[]){ int k; float sum = 0.0; for (k=0; k<size; k++) sum += list[k]; return sum / size;} void print_table(int x_size, int y_size, float table[][5]);{ int i, j; for (i = 0; i < xsize; i++) { for (j = 0; j < y_size; j++) printf("\t%f", (double) table[i][j]); printf("\n"); }} Funções e Vetores
float abs (int x) { ... } void abs (int x) { ... } Cabeçalho de Função abs(x) int x; { ... } abs(int x) { ... }
Escopo de variáveis • Variáveis globais: • Podem ser vista em todo o programa e são declaradas fora das funções • Só são vistas dentro das funções em que foram declaradas