Вы находитесь на странице: 1из 12

Functions

Outline
Introduction
Program Modules in C
Functions
Function Definitions
Calling Functions: Call by Value and Call by Reference
Example: A Game of Chance
Recursion
Example Using Recursion: The Fibonacci Series
Introduction
• A complex problem is often easier to solve by dividing it into
several smaller parts, each of which can be solved by itself.
• This is called structured programming.
• main() then uses these functions to solve the original problem.
• Divide and conquer
– Construct a program from smaller pieces or components
• These smaller pieces are called modules
– Each piece more manageable than the original program
Program Modules in C
• Functions
– Modules in C
– Programs combine user-defined functions with library functions
• C standard library has a wide variety of functions
• Function calls
– Invoking functions
• Provide function name and arguments (data)
• Function performs operations or manipulations
• Function returns results
– Function call analogy:
• Boss asks worker to complete task
– Worker gets information, does task, returns result
– Information hiding: boss does not know details
Functions
• Functions
– Modularize a program
– All variables declared inside functions are local variables
• Known only in function defined
– Parameters
• Communicate information between functions
• Local variables
• Benefits of functions
– Divide and conquer
• Manageable program development
– Software reusability
• Use existing functions as building blocks for new programs
• Abstraction - hide internal details (library functions)
– Avoid code repetition
Function Definitions
• Function definition format
return-value-type function-name( parameter-list )
{
declarations and statements
}
– Function-name: any valid identifier
– Return-value-type: data type of the result (default int)
• void – indicates that the function returns nothing
– Parameter-list: comma separated list, declares parameters
• A type must be listed explicitly for each parameter unless, the
parameter is of type int
Enter three integers: 22 85 17
Maximum is: 85
C – Function Argument, return value

• All C functions can be called either with arguments or


without arguments in a C program. These functions
may or may not return values to the calling function.

– C function with arguments (parameters) and with return value


– C function with arguments (parameters) and without return value
– C function without arguments (parameters) and without return value
– C function without arguments (parameters) and with return value
S.no C function syntax
1 with arguments and with int function ( int ); // function declaration
return values function ( a ); // function call
int function( int a ) // function definition
{statements; return a;}

2 with arguments and without void function ( int ); // function declaration


return values function( a ); // function call
void function( int a ) // function definition
{statements;}

3 without arguments and without void function(); // function declaration


return values function(); // function call
void function() // function definition
{statements;}

4 without arguments and with int function ( ); // function declaration


return values function ( ); // function call
int function( ) // function definition
{statements; return a;}
Calling Functions: Call by Value and Call by Reference
• Call by value
– In call by value method, the value of the variable is passed to the
function as parameter.
– The value of the actual parameter can not be modified by formal
parameter.
– Different Memory is allocated for both actual and formal parameters.
Because, value of actual parameter is copied to formal parameter.
• Call by reference
– In call by reference method, the address of the variable is passed to the
function as parameter.
– The value of the actual parameter can be modified by formal parameter.
– Same memory is used for both actual and formal parameters since only
address is used by both parameters.
Example: Call by Value
#include<stdio.h>
// function prototype, also called function declaration
void swap(int a, int b);
int main()
{
int m = 22, n = 44;
printf(" values before swap m = %d \nand n = %d", m, n);
// calling swap function by value
swap(m, n);
}
void swap(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
printf(" \nvalues after swap m = %d\n and n = %d", a, b);
}
Example: Call by Reference
#include<stdio.h>
// function prototype, also called function declaration
void swap(int *a, int *b);
int main()
{
int m = 22, n = 44;
// calling swap function by reference
printf("values before swap m = %d \n and n = %d",m,n);
swap(&m, &n);
}
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
printf("\n values after swap a = %d \nand b = %d", *a, *b);
}
Recursion
• The C programming language supports recursion, i.e., a
function to call itself. But while using recursion,
programmers need to be careful to define an exit condition
from the function, otherwise it will go in infinite loop.
void recursion()
{
recursion(); /* function calls itself */
}
int main()
{
recursion();
}

Вам также может понравиться