0% found this document useful (0 votes)
32 views52 pages

4_Function

C Programming

Uploaded by

Mir Masrur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views52 pages

4_Function

C Programming

Uploaded by

Mir Masrur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Functi on

C
Prof. Dr. Md. Al Mamun
Method, Procedure, Function or Sub-
routine
• Function is a group of statements that together perform a
task.

• Every C program has at least one function, which is main(),


and all the most trivial programs can define additional
functions.

• 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.

• A function declaration tells the compiler about a function's name,


return type, and parameters. A function definition provides the
actual body of the function.
Defining a function
The general form of a function definition in C
programming language is as follows:

return_type function_name( parameter list )


{
body of the function
}
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.

Parameters: A parameter is like a placeholder. When a function is invoked,


you pass a value to the parameter. This value is referred to as actual
parameter or argument. The parameter list refers to the type, order, and
number of the parameters of a function. Parameters are optional; that is,
a function may contain no parameters.

Function Body: The function body contains a collection of statements that


define what the function does.
Example
/* function returning the max between two numbers */
int max(int num1, int num2)
{ /* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Function Declaration
• A function declaration tells the compiler about
a function name and how to call the function.
The actual body of the function can be defined
separately.
return_type function_name( parameter list );

int max(int num1, int num2);


or
int max(int, int);
Calling a Function
• When a program calls a function, program control is
transferred to the called function. A called function
performs defined task, and when its return statement is
executed or when its function-ending closing brace is
reached, it returns program control back to the main
program.

• To call a function, you simply need to pass the required


parameters along with function name, and if function
returns a value, then you can store returned value.
Example
#include <stdio.h>
/* function declaration */
int max(int num1, int num2);
int main () {
/* local variable definition */
int a = 100;
int b = 200;
int ret;
/* calling a function to get max value */
ret = max(a, b);
printf( "Max value is : %d\n", ret );
return 0;
}
/* function returning the max between two numbers */
int max(int num1, int num2)
{ /* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
Example: hello1.c

Prints a simple greeting.

procedure sayHello
{
output “Hello World!”
}

Main Program
{

do procedure sayHello

}
Example: hello1.c #include <stdio.h>

/*
Prints a simple greeting. * Print a simple greeting.
*/

void sayHello ( void )


procedure sayHello {
{ printf(“Hello World!\n”);
output “Hello World!” }
}
/*
* Call a function which
* prints a simple greeting.
Main Program */
{ int main(void)
{
do procedure sayHello sayHello();
return 0;
}
}
Example: hello1.c
#include <stdio.h>

/*
* 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. */

void badSort ( int a, int b )


{
int temp;

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. */

void badSort ( int a, int b )


{
int temp;

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.
*/

void badSort ( int a, int b )


{
int temp; Actual
if ( a > b ) parameters
{
printf("%d %d\n", b, a);
} int main(void)
else {
{ int x = 3, y = 5;
printf("%d %d\n", a, b);
} badSort ( 10, 9 );
} badSort ( y, x+4 );
return 0;
}
Parameters (cont.)
• Parameters are passed by copying the value of
the actual parameters to the formal
parameters.
• Changes to formal parameters do not affect
the value of the actual parameters.
Example: badswap.c
int main(void)
/* Swap the values of two {
variables. */ int a = 3, b = 5;

void badSwap ( int a, int b ) printf("%d %d\n",a,b);


{ badSwap ( a, b );
int temp; printf("%d %d\n",a,b);

temp = a; return 0;
a = b; }
b = temp;

printf("%d %d\n", a, b);


}
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
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);
}

Called function’s Calling function’s


environment: environment:
a: 5 a: 3
b: 3 b: 5
Parameters (cont.)

• If a function does not take parameters,


declare its formal argument list void.

Declaration: void sayHello ( void )


{
printf(“Hello World!\n”);
}

Function call: sayHello();


Return Values
• Values are returned by copying a value
specified after the return keyword
Example: max.c

Return type /* Returns the larger of two


numbers. */
int max (int a, int b)
{
int result;

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

This style okay. /* Returns the larger of two


numbers. */
int max (int a, int b)
{
int result;

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.

Declaration: void sayHello ( void )


{
printf(“Hello World!\n”);
}

Function call: sayHello();


Prototyping of Functions

• Must declare functions before use (like


variables)
• Declaration is called a “prototype”
• Specifies the name, parameters and return
type of the function, but not the code
Example: isNegative.c
#include <stdio.h>
int isNegative (int); int isNegative ( int n
{
int main (void) int result;
{ if ( n<0 )
int number; {
printf ("Enter an integer: "); result=1;
scanf ("%d",&number); }
else
if (isNegative(number)) {
{ result = 0;
printf("Negative\n"); }
} return result;
else }
{
printf("Positive\n");
}
return 0;
}
Example: isNegative.c
Function Prototype
#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");
}
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 Definition

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

int isNegative ( int n )


{ int isNegative ( int );
int result;
if ( n<0 )
{
result=1;
}
else
{
result = 0;
}
return result;
}
Scope
Where can you use a variable which is declared
in a function?
• In that function only
• Not in a calling function
• Not in a called function
Scope: Local Variables
• Formal parameters: only accessible whilst
function executing
• Variables declared in a function body: only
accessible whilst function executing
• In fact, this is true of every block in a program
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");
}

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");
} }

result & n: local return


to isNegative()
0;
number: local to} main()
Example: isNegativeGlobal.c
#include <stdio.h>
int main (void)
int number; {

int printf ("Enter an integer: ");


isNegative ( void ) scanf ("%d",&number);
{
int result; if (isNegative())
if ( number <0 ) {
{ printf("Negative\n");
result=1; }
} else
else {
{ printf("Positive\n");
result = 0; }
}
return result; return 0;
} }
Example: isNegativeGlobal.c
#include <stdio.h>
int main (void)
int number; {

int printf ("Enter an integer: ");


isNegative ( void ) number is now
scanf GLOBAL -
("%d",&number);
{
int result;
declaredifoutside any function,
(isNegative())
if ( number <0 ) accessible
{ in all functions
{ (after the declaration)
printf("Negative\n");
result=1; }
} else
else {
{ printf("Positive\n");
result = 0; }
}
return result; return 0;
} }
Scope: Global Variables
• Global variables are accessible in any function
after their declaration to the end of that
source file
• They're useful, but risky
– if any and every function can modify them, it can
be difficult to keep track of their value
• Better to use local variables and parameter
passing if possible
Scope: Functions
• Functions are also accessible in any function
after their declaration to the end of that
source file
Summary
• Include function prototype before its use
• Be careful about the scope of variables

You might also like