5 Cse MFF Function
5 Cse MFF Function
C
Prof. Dr. Md. Al Mamun
Method, Procedure, Function or
Sub-routine
• Function is a group of statements that together perform a
task.
3
Defining a function
Return Type: A function may return a value. The return_type is the
data type of the value the function returns. Some functions perform
the desired operations without returning a value. In this case, the
return_type is the keyword void.
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
}
9
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;
}
11
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;
}
12
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;
}
13
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.
14
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);
}
}
15
Example: badsort.c
/* Print two numbers in order. */
if ( a > b )
{
printf("%d %d\n", b, a);
}
else
{
printf("%d %d\n", a, b);
}
}
16
Example: badsort.c Formal
parameters
/* Print two numbers in order.
*/
18
Example: badswap.c
int main(void)
/* Swap the values of two {
variables. */ int a = 3, b = 5;
temp = a; return 0;
a = b; }
b = temp;
19
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
20
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
21
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
22
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);
}
24
Return Values
• Values are returned by copying a value
specified after the return keyword
25
Example: max.c
if (a > b)
{
result = a;
}
else
{
result = b;
}
return result;
}
26
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;
}
27
Example: max.c
if (a > b)
{
result = a;
}
else
{
result = b;
}
return result;
}
28
Return Values (cont.)
• If a function does not return a value, declare
its return type void.
29
Prototyping of Functions
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 printf("Positive\n");
n which 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");
{ }
is the formal
result = n0; 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");
} }