0% found this document useful (0 votes)
6 views

Unit2 7

Uploaded by

phama2401
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Unit2 7

Uploaded by

phama2401
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Unit 7: Functions

11
Functions in C
• Introduction

• Program Modules in C

• Functions

• Function Definitions

• Function Prototypes

• Calling Functions: Call by Value and Call by Reference

• 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
#include<stdio.h>
int square(int x){
int y;
Function y = x * x;

definition }
return y;

before the main(){


int i;
main for (i=0; i<= 10; i++)
// function call
printf(“%d ”,
square(i));
}

6
Function #include<stdio.h>

definition // function prototype, also called function declaration


float square ( float x );

with main( )
{ int i;

prototype for (i=0; i<= 10; i++)


// function call
printf(“%d ”, square(i));
getch();
}
float square ( float x ) // function definition
{
float y ;
y = x * x ;
return ( y ) ;
}

7
Function definition
return-value-type function-name (parameter-list )
{
declarations and statements
}
• Function-name: any valid identifier
• Return-value-type: data type of the result
• void - function returns nothing
• Parameter-list: comma separated list, declares
parameters (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

#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
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) nếu
#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
Example 1: Result of the following program
#include<stdio.h> a 0210
void fun(int n) {
if(n > 0) { b 0102
fun(--n); c 1120
printf("%d ", n); d 0120
fun(--n);
e 0201
}
}
int main() {
fun(3);
return 0;
}

24
Example 1: Result of the following program
#include<stdio.h> a 5, 2
void fun(int *i, int *j) {
b 2, 5
*i = *i * *i;
*j = *j * *j; c 10, 4
} d 4, 25
int main() { e 25, 4
int i=5, j=2;
fun(&i, &j);
printf("%d, %d", i, j);
return 0;
}

25
Example 1: Result of the following program

#include<stdio.h> a AB
void fun(char*);
int main() { b AC
char a[10]="ABCDEF"; c BC
fun(&a[0]); d BD
return 0;
e CD
}
void fun(char *a){
printf("%c", *++a);
a++;
printf("%c", *a);
}

26

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy