Professional Documents
Culture Documents
CHAPTER 6
Functions
Outline
• Modular Programming
• User defined functions
• Function elements:
– Function definition
– Function prototype
– Function call
• Types of functions
1
Objectives
• After this lesson, you should be able to:
Explain function concept
Write C program using function concept.
2
Functions
3
#include<stdio.h>
void main(){
int num1,num2;
int addition, substraction, multiplication, division;
addition=num1+num2;
substraction=num2-num1;
multiplication=num1*num2;
division=num1/num2;
4
Separate the functions
#include<stdio.h> //get two numbers
printf("\n Enter two numbers");
scanf("%d%d",&num1,&num2);
void main()
{ //Addition and its output
int num1,num2; addition=num1+num2;
int addition, substraction, multiplication, division; printf("\n Result of addittion\t\t "
"%d + %d = %d",num1,num2,addition);
printf("\n Enter two numbers");
scanf("%d%d",&num1,&num2); //Substraction and its output
substraction=num2-num1;
addition=num1+num2; printf("\n Result of substraction\t\t"
substraction=num2-num1; "%d - %d = %d",num2,num1,substraction);
multiplication=num1*num2;
division=num1/num2;
//multiplication and its output
printf("\n Result of addittion\t\t " multiplication=num1*num2;
printf("\n Result of multiplication\t\t"
"%d + %d = %d",num1,num2,addition); "%d * %d = %d",num1,num2,multiplication);
printf("\n Result of substraction\t\t"
"%d - %d = %d",num2,num1,substraction);
printf("\n Result of multiplication\t\t" //division and its output
"%d * %d = %d",num1,num2,multiplication); division=num1/num2;
printf("\n Result of division\t\t"
printf("\n Result of division\t\t" "%d / %d = %d",num1,num2,division);
"%d / %d = %d",num1,num2,division);
}
10
5
Modular Programming
• Modular programming in C is called Functions
• This technique is called divide and conquer
• Problem is divided into smaller pieces or
modules
• Module can be divided into sub-modules
• Benefits:
– program structure is readable
– make program development more manageable
– software reusability – using existing functions as
building block to create new program
11
Function prototype
Functions Function definition
elements
Function call
12
6
Separate the operations into functions
#include<stdio.h> void substraction(int c,int d)
{
void addition(int r,int s); int substract;
void substraction(int c,int d); Function substract=d-c;
void multiplication(int e,int f); prototype printf("\nResult of substraction\t"
void division(int m,int n); "%d - %d = %d",d,c,substract);
}
void main(){
int num1,num2; void multiplication(int e,int f)
printf("\n Enter two numbers"); {
scanf("%d%d",&num1,&num2); int multiply;
addition(num1,num2); multiply=e*f;
substraction(num1,num2); Function printf("\nResult of multiplication\t"
multiplication(num1,num2); call "%d * %d = %d",e,f,multiply);
division(num1,num2); }
}
void division(int m,int n){
void addition(int r,int s) int divide;
{ divide=m/n;
int add; Function printf("\nResult of division \t"
add=r+s; definition "%d / %d = %d",m,n,divide);
printf("\nResult of addition\t" }
"%d + %d = %d",r,s,add);
}
13
Function Prototype
• The function prototype
– Must be added to a C program before the main()
function, if call that function before defining it.
– It tells the compiler:
1.what type of value the function returns (function return type)
2.number and types of parameters
3.order in which these parameters are expected.
– There is no need for a prototype if the function is
called after its definition.
14
7
Function Prototype (2)
Function prototype example
void addition (int a, int b);
or
void addition (int, int);
where;
#include<stdio.h>
void multiplication(int e,int f)
void
void
addition(int ,int );
substraction(int c,int d);
Function {
int multiply;
void multiplication(int,int); Prototype multiply=e*f;
void division(int m,int n); printf("\nResult of multiplication\t"
"%d * %d = %d",e,f,multiply);
void main(){ }
int num1,num2;
printf("\n Enter two numbers"); void division(int m,int n){
scanf("%d%d",&num1,&num2); int divide;
addition(num1,num2); divide=m/n;
substraction(num1,num2); printf("\nResult of division \t"
multiplication(num1,num2); "%d / %d = %d",m,n,divide);
division(num1,num2); }
}
16
8
Function Definition
• A function definition has two principal
components:
– the first line (including the parameter/ argument
declarations), and
– the body of the function.
• The first line of a function definition contains
– type of the value returned by the function (function
return type)
– the function name, and
– (optionally) a set of parameters/ arguments,
separated by commas and enclosed in parentheses.
17
First line
Body
18
9
#include<stdio.h>
void main(){
int num1,num2;
Parameters / arguments
printf("\n Enter two numbers");
Function data type
scanf("%d%d",&num1,&num2);
return type
addition(num1,num2);
r and s are parameters
substraction(num1,num2);
Function name / arguments
}
20
10
Function Definition (4)
• Function Definition: argument/parameter
– The arguments are called formal argument
• because they represent the names of data items that are
transferred into the function from the calling portion of the
program.
– There are also known as parameters or formal
parameters.
– The identifiers used as formal arguments are “local”
• because they are not recognized outside of the function.
• the names need not to be same as the names of the actual
arguments in the calling portion of the program.
21
22
11
Function Definition (6)
• Function Definition: compound
statement/body
– compound statement / function body
defines the action to be taken by the function.
– can contain expression statements, other
compound statements, control statements,
and so on.
– It should include one or more return
statements, in order to return a value to the
calling portion of the program.
23
Example
Function prototype & definition
#include <stdio.h>
void main()
{
printf("The mod is: %d ", mod(4,5));
}
{
return a % b; OUTPUT?
}
24
12
Example
• Function with definition but without prototype
#include <stdio.h>
void main()
{
printf("The sum is: %d ", sum(4,5));
}
OUTPUT?
25
Function Calls
• A function can be accessed (i.e., called) by
– specifying function name, followed by a list of arguments
enclosed in parentheses and separated by commas.
• If the function call does not require any arguments
– empty pair of parentheses must follow the name of the function.
• The arguments appearing in the function call are referred
to as actual arguments, in contrast to the formal
arguments that appear in the first line of the function
definition.
26
13
#include<stdio.h>
void main(){
int num1,num2;
printf("\n Enter two numbers");
scanf("%d%d",&num1,&num2); num1 and num2 are
addition(num1,num2);
Function calls actual parameters /
substraction(num1,num2); arguments in function
} call
void addition(int r,int s)
{
int add; Function definition
add=r+s;
printf("\nResult of addition\t”
“%d + %d = %d",r,s,add);
}
void substraction(int c,int d) r, s, c and d are
{
formal parameters /
arguments in
int substract; function definition
substract=d-c;
printf("\nResult of substraction\t”
“%d - %d = %d",d,c,substract);
} 27
28
14
Function Calls (3)
• A function can be called by
– the main function
– other functions
– itself (recursion)
• In C, functions call can be
1.by value
2.by reference
29
30
15
Function Calls (5)
• Example Call By Value
finval = FuncByValue(finval);
31
FuncByReference(&finref)
the function
access
void FuncByReference(float *fvalptr)
appears by {
itself since it *fvalptr = *fvalptr * *fvalptr;
does not }
return a
32
value
16
Scope of variable
•Scope determine where the variable will be used.
•Two types of scope: GLOBAL and LOCAL
Example
Declares inside #include <stdio.h>
main() function or any
void getvalue();
other functions void main()
{ int x;
x is a local variable to }
main() function and z
is a local variable to void getvalue()
getvalue() function { int z;
………… 34
}
17
Scope of variable (3)
Example 1:
#include <stdio.h>
void main()
{ printf(“\nEnter value of a and b : ”);
scanf(“%d %d”,&a,&b);
display();
}
void display()
{
printf(“Value a is % and b is %d”, a,b);
}
35
void main()
{ int c; //local variable
a= 5;
b = 9;
c = 12;
display();
printf(“\nValue c is %d”, c);
}
void display()
{
printf(“\nValue a is %d”,a);
printf(“\nValue b is %d”,b);
printf(“\nValue c is %d”,c);
} 36
18
Scope of variable (5)
Example 3:
#include <stdio.h> Output?
void display();
void main()
{ int c; // local variable
a= 5;
b = 9;
c = 12;
display();
printf(“\nValue c is %d”, c);
}
void display()
{ int a, b; //local variables
printf(“\nValue a is %d”,a);
printf(“\nValue b is %d”,b);
}
37
19
Types of function
1. Does not receive and not return value
2. Receive but does not return any value
3. Receive and return value
Format
39
void function2(void);
40
20
Types of function (3)
Example:
Example:
void function1(int);
void function2(int,int);
void function3(int,float,int);
21
Types of function (5)
Example 1:
void function1(int a)
{ printf(“Value a is %d”,a); }
43
22
Example 3
#include<stdio.h> void substraction(int c,int d)
{
void addition(int r,int s); int substract;
substract=d-c;
void substraction(int c,int d);
printf("\nResult of substraction\t"
void multiplication(int e,int f); "%d - %d = %d",d,c,substract);
void division(int m,int n); }
45
Example:
int function1(int);
float function2(int,int);
int function3(int,float,int);
23
Types of function (8)
Prototype: #include <stdio.h>
float calculate_discount(int); float calculate_discount(int);
void main()
Call: { int quantity;
calculate_discount(kuantiti); printf(“Enter quantity :”);
scanf(“%d”,&quantity);
Definition: printf(“Discount %d”,calculate_discount(quantity));
}
float calculate_discount(int b)
float calculate_discount(int quantity)
{ if (b > 1000)
{
discount = 0.5; float discount;
else if (quantity > 1000)
discount = 0.1; discount = 0.5;
return discount; else
} discount = 0.1;
return discount;
}
return statement 47
Exercise
• Modify Example 3 (Slide 45) from a
program that consist of functions that
receive but does not return any value to a
program that consist of functions that
receive and return value.
48
24
Exercises
A. Give the function prototype for each of the following:
• Function smallest that takes three integers, x, y, z
and returns an integer.
• Function instructions that does not receive any
arguments and does not return a value.
• Function inToFloat that takes an integer argument,
number, and returns a floating-point result.
49
void change1(int a)
{
a=2*a;
printf(“Value of a in change1 function: %d\n",a);
}
int change2(int b)
{
int a;
a=4;
b=a + b;
return (b); 50
}
25
/* Program for Lesson 5_4 */ Function prototype. The * indicates that to work with
the value of the corresponding argument in the
#include <stdio.h> function call, we must use * in the function body
void function1 (int a, int b, double r ,double s, int *c, double *t);
void main (void)
{
int i=5, j=6, k;
double x=10.6, y=22.3, z;
Symbol ‘&’
- An operator that means “address of”
Symbol ‘*’
- 1. the binary multiplication operator
d=e*f;
26
/* Program for Lesson 5_5 */
#include <stdio.h>
void function1 (int a, int b, double r, double s, int *c, double *t);
function1 (i,j,x,y,&k,&z);
int i=5,j=6,k;
double x=10.6,y=22.3,z;
j int FFF2
k int FFF0
x double FFE8
y double FFE0
z double FFD8
Notes: C is responsible for creating the variable address. You need not 54
specify the addresses; they are taken automatically.
27
function1 (i,j,x,y,&k,&z);
void function1 (int a,int b,double r,double s, int *c, double *t)
*c = a+b;
/*5+6 is stored in the location indicated by the left inside
(go to address contained in c’s memory cell*/
*t = r+s+(*c);
/*10.6+22.3 +11 is stored in the location indicated by the
left inside (go to address contained in t’s memory cell*/
28
Revise the
application
programs in
text book!!!
57
29