functions - bangladesh university of engineering and technology · 2019. 7. 13. · functions...

23
Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions 5.5 Function Definitions 5.6 Function Prototypes 5.7 Header Files 5.8 Calling Functions: Call by Value

Upload: others

Post on 19-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

Functions

Outline5.1 Introduction5.2 Program Modules in C5.3 Math Library Functions5.4 Functions5.5 Function Definitions5.6 Function Prototypes5.7 Header Files5.7 Header Files5.8 Calling Functions: Call by Value

Page 2: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

Reading list

• From Teach yourself book

• Section 1.7, 1.8 and 1.9

• Class lecture

Page 3: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

5.1 Introduction

• Divide and conquer

– Construct a program from smaller pieces or components

– Each piece more manageable than the original program

Page 4: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

5.2 Program Modules in C

• Functions– Modules in C

– Programs written by combining user-defined functions with library functions

• C standard library has a wide variety of functions

• Makes programmer's job easier - avoid reinventing the wheel

Page 5: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

5.2 Program Modules in C (II)

• Function calls– Invoking functions

• Provide function name and arguments (data)

• Function performs operations or manipulations

• Function returns results

– Boss asks worker to complete task– Boss asks worker to complete task

• Worker gets information, does task, returns result

• Information hiding: boss does not know details

Page 6: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

5.3 Math Library Functions

• Math library functions – perform common mathematical calculations

– #include <math.h>

• Format for calling functionsFunctionName (argument);

• If multiple arguments, use comma-separated list

– printf( "%.2f", sqrt( 900.0 ) );

• Calls function sqrt, which returns the square root of its argument

• All math functions return data type double

– Arguments may be constants, variables, or expressions

Page 7: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

5.4 Functions

• Functions– Modularize a program

– All variables declared inside functions are local variables

• Known only in function defined

– Parameters

• Communicate information between functions

• Local variables• Local variables

• Benefits– Divide and conquer

• Manageable program development

– Software reusability

• Use existing functions as building blocks for new programs

• Abstraction - hide internal details (library functions)

– Avoids code repetition

Page 8: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

5.5 Function Definitions

• Function definition format

return-value-type function-name( parameter-list ){

declarations and statements}

– Function-name: any valid identifier

– Return-value-type: data type of the result (default int)

• void - function returns nothing

– Parameter-list: comma separated list, declares parameters (default int)

Page 9: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

5.5 Function Definitions (II)

• Function definition format (continued)return-value-type function-name( parameter-list )

{

declarations and statements}

– Declarations and statements: function body (block)

• Variables can be declared inside blocks (can be nested)

• Function can not be defined inside another function

– Returning control

• If nothing returned

– return;

– or, until reaches right brace

• If something returned

– return expression;

Page 10: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

Outline

1. Function prototype (3 parameters)

2. Input values

2.1 Call function

3. Function definition

1 /* Fig. 5.4: fig05_04.c

2 Finding the maximum of three integers */

3 #include <stdio.h>

4

5 int maximum( int, int, int ); /* function prototype */

6

7 int main()

8 {

9 int a, b, c;

10

11 printf( "Enter three integers: " );

12 scanf( "%d%d%d", &a, &b, &c );

13 printf( "Maximum is: %d\n", maximum( a, b, c ) );

14

15 return 0;

16 }

17

Program Output

17

18 /* Function maximum definition */

19 int maximum( int x, int y, int z )

20 {

21 int max = x;

22

23 if ( y > max )

24 max = y;

25

26 if ( z > max )

27 max = z;

28

29 return max;

30 }

Enter three integers: 22 85 17Maximum is: 85

Page 11: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

5.6 Function Prototypes

• Function prototype– Function name

– Parameters - what the function takes in

– Return type - data type function returns (default int)

– Used to validate functions

– Prototype only needed if function definition comes after use in programprogram

int maximum( int, int, int );

• Takes in 3 ints

• Returns an int

• Promotion rules and conversions– Converting to lower types can lead to errors

Page 12: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

5.7 Header Files

• Header files– contain function prototypes for library functions

– <stdlib.h> , <math.h> , etc

– Load with #include <filename>

#include <math.h>

• Custom header files– Create file with functions

– Save as filename.h

– Load in other files with #include "filename.h"

– Reuse functions

Page 13: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

5.8 Calling Functions: Call by Value and Call by Reference

• Used when invoking functions

• Call by value– Copy of argument passed to function

– Changes in function do not effect original

– Use when function does not need to modify argument

• Avoids accidental changes• Avoids accidental changes

• Call by reference – Passes original argument

– Changes in function effect original

– Only used with trusted functions

• For now, we focus on call by value

Page 14: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

Write down a function that will take an integer as parameter and will return the integer as parameter and will return the reverse of the number.

Page 15: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

5.9 Reversing an integerint reverse (int numb) {

int r, sum = 0;

do{

r = numb%10;

numb = numb/10;

sum = sum*10+r;

}while (numb > 0);

return sum;

}

int main(){

int x, y;

x = 1243;

y = reverse(x);

printf("Reversing: %d Result %d", x , y);

}

Page 16: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

Write down a function that will take two integers x and y as parameter and will integers x and y as parameter and will return xy.

Page 17: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

5.10 Power Function

int power(int x, int y) {

int i, p = 1;

for(i = 1; i <= y; i++)

p = p*x;

return p;return p;

}

int main() {

int r;

r = power(2,5);

printf("Power of 2^5 is %d", r);

}

Page 18: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

Write down a function that will take an integer as parameter and will return integer as parameter and will return number of digits on it.

Page 19: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

5.11 Determining how many digits in a number

int howManyDigits(int numb){

int r,c = 0;

do {

r = numb%10;

numb = numb/10;

c++;

}while (numb > 0);}while (numb > 0);

return c;

}

int main( ){

int x,y;

x = 1345;

y = howmanydigits(x);

printf("Number of digits of the number %d is %d", x, y);

}

Page 20: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

Write down a function that will take two integers x and y as parameters and will integers x and y as parameters and will return GCD(x, y).

Page 21: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

int gcd(int x, int y) {

int i, gcd = 1,min;

min = (x > y)? y: x;

for(i = 2; i <= min; i++) {

if (x % i = = 0 && y % i = = 0)

gcd = i;

5.12 GCD Function

gcd = i;

}

return gcd;

}

int main() {

int r;

r = gcd(12,15);

printf(“GCD of 12 and 15 is %d", r);

}

Page 22: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

Write down a function that will take an integer as parameter and will return 1 if the integer is a prime number and return 0 otherwise. Using this function write down a program that will print all prime numbers between 2 to n where n will be numbers between 2 to n where n will be input to your program.

Page 23: Functions - Bangladesh University of Engineering and Technology · 2019. 7. 13. · Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions

int isPrime(int x) {

int i;

for(i = 2; i < x; i++) {

if (x % i = = 0)

return 0;

}

return 1;

}

5.13 Primality testing using function

}

int main() {

int n,i;

scanf(“%d”, &n);

for(i = 2; i < n; i++) {

if( isPrime(i) == 1)

printf(“%d ", i);

}

}