Vous êtes sur la page 1sur 43

Functions

FUNCTIONS
• A complete and independent
subprogram that can be developed
and tested successfully
What is a Function?
• The function takes a data from main ()
function and returns a value.

• Calling Function - sends the data to the


function
• Called Function - function which is called
by the calling function
What is a Function?
#include <stdio.h>
int main (void)
{
printf ("Programming is interesting.\n");
return 0;
}
What is a Function?
#include <stdio.h>
int main (void)
{
printMessage ();
return 0;
}

void printMessage (void)


{
printf ("Programming is interesting.\n");
}
Functions are Used in C for the
Following Reasons
1. Thus the use of a function avoids the
need for redundant (repeated)
programming of the same instructions.
2. A different set of data can be transferred
to the function each time it is accessed.
3. Logical clarity is achieved by using
functions in Programs.
Functions are Used in C for the
Following Reasons
4. Testing and correcting errors is easy because
errors are localized.
5. The flow of program and its code are easily
understandable since the readability is
enhanced while using the functions.
6. A single function written in a program can
also be used in other programs also.
7. A function also promotes portability since
programs can be written that are independent
of system-dependent features.
Classification
C functions can be classified into two
categories:
1. library functions - standard functions
available within C-Language library (built in
functions)
2. user-defined functions - functions that are
created by the user
Structure of Function
1. Function definition
2. Function declaration
3. Function invocation
Function Definition
The function definition is the C code
that implements what the function
does.
It contains two parts.
1. function header
2. function body
Function Definition
Function definition has the following
syntax
Function Definition
Function Header
DATA TYPE
The data type in the function header tells the
type of the value returned by the function.

When a function is not returning any value it


may be declared as type void. For example,

Void function name(…)


int function name(…)
float function name(…)
char function name(…)
function name(…)
DATA TYPE
Consider a function which just shows a message
on the screen. In this function there is no need
to return any value to the calling program.
Function name
The function name can be anything. Normally it
is named relevant to the function operation, as it
is easy to keep the tack of functions.

counter();
square();
message();
output();
Arguments List
The arguments are also called as parameters.
This tells what arguments the function needs
when it is called (and what their types are). The
different parameters are separated by commas.

int square(int x,int y);


void name(double a, char car);
float space(p,x,y);
Function Body
Whatever is written with in the curly braces { }
below the function header. The function body
contains three parts:
1. Local variables declaration
- It specifies the variables needed by the function
locally.
2. Function Statements
- That actually performs task of the function.
3. The return statement
- Generally return statement is used at the end
of function body.
Function Body
The keyword return is used to terminate the
function and return a value. The return
statement may or may not include an
expression. Syntax of return statement:

return;
return (expression);
Ex: return;
return 3;
return n;
return ++a;
return (a*b);
Function Body
float add numbers (float n1, float n2)
{
return n1 + n2; /*legal*/
return 6; /*illegal, not the same data type*/
return 6.0; /*legal*/
}
Function Prototypes
For functions the declaration needs to be
done before the first call of the function.
There are two ways to do this. They are:

1. Define the function before it is called


2. Declare the function before it is called.
Function Prototypes
This program allows the user to input two
numbers, then passes those two numbers
into a sum function, and finally prints the
returned sum.
Declare the function before it is called
Function Prototypes
#include <stdio.h>
int sum (int n1, int n2) /* Function header*/
{
int answer; /*Function body */
answer = n1 + n2; /*Put the sum of the two numbers into answer */
return answer(); /* Exit function and return answer */
}
main ()
{
int number1,number2,total;
printf ("What is the first number? ");
scanf ("%d", &number1);
printf ("What is the second number? ");
scanf ("%d", &number2);
total = sum (number1, number2); /* Get the sum of the two numbers from the return value of sum() */
printf ("The sum of %d plus %d is %d\n", number1, number2, total);
}
Function Invocation
The function is called (or invoked) from main ().
To invoke a function in main, the function name is
written, followed by parentheses.

If function is returning a value, variable can be


assigned to the return value of the function. For
example,
Function Invocation
#include <stdio.h>
int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}
int main ()
{
int z;
z = addition (5,3);
Printf(“The result is%d “,z);
return 0;
}
Returning values from functions
square (float X)
{
float y;
y = x * x;
return (y);
}
main ( )
{
float a,b,
printf ("\n Enter any number:");
scanf ("\% f", &a );
b = square (a)
printf ("\n square of entered number % f is % f", a,b);
}
Returning values from functions
float square (float X)
{
float y;
y = x * x;
return (y);
}
main ( )
{
float a,b,
printf ("\n Enter any number:");
scanf ("\% f", &a );
b = square (a)
printf ("\n square of entered number % f is % f", a,b);
}
Some facts about functions
1. C program is a collection of one or more
functions.

2. A function gets called when the function name is


followed by a semicolon. Rules for declaring the
functions name are same as that of variables
declaration rules. for e.g.
main ( )
{
{
int Message1 ( );
}
}
Some facts about functions
3. A function is defined when function name is
followed by a pair of braces in which one or more
statements may be present for e.g.

int message1 ( )
{
statement 1;
statement2;
statement 3;
}
Some facts about functions
4. Any function can be called from any other function
even main ( ) can be called from other functions. for e.g.

main ( )
{
message ( );
}
int message ( )
{
printf (“ \n Hello”);
main ( );
return 0
}
Some facts about functions
5. A function can be called any number
of times for eg.
message ( )
{
printf (“\n Hello”);
}
main ()
{
message ( );
message ( );
}
Some facts about functions
6. The order in which the functions are
defined in a program and the order in which
they get called need not necessarily be same
for e.g.
Some facts about functions
7. A function can call itself such a
process as called ‘recursion’.
8. A function can be called from other
function, but a function cannot be
defined in another function.
9. Any C program contains at least one
function
10. If a program contains only one
function, it must be main ( )
Types of Functions
1) A function with no arguments and
no return value
2) A function with no arguments and
return a value
3) A function with an argument or
arguments and returning no value
4) A function with arguments and
returning a value.
A Function With No Arguments And
No Return Value
A Function With No Arguments And
No Return Value
main( )
{
message ( );
}
void message( )
{
printf(“\n WELCOME”);
}
A Function with No Arguments and
Returns a Value
A function which does not get value from the calling
function but it can return a value to calling program.
This can be represented as given below.
A Function with Arguments and
Returns No Value
A Function with Arguments and
Returns No Value
#include
main()
{
int a,b;
printf(“Enter the two numbers”);
scanf(“%d%d”,&a,&b);
largest(a,b)
}

/*Function to find the largest of two numbers*/


largest(int a, int b)
{
if(a>b)
printf(“Largest element=%d”,a);
else
printf(“Largest element=%d”,b);
}
A Function with Arguments and
Returning a Value

fun (a,b)
int a,b;
{
int y,z;
y=a+b;
z=a-b;
return y;
return z; /* Two return statements for one single function called is invalid * /
}
A Function with Arguments and
Returning a Value
A Function with Arguments and
Returning a Value
#include<stdio.h>
float findaverage(float a, float b); /*Function prototype*/
main()
{
float a=5,b=15,result;
result=findaverage(a,b);
printf("average=%f \ n",result); /*Function prototype*/
}

float findaverage(float a, float b)


{
float average;
average=(a+b)/2;
return(average);
}

Vous aimerez peut-être aussi