Вы находитесь на странице: 1из 21

8/8/2020

Functions – a big Topic

• Motivation—why needed
• Examples
Introduction to Functions • How two functions communicate
• void functions
Structured Programming • Function parameters
• Function returning values
(Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and
from C Teach Yourself, 3rd editions, by Herbert Shiield)
• Local variable concept and scope rule
• Function prototypes & Header files
Introduction to Functions 1 Introduction to Functions 2
Ashikur Rahman

1
8/8/2020

Functions – the bigger picture


Definition – Function

• A fragment of code that accepts zero or


more argument values, produces a result
value, and has zero or more side effects.

• A method of encapsulating a subset of a


program or a system
• To hide details
• To be invoked from multiple places
• To share with others
Introduction to Functions 3 Introduction to Functions 4

2
8/8/2020

Motivation-why needed Example


LIKE(){
• Functions
printf("Like ");
– Modularize a program }

• Benefits main(){
printf("I ");
– Divide and conquer printf("Like ");
• Manageable program development printf("C");
}
– Software reusability
• Use existing functions as building blocks for new
programs OUTPUT: I C
Like C
– Avoids code repetition

Ashikur Rahman Introduction to Functions 5 Ashikur Rahman Introduction to Functions 6

3
8/8/2020

Example Example
LIKE(){ LIKE(){

printf("Like "); printf("Like ");


} }

main(){ main(){
printf("I "); printf("I ");
LIKE(); LIKE();
printf("C"); printf("C");
} }

OUTPUT: I C
Like C OUTPUT: I Like C

Ashikur Rahman Introduction to Functions 7 Ashikur Rahman Introduction to Functions 8

4
8/8/2020

Example--Reusability How two functions communicates


LIKE(){

printf("Like ");
}
Through
main(){ Caller Callee
int i; Function Parameters Function
printf("I "); (for example (for example
for(i = 1; i <= 5; i++)
main ) LIKE )
LIKE();
By returning
printf("C");
}
Values
OUTPUT: I Like C
Like Like Like Like C

Ashikur Rahman Introduction to Functions 9 Ashikur Rahman Introduction to Functions 10

5
8/8/2020

Example: Parameter passing Another Example: Parameter passing


