Vous êtes sur la page 1sur 10

Function

1. 1. Function Introduction( 1 ) 1. 6. Function Pointer( 4 )


1. 2. Function Definition( 1 ) 1. 7. Function Return( 3 )
1. 3. Function Prototype( 3 ) 1. 1. Recursive Function( 3 )
1. 4. Function Call( 1 ) 1. 9. Variable Number of Arguments( 1 )
1. 5. Function Parameter( 6 )

1. 1. 1. FUNCTON
Functions provides modularity to the software.

#include <stdio.h>

int add (int x, int y) {


int z;
z = x + y;
return (z);
}
main ()
{
int i, j, k;
i = 10;
j = 20;
k = add(i, j);
printf ("The value of k is %d\n", k);
}

The value of k is 30

1. 2. 1. SYNTAX OF FUNCTON DEFNTON


The general format of a function is

<Return type> <Function name> <Parameter list>{


local definitions;
statements;
Return value;
}

#include <stdio.h>

int f1 (int j, int f){


int k;
k = j + f;
return k;
}

main(){
int i=3;
int j = 6;

int k = f1(i,j);

printf("%d",k);
}

9
1. 3. 1. FUNCTON PROTOTYPE
#include <stdio.h>

int change(int number); /*Function prototype */

int main(void)
{
int number = 10;
int result = 0;

result = change(number);
printf("\nIn main, result = %d\tnumber = %d", result, number);
return 0;
}

int change(int number)


{
number = 2 * number;
printf("\nIn function change, number = %d\n", number);
return number;
}

In function change, number = 20

In main, result = 20 number = 10

1. 3. 2. FUNCTON S DEFNED AFTER MAN: PROTOTYPE THE FUNCTON

#include <stdio.h>

f1(int *k);

main ( ){
int i;

i = 0;
printf (" The value of i before call %d \n", i);
f1 (&i);
printf (" The value of i after call %d \n", i);
}

f1(int *k)
{
*k = *k + 10;
}

The value of i before call 0


The value of i after call 10
1. 3. 3. FUNCTON S DEFNED ABOVE THE FUNCTON MAN:
t s Not Necessary To Prototype The Function

#include <stdio.h>

void f1(int *k)


{
*k = *k + 10;
}

main ( ){
int i;
i = 0;
printf (" The value of i before call %d \n", i);
f1 (&i);
printf (" The value of i after call %d \n", i);
}

The value of i before call 0


The value of i after call 10

1. 4. 1. FUNCTON CALLS FUNCTON

#include<stdio.h>

f1 (void)
{
printf ("f1-1 \n");
f2 ( );
printf ("f1-2 \n");
}
f2 ()
{
printf ("f2 \n");
}
main ( )
{
printf ("1 \n");
f1 ( );
printf ("2 \n");
}

1
f1-1
f2
f1-2
2
1. 5. FUNCTON PARAMETER
1. 5. 1. PARAMETER PASSNG
1. 5. 2. CALL BY REFERENCE
1. 5. 3. PASS VARABLES TO FUNCTON
1. 5. 4. USE PONTER AS FUNCTON PARAMETER
1. 5. 5. CUBE A VARABLE USNG CALL-BY-VALUE
1. 5. 6. CUBE A VARABLE USNG CALL-BY-REFERENCE WTH A PONTER
ARGUMENT

1 5. 1. PARAMETER PASSNG
Information can be passed into function.

#include<stdio.h>

main ( )
{
int i;
i = 0;
printf (" The value of i before call %d \n", i);
f1 (i);
printf (" The value of i after call %d \n", i);
}
f1 (int k)
{
k = k + 10;
}

The value of i before call 0


The value of i after call 0

1. 5. 2. CALL BY REFERENCE

#include<stdio.h>
main ( )
{
int i;
i = 0;
printf (" The value of i before call %d \n", i);
f1 (&i);
printf (" The value of i after call %d \n", i);
}
f1(int *k)
{
*k = *k + 10;
}

The value of i before call 0


The value of i after call 10
1. 5. 3. PASS VARABLES TO FUNCTON

#include <stdio.h>

float average(float x, float y)


{
return (x + y)/2.0f;
}

int main(void)
{
float value1 = 1.0F;
float value2 = 2.0F;
float value3 = 0.0F;

value3 = average(value1, value2);


printf("\nThe average is: %f\n", value3);
return 0;
}

The average is: 1.500000

1. 5. 4. USE PONTER AS FUNCTON PARAMETER

#include <stdio.h>

int change(int* pnumber); /* Function prototype */

int main(void)
{
int number = 10;
int *pnumber = &number;
int result = 0;

result = change(pnumber);
printf("\nIn main, result = %d\tnumber = %d", result, number);
return 0;
}

int change(int *pnumber)


{
*pnumber *= 2;
printf("\nIn function change, *pnumber = %d\n", *pnumber );

return *pnumber;
}

