Unit2 7
Unit2 7
11
Functions in C
• Introduction
• Program Modules in C
• Functions
• Function Definitions
• Function Prototypes
• Scope Rules
2
Introduction: Divide and conquer
3
Program Modules in C
• Programs written by combining user-defined
functions with library functions
FUNCTIONS
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;
6
Function #include<stdio.h>
with main( )
{ int i;
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
13
Example: Call_by_reference parameter
#include <stdio.h>
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);
}
#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)
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