N n fatorial else printfnão existe fatorial de

Info icon This preview shows pages 6–15. Sign up to view the full content.

View Full Document Right Arrow Icon
if(n>=0){printf(“O fatorial de N = %d vale %d.\n”, n, fatorial);} else {printf(“Não existe fatorial de numero negativo\n”);} system("PAUSE"); return 0; }
Image of page 6

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Funções em Linguagem “C” Exemplo: Calcular o fatorial de um número n (com função): #include <stdio.h> #include <stdlib.h> void fat() { fatorial = 1; for(i=2;i<=n;i++) { fatorial = fatorial * i; } } int main() { int i,n,fatorial; printf(“Digite o numero N:”); scanf(“%d”, &n); fat(); if(n>=0){printf(“O fatorial de N = %d vale %d.\n”, n, fatorial);} else {printf(“Não existe fatorial de numero negativo\n”);} system("PAUSE"); return 0; }
Image of page 7
Funções em Linguagem “C” Exemplo: Calcular o fatorial de um número n (com função): #include <stdio.h> #include <stdlib.h> void fat() { int i,n,fatorial; fatorial = 1; for(i=2;i<=n;i++) { fatorial = fatorial * i; } } int main() { int i,n,fatorial; printf(“Digite o numero N:”); scanf(“%d”, &n); fat(); if(n>=0){printf(“O fatorial de N = %d vale %d.\n”, n, fatorial);} else {printf(“Não existe fatorial de numero negativo\n”);} system("PAUSE"); return 0; }
Image of page 8

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Funções em Linguagem “C” Até agora, nenhuma vantagem…….mas e se a função “retornar o valor do fatorial”????
Image of page 9
Funções em Linguagem “C” Com o retorno da função fat(), começa a ficar interessante…. #include <stdio.h> #include <stdlib.h> int i,n,fatorial; int fat() { fatorial = 1; for(i=2;i<=n;i++) { fatorial = fatorial * i; } return(fatorial); } int main() { printf(“Digite o numero N:”); scanf(“%d”, &n); if(n>=0){printf(“O fatorial de N = %d vale %d.\n”, n, fat() );} else {printf(“Não existe fatorial de numero negativo\n”);} system("PAUSE"); return 0; }
Image of page 10

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Funções em Linguagem “C” E se, ao invés de utilizar sempre a variável “n” como “parâmetro”, se pudéssemos variar….
Image of page 11
Funções em Linguagem “C” Função fat recebendo um número como parâmetro(por valor!) #include <stdio.h> #include <stdlib.h> int i,n,fatorial; int fat( int numero ) { fatorial = 1; for(i=2;i<= numero ;i++) { fatorial = fatorial * i; } return(fatorial); } int main() { printf(“Digite o numero N:”); scanf(“%d”, &n); if(n>=0){printf(“O fatorial de N = %d vale %d.\n”, n, fat(n) );} else {printf(“Não existe fatorial de numero negativo\n”);} system("PAUSE"); return 0; }
Image of page 12

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Funções em Linguagem “C” Agora o programa principal-função main- não precisa mais “conhecer” as variáveis i e fatorial. Portanto estas podem ser variáveis “locais” à função “fat”.
Image of page 13
Funções em Linguagem “C” Variáveis “locais” às funções “fat” e “main”.
Image of page 14

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern