Functions in C
- a01337106
- 12 feb 2015
- 2 Min. de lectura

This one is a program in C using functions:
#include <stdio.h>
void suma(int array[],int size);
int main()
{
int N=10;
int A[N];
suma(A,N);
}
void suma(int array[], int size){
int sum=0;
int i;
for (i=0; i<size-1; i++){
sum=sum+1;
}
printf("N-1 es = %d \n",sum);
}
As you can see a function is created before starting the main.
Void is the type of variable that will be returned
Suma is the name of the function
And inside the parenthesis are the external variables that will be used in the function.
We have this statement: (it calls the function)
suma(A,N)
First, it refers to the function, inside the parenthesis is stating (A,B). This refers to the established variables. In the function suma, we used to have the variables (int array[], int size), this variables are being set outside the function, in the main, but with other names. So, we can say that variable A[] is equal toarray[] and that variable N is equal to size. They only have different names inside and outside the function.
After the main the function is written.
First you open the function:
void suma(int array[], int size){
}
And inside the function you can write the code you want. In this case the function sums one each time a loop is executed. The result is N-1.
NOTE: Don’t forget that the value will be called inside the main. It will be called right after it is called.
Comentários