In function change, *pnumber = 20

In main, result = 20 number = 20


1. 5. 5. CUBE A VARABLE USNG CALL-BY-VALUE

#include <stdio.h>

int cubeByValue( int n );

int main()
{
int number = 5;

printf( "The original value of number is %d", number );

/* pass number by value to cubeByValue */


number = cubeByValue( number );

printf( "\nThe new value of number is %d\n", number );

return 0;

int cubeByValue( int n ) {


return n * n * n;

The original value of number is 5


The new value of number is 125

1. 5. 6. CUBE A VARABLE USNG CALL-BY-REFERENCE WTH A PONTER


ARGUMENT

#include <stdio.h>

void cubeByReference( int *nPtr );

int main()
{
int number = 5;

printf( "The original value of number is %d", number );

cubeByReference( &number );

printf( "\nThe new value of number is %d\n", number );

return 0;

void cubeByReference( int *nPtr )


{
*nPtr = *nPtr * *nPtr * *nPtr; /* cube *nPtr */
}

The original value of number is 5


The new value of number is 125

1. 6. FUNCTON PONTER
1. 6. 1. PONTNG TO FUNCTONS
1. 6. 2. ARRAYS OF PONTERS TO FUNCTONS
1. 6. 3. USNG ARRAY OF PONTERS TO FUNCTONS N ONE STATEMENT
1. 6. 4. PASSNG A PONTER OF A FUNCTON TO ANOTHER FUNCTON

1. 6. 1. PONTNG TO FUNCTONS

#include <stdio.h>

int sum(int x, int y)


{
return x + y;
}

int product(int x, int y)


{
return x * y;
}

int difference(int x, int y)


{
return x - y;
}

int main(void)
{
int a = 10;
int b = 5;
int result = 0;
int (*pfun)(int, int); /* Function pointer declaration */

pfun = sum;
result = pfun(a, b); /* Call sum() through pointer */
printf("\npfun = sum result = %d", result);

pfun = product;
result = pfun(a, b); /* Call product() through pointer */
printf("\npfun = product result = %d", result);

pfun = difference;
result = pfun(a, b); /* Call difference() through pointer */
printf("\npfun = difference result = %d\n", result);
return 0;
}

pfun = sum result = 15


pfun = product result = 50
pfun = difference result = 5
1. 6. 2. ARRAYS OF PONTERS TO FUNCTONS

#include <stdio.h>

int sum(int x, int y){


return x + y;
}

int product(int x, int y){


return x * y;
}

int difference(int x, int y){


return x - y;
}

int main(void){
int a = 10;
int b = 5;
int result = 0;
int (*pfun[3])(int, int); /* Function pointer array declaration */

/* Initialize pointers */
pfun[0] = sum;
pfun[1] = product;
pfun[2] = difference;

int i;
for(i = 0 ; i < 3 ; i++)
{
result = pfun[i](a, b); /* Call the function through a pointer */
printf("\nresult = %d", result); /* Display the result */
}

return 0;
}

result = 15
result = 50
result = 5

1. 6. 3. USNG ARRAY OF PONTERS TO FUNCTONS N ONE STATEMENT

#include <stdio.h>

int sum(int x, int y){


return x + y;
}

int product(int x, int y){


return x * y;
}

int difference(int x, int y){


return x - y;
}

int main(void){
int a = 10;
int b = 5;
int result = 0;
int (*pfun[3])(int, int); /* Function pointer array declaration */

/* Initialize pointers */
pfun[0] = sum;
pfun[1] = product;
pfun[2] = difference;

/* Call all three functions through pointers in an expression */


result = pfun[1](pfun[0](a, b), pfun[2](a, b));
printf("\n\nThe product of the sum and the difference = %d\n",
result);

return 0;
}

The product of the sum and the difference = 75

1. 6. 4. PASSNG A PONTER OF A FUNCTON TO ANOTHER FUNCTON

#include <stdio.h>

int any_function(int(*pfun)(int, int), int x, int y){


return pfun(x, y);
}

int sum(int x, int y){


return x + y;
}

int product(int x, int y){


return x * y;
}

int difference(int x, int y){


return x - y;
}

int main(void){
int a = 10;
int b = 5;
int result = 0;
int (*pf)(int, int) = sum; /* Pointer to sum function */

result = any_function(pf, a, b);


printf("\nresult = %d", result );

result = any_function(product,a, b);


printf("\nresult = %d", result );
printf("\nresult = %d\n", any_function(difference, a, b));
return 0;
}

result = 15
result = 50
result = 5

1. 7. FUNCTON RETURN
1. 7. 1. IF THE FUNCTON DOES NOT RETURN ANY VALUE
1. 7. 2. RETURN VALUE AS PONTER
1. 7. 3. FUNCTONS THAT RETURN VALUE

Vous aimerez peut-être aussi