You are on page 1of 4

EJEMPLO: PROGRAMA QUE USA UNA FUNCION PARA CALCULAR EL CUBO DE UN

NUMERO ENTERO
/*PROGRAMA FUNCION CUBO*/
#include <stdio.h>
#include <conio.h>
long cubo(long x);  PROTOTIPO DE FUNCION
long entrada, resultado;  VARIABLES GLOBALES

void main()
{
textcolor(5);
textbackground(16);
clrscr();
puts("\n+-------------------------------------------------------------------------+");
printf("\n|PROGRAMA QUE USA UNA FUNCION PARA CALCULAR EL CUBO DE UN NUMERO ENTERO |");
puts("\n+-------------------------------------------------------------------------+");
printf("\n\n INTRODUZCA NUMERO:");
scanf("%d", &entrada);  DATOS DE ENTRADA
resultado=cubo(entrada);  LLAMADA A FUNCION ENTREGANDO VALOR DE ENTRADA
printf("\n\n EL CUBO DE %ld es: %ld ", entrada, resultado);  RESULTADO DEVUELTO POR LA FUNCION CUBO
}
long cubo(long x)  FUNCION CUBO (PARAMETRO FORMAL)
{
long calculacubo;  VARIABLE LOCAL
calculacubo=x*x*x;  CUERPO DE FUNCION
return calculacubo;  RESULTADO DEVUELTO A LA FUNCION LLAMADORA main
}
CORRIDA DEL PROGRAMA
Programa: Obtener el Cubo de un Numero
Programador: XXXXXXXXX
Sección: PD
-----------------------------------------------------------------

Ingrese Numero al que desee calcular el Cubo: 5

El cubo del Numero 5 es :


125

Desea Continuar con otro numero(s/n)========= >


PASO DE ARGUMENTOS A UNA FUNCION
Cada vez que una función es llamada, los argumentos son pasados a los parámetros
formales de la función
• POR VALOR: copia el valor de un argumento en el parámetro formal de la función. Por lo
tanto, los cambios en los parámetros de la función no afectan a las variables que se usan
en la llamada. void func (int i)
EJEMPLO: 6 {
printf(“%d”, i);
main()
i++;
{ }
int i=6;
func(i);
return 0;
}

• POR REFERENCIA: se copia la dirección del argumento en el parámetro.


#include <stdio.h> PASO POR REFERENCIA
#include<conio.h>
void intercambio(int *x,int *y);
void main()
{
int a=1,b=2;
printf("\n Programa que intercambia valor de variables argumento por referencia");
printf("\n\n\ta=%d b=%d\n",a,b);
intercambio(&a,&b);
printf("\n\ta=%d b=%d",a,b);
printf("\n\n\tPROGRAMA TERMINADO. PRESIONE ENTER PARA SALIR");
getch();
}

void intercambio(int *x, int *y)


{
int temp;
temp=*x;
*x=*y;
*y=temp;
printf("\n\tx=%d y=%d temp=%d \n",*x,*y,temp);
}

You might also like