0% found this document useful (0 votes)
12 views23 pages

Chapter2 7

Uploaded by

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

Chapter2 7

Uploaded by

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

Chapter 7: Functions

11
Functions in C

7.1. Introduction

7.2. Program Modules in C

7.3. Functions

7.4. Function Definitions

7.5. Function Prototypes

7.6. Calling Functions: Call by Value and Call by Reference

7.7. Scope Rules

2
Introduction: Divide and conquer

• Construct a program from smaller pieces or


components
• Avoid rewriting same logic/code again and again in a
program.
• Each piece more manageable than the original
program
• Improve understandability of very large C programs.

3
Program Modules in C

• Programs written by combining user-defined functions


with library functions

FUNCTIONS

Built – in functions User – defined functions


(in libraries) (Written by programmers)

4
Where to write function definition?

• The function definition itself can act as an implicit


function declaration.
• All identifiers in C need to be declared before they are
used.That’s why function definitions were put
before main. If the order was reversed the compiler
would not recognize the function.
• To correct this a prototype could be added before main.

5
Function definition before the main

#include<stdio.h>
int square(int x){
int y;
y = x * x;
return y;
}
main(){
int i;
for (i=0; i<= 10; i++)
// function call
printf(“%d ”, square(i));
}

6
Function definition with prototype

#include<stdio.h>
float square(float x);
main()
{int i;
for (i=0; i<= 10; i++)
printf("\n%f",square(i));}
float square(float x)
{return x*x;}
7
Function definition

return-type function-name (parameter-list)


{
declarations and statements
}
• Function-name: any valid identifier
• Return-type: data type of the result
• void - function returns nothing
• Parameter-list: comma separated list, declares
parameters with data type (default int)

8
Function definitions

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

9
Function prototype

• The prototype gives basic structural information:


• what the function will return,
• what the function will be called
• what arguments the function can be passed

10
Function Prototypes
• Function prototype
• Function name
• Parameters - what the function takes in
• Return type - data type function returns
• Used to validate functions
• Prototype only needed if function definition comes
after use in program
int maximum( int x, int y, int z);
• Takes in 3 ints
• Returns an int

11
Function calls

• Invoking functions
• Provide function name and arguments (data)
• Function performs operations or manipulations
• Function returns results

12
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 affect original
• Use when function does not need to modify argument
• 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

13
Example: Call_by_reference parameter

#include <stdio.h>

void swap(int * pa, int * pb) {


int x = *pa;
*pa = *pb;
*pb = x;
}

int main() {
int a = 5, b = 100;
printf(“Before: a=%d, b=%d \n\n", a,b);
swap(&a, &b);
printf(“After : a=%d, b=%d \n\n", a, b);
return 0;
}

14
Scope rules

• Scope rules tell us if an entity (i.e., variable, parameter or


function) is accessible at certain places.
• Places where an entity can be accessed is referred to
the scope of that entity.

15
Scope Rules

• File scope
• Identifier defined outside functions, known in all functions
• Used for global variables, function definitions, function
prototypes
• Function scope
• Can only be referenced inside a function body

16
Scope Rules

• Block scope
• Identifier declared inside a block
• Block scope begins at definition, ends at right brace
• Used for variables, function parameters (local variables of
function)
• Outer blocks "hidden" from inner blocks if there is a
variable with the same name in the inner block

17
Block Scope
• Execution blocks, delimited with {}, define scopes.
{ int t=5;
{
float t=3.14;
printf("%f",t);
}
printf("%d",t);
}

• The variable's scope is limited to the {} block that contains


its declaration.
Example 1: Result of the following program

• Nested functions
#include <stdio.h> 3

int fun(int a){


fun(3)
a++; 4
return a; fun(4)
}
5
int main(){ fun(5)
printf("%d\n", fun(fun(fun(3))));
return 0; 6
}
19
Example 2: Result of the following program

#include <stdio.h> 3
int fun(int a){ fun(3)
return a++;
3
}
fun(4)
int main(){ 3
printf("%d\n", fun(fun(fun(3))));
fun(5)
return 0;
}
3
20
Example 3: Result of the following program

• Recursive function fun(5)


#include<stdio.h>
5 * fun(4)
int fun(int n){
if(n==0) return 1; 4 * fun(3)
else return n*fun(n-1);
}
3 * fun(2)

int main(){ 2 * fun(1)


printf("%d\n", fun(5));
return 0;
120 1 * fun(0)
}
1
21
Example 4: Compute average of f(a),f(b), f(c) if 𝟓

#include <stdio.h>
#include <math.h>
float f(float x) {
if(x == 0.0)
return 0;
else
return pow(x,5)+x/fabs(x) * pow(fabs(x), 0.2);
}
int main() {
float a, b, c;
printf(“Enter 3 numbers: "); scanf("%f%f%f", &a, &b, &c);
printf(“Result:%f \n", (f(a) + f(b) + f(c)) / 3);
return 0;
}

22
Example 6: Compute GCD of a list
1. #include <stdio.h>
2. int gcd(int a, int b) {
3. while (a != b){
4. if(a > b) a = a- b;
5. else b = b - a;
6. }
7. return a;
8. }
9. int main() {
10. int A[100], N, i, r;
11. printf(“Number of Elements: "); scanf("%d", &N);
12. for(i=0; i < N; i++) {
13. printf("A[%d] = ", i+1); scanf("%d", &A[i]);
14. }
15. r = A[0];
16. for(i = 1; i < N; i++)
17. r = gcd(r,A[i]);
18. printf(“GCD of the list:%d \n", r);
19. return 0;
20. }

23

You might also like