4_Function
4_Function
C
Prof. Dr. Md. Al Mamun
Method, Procedure, Function or Sub-
routine
• Function is a group of statements that together perform a
task.
• You can divide up your code into separate functions. How you divide
up your code among different functions is up to you, but logically
the division usually is so each function performs a specific task.
Function Name: This is the actual name of the function. The function
name and the parameter list together constitute the function signature.
procedure sayHello
{
output “Hello World!”
}
Main Program
{
do procedure sayHello
}
Example: hello1.c #include <stdio.h>
/*
Prints a simple greeting. * Print a simple greeting.
*/
/*
* Print a simple greeting.
*/
void sayHello ( void )
Function {
definition printf(“Hello World!\n”);
}
/*
* Call a function which
* prints a simple greeting.
*/
int main(void)
{
Function call sayHello();
return 0;
}
Example: hello1.c #include <stdio.h>
/*
Function name * Print a simple greeting.
*/
void sayHello ( void )
{
Function body printf(“Hello World!\n”);
}
/*
* Call a function which
* prints a simple greeting.
*/
int main(void)
{
sayHello();
return 0;
}
Example: hello1.c #include <stdio.h>
/*
Return type * Print a simple greeting.
*/
void sayHello ( void )
{
printf(“Hello World!\n”);
}
/*
Formal * Call a function which
* prints a simple greeting.
Parameter List */
int main(void)
{
sayHello();
return 0;
}
Parameters
• Information passed to a function
• “Formal” parameters are local variables
declared in the function declaration.
• “Actual” parameters are values passed to the
function when it is called.
Example: badsort.c
/* Print two numbers in order. */
if ( a > b )
{
}
printf("%d %d\n", b, a); Parameters
else ( Arguments)
{
printf("%d %d\n", a, b);
}
}
Example: badsort.c
/* Print two numbers in order. */
if ( a > b )
{
printf("%d %d\n", b, a);
}
else
{
printf("%d %d\n", a, b);
}
}
Example: badsort.c Formal
parameters
/* Print two numbers in order.
*/
temp = a; return 0;
a = b; }
b = temp;
Output: 3 5
Example: badswap.c
/* Swap the values of two int main(void)
variables. */ {
void badSwap ( int a, int b ) int a = 3, b = 5;
{
int temp; printf("%d %d\n",a,b);
badSwap ( a, b );
temp = a; printf("%d %d\n",a,b);
a = b;
b = temp; return 0;
}
printf("%d %d\n", a, b);
}
Output: 3 5
5 3
Example: badswap.c
/* Swap the values of two int main(void)
variables. */ {
void badSwap ( int a, int b ) int a = 3, b = 5;
{
int temp; printf("%d %d\n",a,b);
badSwap ( a, b );
temp = a; printf("%d %d\n",a,b);
a = b;
b = temp; return 0;
}
printf("%d %d\n", a, b);
}
Output: 3 5
5 3
3 5
Example: badswap.c
/* Swap the values of two int main(void)
variables. */ {
void badSwap ( int a, int b ) int a = 3, b = 5;
{
int temp; printf("%d %d\n",a,b);
badSwap ( a, b );
temp = a; printf("%d %d\n",a,b);
a = b;
b = temp; return 0;
}
printf("%d %d\n", a, b);
}
if (a > b)
{
result = a;
}
else
{
result = b;
}
return result;
}
Example: max.c
/* Returns the larger of two
numbers. */
int max (int a, int b)
{
int result;
For example:
if (a > b)
{
result = a;
The value of the
} expression
else
{
result = b;
max(7,5)
}
is the integer 7.
return result;
}
Example: max.c
if (a > b)
{
result = a;
}
else
{
result = b;
}
return result;
}
Return Values (cont.)
• If a function does not return a value, declare
its return type void.
return 0;
}
Example: isNegative.c
#include <stdio.h>
int isNegative (int); int
isNegative ( int n )
int main (void) {
{ int result;
int number; if ( n<0 )
printf ("Enter an integer: "); {
scanf ("%d",&number); result=1;
}
if (isNegative(number)) else
{ {
printf("Negative\n"); result = 0;
} }
else return result;
{ }
printf("Positive\n");
} Function Call
(Must be after prototype, but can be
return 0; before definition)
}
Example: isNegative.c
#include <stdio.h>
int isNegative (int); int
isNegative
Header files (filename.h) contain(function
int n )
int main (void) prototypes and{global variable declarations
{ int result;
int number; if ( n<0 )
printf ("Enter an integer: "); {
scanf ("%d",&number); result=1;
}
if (isNegative(number)) else
{ {
printf("Negative\n"); result = 0;
} }
else return result;
{ }
printf("Positive\n");
}
return 0;
}
Example: isNegative.c
#include <stdio.h>
int isNegative (int); int
stdio.h contains function prototypes
isNegative for n )
( int
int main (void) printf(), scanf(), and other I/O
{
{ functions
int result;
int number; if ( n<0 )
printf ("Enter an integer: "); {
scanf ("%d",&number); result=1;
}
if (isNegative(number)) else
{ {
printf("Negative\n"); result = 0;
} }
else return result;
{ }
printf("Positive\n");
}
return 0;
}
Header files
• You can make your own header files with
prototypes of frequently used functions:
#include "myFunctions.h"
• Put the functions in a corresponding C file,
and include those too:
#include "myFunctions.c"
Example: isNegative.c
#include <stdio.h>
#include "myFunctions.h"
#include "myFunctions.c" Note:
• " " around file name for user-defined
int main (void)
files
{ • < > for standard system files
int number;
printf ("Enter an integer: ");
scanf ("%d",&number);
if (isNegative(number))
{
printf("Negative\n"); isNegative() is declared in
} myFunctions.h and defined in
else myFunctions.c, not in this file!
{
printf("Positive\n");
}
return 0;
}
Example: myFunctions.c Example: myFunctions.h
return 0;
}
Example: isNegative.c
#include <stdio.h>
int main (void)
int isNegative ( int n ) {
{ int number;
int result;
if (number<0) printf ("Enter an integer: ");
{ scanf ("%d",&number);
result=1;
} if (isNegative(number))
else {
{ printf("Negative\n");
result = 0; }
} else
return result; {
} printf("Positive\n");
}
ERROR! Number is local to the main
function, not accessible here return 0;
}
Example: isNegative.c
#include <stdio.h>
int main (void)
int {
isNegative ( int n ) int number;
{
int result; printf ("Enter an integer: ");
if ( n<0 ) scanf ("%d",&number);
{
result=1; if (isNegative(number))
} {
else printf("Negative\n");
{ }
result = 0; else
} {
return result;
Use the parameter n which printf("Positive\n");
is local
} }
to the function isNegative()
return 0;
}
Example: isNegative.c
#include <stdio.h>
int main (void)
int {
isNegative ( int n ) int number;
{
int result; printf ("Enter an integer: ");
if ( n<0 ) scanf ("%d",&number);
{
result=1; if (isNegative(number))
} {
else printf("Negative\n");
{ }
n is the=formal
result 0; else
} parameter {
return result; printf("Positive\n");
} } number is the actual
parameter
return 0;
}
Example: isNegative.c
#include <stdio.h>
int main (void)
int {
isNegative ( int n ) int number;
{
int result; printf ("Enter an integer: ");
if ( n<0 ) scanf ("%d",&number);
{
result=1; if (isNegative(number))
} {
else printf("Negative\n");
{ }
result = 0; else
} {
return result; printf("Positive\n");
} }