LIKE(){ LIKE(int n){ oddOREven(int n){
int i; if(n%2 == 0)
printf("Like "); for(i = 1; i <= n; i++) printf("Even\n");
} printf("Like "); else
} printf(“Odd\n");
main(){ }
printf("I ");
LIKE();
LIKE(3);
main(){
printf("C");
}
oddOREven(2);
oddOREven(3);
}
OUTPUT: I Like C
Like Like C
OUTPUT: Even
OUTPUT: Odd

Ashikur Rahman Introduction to Functions 11 Ashikur Rahman Introduction to Functions 12

6
8/8/2020

Returning Values from a Function Example: Returning values


int max(int a, int b){
int r;
Steps for returning values from a function if (a > b)
r = a;
else
r = b;
1. Mention return data type in front of function return r;
}
2. Use return statement to return value main(){
int p;
3. Catch the value from the caller using p = max(3,4);
printf("%d\n",p);
assignment (=) operator }

OUTPUT: 4

Ashikur Rahman Introduction to Functions 13 Ashikur Rahman Introduction to Functions 14

7
8/8/2020

Three types of functions at a glance Three types of functions at a glance


No parameters Has parameter Has parameter No parameters Has parameter Has parameter
No return No return Returns result No return No return Returns result

int max(int a, int b){ int max(int a, int b){


LIKE(){ oddOREven(int n){ LIKE(){ oddOREven(int n){
int r; int r;
if(n%2 == 0) if(n%2 == 0)
printf("Like "); if (a > b) printf("Like "); if (a > b)
printf("Even\n"); printf("Even\n");
} r = a; } r = a;
else else
else else
printf(“Odd\n"); printf(“Odd\n");
r = b; r = b;
} }
return r; return r;
} }
main(){ main(){ main(){ main(){ main(){ main(){
Like(); oddOREven(2); int p = max(4,6); Like(); oddOREven(2); int p = max(4,6);
} } } } } }

Ashikur Rahman Introduction to Functions 15 Ashikur Rahman Introduction to Functions 16

8
8/8/2020

Function Definition Three types of functions at a glance


No parameters
• Every function definition has the form No return
return-type function-name (parameter declarations) {
int max(int a, int b){
definitions and statements LIKE(){ oddOREven(int n){
int r;
if(n%2 == 0)
} printf("Like ");
printf("Even\n");
if (a > b)
} r = a;
else
• If there is no parameter mention void printf(“Odd\n");
else
r = b;
}
• If there is no return mention void return r;
}
main(){ main(){ main(){
Like(); oddOREven(2); int p = max(4,6);
} } }

Ashikur Rahman Introduction to Functions 17 Ashikur Rahman Introduction to Functions 18

9
8/8/2020

Three types of functions at a glance Three types of functions at a glance


Has parameter Has parameter
No return No return

int max(int a, int b){ int max(int a, int b){


void LIKE(void){ oddOREven(int n){ void LIKE(void){ void oddOREven(int n){
int r; int r;
if(n%2 == 0) if(n%2 == 0)
printf("Like "); if (a > b) printf("Like "); if (a > b)
printf("Even\n"); printf("Even\n");
} r = a; } r = a;
else else
else else
printf(“Odd\n"); printf(“Odd\n");
r = b; r = b;
} }
return r; return r;
} }
main(){ main(){ main(){ main(){ main(){ main(){
Like(); oddOREven(2); int p = max(4,6); Like(); oddOREven(2); int p = max(4,6);
} } } } } }

Ashikur Rahman Introduction to Functions 19 Ashikur Rahman Introduction to Functions 20

10
8/8/2020

Local variable concept User input


int min(int a, int b){ int min(int a, int b){
int r; SELFISH principle: int r; SELFISH principle:
if (a < b) Variables declared if (a < b) Variables declared
r = a; r = a;
else
within a function can else
within a function can
r = b; only be used by that r = b; only be used by that
return r; return r;
}
function }
function

main(){ main(){
main(){ main(){
int p;
min(3,4); int x,y,p; int p;
p = min(3,4);
printf("%d\n",r); scanf("%d%d",&x,&y); scanf("%d%d",&a,&b);
printf("%d\n",p);
} p = min(x,y); p = min(a,b);
}
printf("%d\n",p); printf("%d\n",p);
} }

Ashikur Rahman Introduction to Functions 21 Ashikur Rahman Introduction to Functions 22

11
8/8/2020

Global Variable Concept Function prototype—Why needed?


int a, b; In all these three examples, functions have been written prior to
the function call. But it can not be maintained always!!!
int min(void){
int r;
if (a < b) int max(int a, int b){
void LIKE(void){ void oddOREven(int n){
r = a; int r;
if(n%2 == 0)
else printf("Like "); if (a > b)
printf("Even\n");
r = b; } r = a;
else
return r; else
printf(“Odd\n");
} r = b;
}
return r;
main(){
}
int p; main(){ main(){ main(){
scanf("%d%d",&a,&b); Like(); oddOREven(2); int p = max(4,6);
p = min(); } } }
printf("%d\n",p);
}

Ashikur Rahman Introduction to Functions 23 Ashikur Rahman Introduction to Functions 24

12
8/8/2020

Function prototype— A(){


…..
Prototype
why needed? }
…..

C(){ • Prototype means a replica of a real system


…..
….
A ….
}
B(){
…..
C B
….
….
}

A(){
…..
….
….
}
Ashikur Rahman Introduction to Functions 25 Ashikur Rahman Introduction to Functions 26

13
8/8/2020

Prototypes of our previously defined


Function Prototype
three functions at a glance
• Definition:– a Function Prototype in C is a
void LIKE(void); int max(int a, int b);
language construct of the form:– void oddOREven(int n);

void LIKE(void){ main(){ main(){


oddOREven(); int p = max(4,6);
return-type function-name (parameter declarations) ; printf("Like "); } }
}
void oddOREven(int n){ int max(int a, int b){
if(n%2 == 0) int r;
printf("Even\n"); if (a > b)
• i.e., exactly like a function definition, except with a main(){
else r = a;

';' instead of a body in curly brackets Like();


printf(“Odd\n"); else
} r = b;
}
return r;
}

Ashikur Rahman Introduction to Functions 27 Ashikur Rahman Introduction to Functions 28

14
8/8/2020

Library Functions Library functions’ prototype & Header files

#include <math.h> #include <stdio.h> • Function prototypes of library functions are


– sin(x) // radians – printf()
– cos(x) // radians – fprintf()
typically provided in header files
– tan(x) // radians – scanf() • i.e., the ‘.h’ files that programmers include in their
– atan(x) – sscanf() code
– atan2(y,x) – ...
• Grouped by related functions and features
– exp(x) // ex #include <string.h>
– log(x) // loge x • To make it easier for developers to understand
– strcpy()
– log10(x) // log10 x – strcat() • To make it easier for team development
– sqrt(x) // x  0 – strcmp() • To make a package that can be used by someone
– pow(x, y) // xy – strlen() else
– ... – ...
Introduction to Functions 29 Ashikur Rahman Introduction to Functions 30

15
8/8/2020

A typical diagram of function Example

Parameter 1 Write down a function that will take a


Parameter 2 Function year as a parameter and will return
………. Result
………. number of days in that year. In your
Parameter n main function take user input for year
and use this function, to print two
return-type function-name (parameter declarations) { things: (1) number of days in the year,
definitions and statements and (2) whether the year is a leap year
}
or not.
Ashikur Rahman Introduction to Functions 31 Ashikur Rahman Introduction to Functions 32

16
8/8/2020

Solution
Solution diagram int numberofDays(int year){
int days;
if (year%400 == 0)
days = 366;
else if (year%100 == 0)
days = 365;
year numberofDays else if (year%4 == 0)
days
days = 366;
else days = 365;
return days;
}
void main(void){
int y,p;
return-type function-name (parameter declarations) { scanf("%d",&y);
p = numberofDays(y);
definitions and statements
printf("Number of days %d\n",p);
} if(p == 366) printf("LEAP YEAR");
else printf("Not a Leap Year ");
Introduction to Functions 33 } Introduction to Functions 34
Ashikur Rahman Ashikur Rahman

17
8/8/2020

Example: Function returning indirect


Solution diagram
results
Write down a function that will take a
number as a parameter and will return number isPrime
0/1
1 if the number is a prime number and
0 otherwise. In your main function take
a number as user input and use this
function, to print whether the number return-type function-name (parameter declarations) {
is a prime number or not. definitions and statements
}

Ashikur Rahman Introduction to Functions 35 Ashikur Rahman Introduction to Functions 36

18
8/8/2020

Solution Example: Function accepting array as


int isPrime(int n){
int i, c = 0;
parameter
for(i = 1; i <= n; i++){
if (n%i == 0)
c++;
Write down a function that will take a
}
if (c == 2) return 1;
set of numbers as a parameter and will
}
else return 0; find and return maximum value within
the set. Show how we can use this
void main(void){ function from main to find and print
int n,p;
scanf("%d",&n); maximum value.
p = isPrime(n);
if(p == 1) printf("YES");
else printf("NO");
}

Ashikur Rahman Introduction to Functions 37 Ashikur Rahman Introduction to Functions 38

19
8/8/2020

Solution
Solution diagram int findMax(int x[], int n){
int i, max;
max = x[0];
for(i = 1; i < n; i++){
if (max < x[i])
max = x[i];
Number set findMax }
Max value
return max;
}

void main(void){
int a[] = {34, 21, 65, 78, 90};
int b[] = {4, 2, 8};
return-type function-name (parameter declarations) { int r;
findMax(a);
r = findMax(a,5);
definitions and statements printf("%d\n", r);
} r = findMax(b,3);
findMax(b);
printf("%d", r);
Introduction to Functions 39 } Introduction to Functions 40
Ashikur Rahman Ashikur Rahman

20
8/8/2020

Questions?

Introduction to Functions 41

21

Вам также может понравиться