C Functions
C Functions
sum = 0;
for (int i = 20; i <= 30; i++)
sum = sum + i;
printf("Sum from 20 to 30 is %d”, sum);
sum = 0;
for (int i = 35; i <= 45; i++)
sum = sum + i;
printf("Sum from 35 to 45 is %d “, sum);
4
Solution
int sum = 0;
for (int i = 1; i <= 10; i++)
sum = sum + i;
printf("Sum from 1 to 10 is %d" , sum);
sum = 0;
for (int i = 20; i <= 30; i++)
sum = sum + i;
printf("Sum from 20 to 30 is %d”, sum);
sum = 0;
for (int i = 35; i <= 45; i++)
sum = sum + i;
printf("Sum from 35 to 45 is %d “, sum);
5
Solution
int sum(int i1, int i2) {
int sum = 0;
for (int i = i1; i <= i2; i++)
sum += i;
return sum;
}
int main() {
pritntf("Sum from 1 to 10 is %d “ , sum(1, 10));
7
Benefit of function
int sum(int i1, int i2) {
int sum = 0;
for (int i = i1; i <= i2; i++)
sum += i;
return sum;
} Return
Value
Call
int main() {
pritntf("Sum from 1 to 10 is %d “ , sum(1, 10));
return 0;
}
8
Benefit of function
int sum(int i1, int i2) {
int sum = 0;
for (int i = i1; i <= i2; i++)
sum += i;
return sum;
Black Box
} Return
Value
Call
int main() {
pritntf("Sum from 1 to 10 is %d “ , sum(1, 10));
return 0;
} You can think of the method body as a black box that
contains the detailed implementation for 9the method.
Benefits of Methods
10
Functions - Mathematical View
Returned
X Function value
Function Input and Output
Sample Function Call
#include <stdio.h>
result = x + y;
return result;
}
Calculating the Area of a Circle
with a Function
Example: function with no arguments
int main(){
printf(“Programming is Cool!!”);
message();
return 0;
}
void message(){
printf(“IICT, Ahmedabad University”);
}
Example: function with no arguments
int main(){
printf(“Programming is Cool!!”);
message();
return 0;
}
void message(){
printf(“IICT, Ahmedabad University”);
}
Example: function with no arguments
int main(){
printf(“Programming is Cool!!”);
message();
return 0; A function,
} which returns no value
& no arguments
void message(){
printf(“IICT, Ahmedabad University”);
}
Example: function with no arguments
int main(){
printf(“Programming is Cool!!”);
message();
return 0;
}
void message(){
printf(“IICT, Ahmedabad University”);
}
Example: function with no arguments
int main(){
printf(“Programming is Cool!!”);
message();
return 0;
}
void message(){
printf(“IICT, Ahmedabad University”);
}
Example: program with multiple functions
int main() {
printf(“I am in main function”);
india();
france();
italy();
return 0;
}
void india() {
printf(“I am in India function”);
}
void france(){
pritnf(“I am in France function”);
}
void italy() {
pritnf(“I am in italy function”);
}
Example: program with multiple functions
int main() {
printf(“I am in main function”);
india();
france();
italy();
return 0;
}
void india() {
printf(“I am in India function”);
}
void france(){
pritnf(“I am in France function”);
}
void italy() {
pritnf(“I am in italy function”);
}
Example: program with multiple functions
int main() {
printf(“I am in main function”);
india();
france();
italy();
return 0;
}
void india() {
printf(“I am in India function”);
}
void france(){
pritnf(“I am in France function”);
}
void italy() {
pritnf(“I am in italy function”);
}
Example: program with multiple functions
int main() {
printf(“I am in main function”);
india();
france();
italy();
return 0;
}
void india() {
printf(“I am in India function”);
}
void france(){
pritnf(“I am in France function”);
}
void italy() {
pritnf(“I am in italy function”);
}
Example: program with multiple functions
int main() {
printf(“I am in main function”);
india();
france();
italy();
return 0;
}
void india() {
printf(“I am in India function”);
}
void france(){
pritnf(“I am in France function”);
}
void italy() {
pritnf(“I am in italy function”);
}
Example: program with multiple functions
int main() {
printf(“I am in main function”);
india();
france();
italy();
return 0;
}
void india() {
printf(“I am in India function”);
}
void france(){
pritnf(“I am in France function”);
}
void italy() {
pritnf(“I am in italy function”);
}
Example: program with multiple functions
int main() {
printf(“I am in main function”);
india();
france();
italy();
return 0;
}
void india() {
printf(“I am in India function”);
}
void france(){
pritnf(“I am in France function”);
}
void italy() {
pritnf(“I am in italy function”);
}
Example: program with multiple functions
int main() {
printf(“I am in main function”);
india();
france();
italy();
return 0;
}
void india() {
printf(“I am in India function”);
}
void france(){
pritnf(“I am in France function”);
}
void italy() {
pritnf(“I am in italy function”);
}
Example: program with multiple functions
int main() {
printf(“I am in main function”);
india();
france();
italy();
return 0;
}
void india() {
printf(“I am in India function”);
}
void france(){
pritnf(“I am in France function”);
}
void italy() {
pritnf(“I am in italy function”);
}
Example: program with multiple functions
int main() {
printf(“I am in main function”);
india();
france();
italy();
return 0;
}
void india() {
printf(“I am in India function”);
}
void france(){
pritnf(“I am in France function”);
}
void italy() {
pritnf(“I am in italy function”);
}
Example: program with multiple functions
int main() {
printf(“I am in main function”);
india();
france();
italy();
return 0;
}
void india() {
printf(“I am in India function”);
}
void france(){
pritnf(“I am in France function”);
}
void italy() {
pritnf(“I am in italy function”);
}
Example: function
calls other function
int main() {
printf(“I am in main function”);
india();
printf(“Finally, I back in main function”);
return 0;
}
void india() {
printf(“I am in India function”);
france();
}
void france(){
pritnf(“I am in France function”);
italy();
}
void italy() {
pritnf(“I am in italy function”);
}
Example: function
calls other function
int main() {
printf(“I am in main function”);
india();
printf(“Finally, I back in main function”);
return 0;
}
void india() {
printf(“I am in India function”);
france();
}
void france(){
pritnf(“I am in France function”);
italy();
}
void italy() {
pritnf(“I am in italy function”);
}
Example: function
calls other function
int main() {
printf(“I am in main function”);
india();
printf(“Finally, I back in main function”);
return 0;
}
void india() {
printf(“I am in India function”);
france();
}
void france(){
pritnf(“I am in France function”);
italy();
}
void italy() {
pritnf(“I am in italy function”);
}
Example: function
calls other function
int main() {
printf(“I am in main function”);
india();
printf(“Finally, I back in main function”);
return 0;
}
void india() {
printf(“I am in India function”);
france();
}
void france(){
pritnf(“I am in France function”);
italy();
}
void italy() {
pritnf(“I am in italy function”);
}
Example: function
calls other function
int main() {
printf(“I am in main function”);
india();
printf(“Finally, I back in main function”);
return 0;
}
void india() {
printf(“I am in India function”);
france();
}
void france(){
pritnf(“I am in France function”);
italy();
}
void italy() {
pritnf(“I am in italy function”);
}
Example: function
calls other function
int main() {
printf(“I am in main function”);
india();
printf(“Finally, I back in main function”);
return 0;
}
void india() {
printf(“I am in India function”);
france();
}
void france(){
pritnf(“I am in France function”);
italy();
}
void italy() {
pritnf(“I am in italy function”);
}
Example: function
calls other function
int main() {
printf(“I am in main function”);
india();
printf(“Finally, I back in main function”);
return 0;
}
void india() {
printf(“I am in India function”);
france();
}
void france(){
pritnf(“I am in France function”);
italy();
}
void italy() {
pritnf(“I am in italy function”);
}
Example: function
calls other function
int main() {
printf(“I am in main function”);
india();
printf(“Finally, I back in main function”);
return 0;
}
void india() {
printf(“I am in India function”);
france();
}
void france(){
pritnf(“I am in France function”);
italy();
}
void italy() {
pritnf(“I am in italy function”);
}
Example: function
calls other function
int main() {
printf(“I am in main function”);
india();
printf(“Finally, I back in main function”);
return 0;
}
void india() {
printf(“I am in India function”);
france();
}
void france(){
pritnf(“I am in France function”);
italy();
}
void italy() {
pritnf(“I am in italy function”);
}
Example: function
calls other function
int main() {
printf(“I am in main function”);
india();
printf(“Finally, I back in main function”);
return 0;
}
void india() {
printf(“I am in India function”);
france();
}
void france(){
pritnf(“I am in France function”);
italy();
}
void italy() {
pritnf(“I am in italy function”);
}
Example: function
calls other function
int main() {
printf(“I am in main function”);
india();
printf(“Finally, I back in main function”);
return 0;
}
void india() {
printf(“I am in India function”);
france();
}
void france(){
pritnf(“I am in France function”);
italy();
}
void italy() {
pritnf(“I am in italy function”);
}
Example: function
calls other function
int main() {
printf(“I am in main function”);
india();
printf(“Finally, I back in main function”);
return 0;
}
void india() {
printf(“I am in India function”);
france();
}
void france(){
pritnf(“I am in France function”);
italy();
}
void italy() {
pritnf(“I am in italy function”);
}
Example: function
calls other function
int main() {
printf(“I am in main function”);
india();
printf(“Finally, I back in main function”);
return 0;
}
void india() {
printf(“I am in India function”);
france();
}
void france(){
pritnf(“I am in France function”);
italy();
}
void italy() {
pritnf(“I am in italy function”);
}
Example
int i =0;
i = i + 1;
if (i <=5) {
printf("IICT");
exit (0);
main();
}
return 0;
}
Output?
#include <stdio.h>
newI (int I) {
I = 5;
printf(“%d\n”, I);
}
main() {
int I;
I = 4;
printf(“%d\n”, I);
newI(I);
printf(“%d\n”, I);
}
Output?
#include <stdio.h>
newI (int I) {
I = 5;
printf(“%d\n”, I);
}
Same variable name,
main() {
which variable is
int I;
going to be considered
for output? I = 4;
printf(“%d\n”, I);
newI(I);
printf(“%d\n”, I);
}
Output?
Same variable name, which variable is #include <stdio.h>
going to be considered for output?
newI (int I) {
Scope I = 5;
printf(“%d\n”, I);
Answer is depends }
on scope of a variable
main() {
int I;
I = 4;
Scope printf(“%d\n”, I);
newI (int I) {
Here, variableI is created and destroyed , I = 5;
printf(“%d\n”, I);
when function is exited. }
main() {
int I;
I = 4;
Here, variableI is created and destroyed , printf(“%d\n”, I);
when function is exited. newI(I);
printf(“%d\n”, I);
}
Answer
This program will output #include <stdio.h>
three lines:
4 newI (int I) {
I = 5;
5
printf(“%d\n”, I);
4 }
main() {
int I;
I = 4;
printf(“%d\n”, I);
newI(I);
printf(“%d\n”, I);
}
Exercise : Output?
int main(){
int m = 1000;
function2();
printf(“%d\n”, m);
}
void function1(){
int m=10;
printf(“%d”,m);
}
void function2(){
int m=100;
function1();
printf(“%d”, m);
}
Exercise : Output?
int main(){
int m = 1000;
m = 1000
function2(); is local to main() function
printf(“%d\n”, m);
retrurn 0;
}
void function1(){
m = 10
int m=10;
is local to function1() function
printf(“%d”,m);
}
void function2(){
int m=100; m = 100
function1(); is local to function2() function
printf(“%d”, m);
}
Global variable
Unlike local variables, global variables can be accessed by
any function in the program.
int number = 0;
float length = 7.5;
int main(){ Global variables are declared
----------;
----------;
outside of function.
}
void function1(){ Variable number and length
---------- are available to all the functions
----------
}
void function2(){
----------
----------
}
Global In a case a local and a global variable have
variable a same name, the local variable will have
int count; precedence over the global one in
int main(){ the function where it is declared.
count = 10;
--------
--------
When function() references
} the variable count, it will be referencing
void function(){ only its local variable, not the global one.
{ The value of count in main will
int count=0; not be affected
----------
----------
count = count + 1;
}
Exercise: Output?
int x;
int main(){
x = 10;
printf(“%d”,x);
fun1(); void fun1(){
x = x + 10;
printf(“%d”,x); }
int fun2(){
printf(“%d”, fun2()); int x=1;
fun3(); x = x + 1;
return x;
printf(“%d”, x); }
return 0; void fun3(){
x = x + 10;
} }
Exercise: Output?
int x;
int main(){
printf(“%d”,x);
fun1();
printf(“%d”,x); void fun1(){
x = x + 10;
printf(“%d”, fun2()); }
int fun2(){
fun3(); int x=1;
printf(“%d”, x); x = x + 1;
return x;
return 0; }
} void fun3(){
x = x + 10;
}
Exercise: Output? Global variables are initialized to zero by
default.
int x;
int main(){
printf(“%d”,x);
fun1();
printf(“%d”,x); void fun1(){
x = x + 10;
printf(“%d”, fun2()); }
int fun2(){
fun3(); int x=1;
printf(“%d”, x); x = x + 1;
return x;
return 0; }
} void fun3(){
x = x + 10;
}
Output?
int main(){
int i;
for(i=1; i<=3;i++)
stat();
return 0;
}
void stat(){
int x = 0;
x = x + 1;
printf(“x = %d”, x);
}
Answer
int main(){
int i;
for(i=1; i<=3;i++)
stat(); Output:
return 0; X = 1
X = 1
} X = 1
void stat(){
int x = 0;
x = x + 1;
printf(“x = %d”, x);
}
The value of the static variable persists
until the end of program.
Output?
A static variable of two types:
int main(){ • Internal type
• External type
int i;
for(i=1; i<=3;i++)
stat();
return 0;
Internal type of static variable are declared
} inside the function. Its scope extends up
to end of the function
void stat(){ in which they are defined.
static int x = 0;
x = x + 1;
printf(“x = %d”, x);
}
The value of the static variable persists
until the end of program.
Output?
A static variable of two types:
int main(){ • Internal type
• External type
int i;
for(i=1; i<=3;i++)
stat();
return 0;
Internal type of static variable are declared
} inside the function. Its scope extends up
to end of the function
void stat(){ in which they are defined.
static int x = 0;
Output:
x = x + 1;
printf(“x = %d”, x); X = 1
X = 2
} X = 3
The value of the static variable persists
until the end of program.
Output?
A static variable of two types:
static int x = 5; • Internal type
int main(){ • External type
int i;
for(i=1; i<=3;i++)
stat();
printf(“The value of x in main=%d”,x);
return 0;
}
void stat(){
x = x + 1;
printf(“x = %d”, x);
}
The value of the static variable persists
until the end of program.
Output?
A static variable of two types:
static int x = 5; • Internal type
int main(){
int i;
for(i=1; i<=3;i++) • External type
stat();
x = x + 2;
printf(“The value of x in main=%d”,x);
return 0;
}
void stat(){
static int x=2;
x = x + 1;
printf(“x = %d”, x);
}
Output?
int main() {
int a = 20;
int b = 10;
{
int a = 0;
int c = a + b;
printf(“%d”, c);
}
printf(“%d”, a);
return 0;
}
Output?
int main() {
int i=5, j=2;
fun(i,j);
printf(“%d %d”,i,j);
return 0;
}
void fun(int i, int j){
i=i*i;
j= j*j;
}
1 /* Fig. 5.15: fig05_15.c
Outline
3 #include <stdio.h>
4
6
7 int main()
8 {
9 int result, number;
10
11 printf( "Enter an integer: " );
12 scanf( "%ld", &number );
13 result = fibonacci( number );
14 printf( "Fibonacci( %ld ) = %ld\n", number, result );
15 return 0;
16 }
17
18 /* Recursive definition of function fibonacci */
19 int fibonacci( int n )
20 {
21 if ( n == 0 || n == 1 )
22 return n;
23 else
24 return fibonacci( n - 1 ) + fibonacci( n - 2 );
25 }
Enter an integer: 0
Fibonacci(0) = 0
Enter an integer: 1
Fibonacci(1) = 1
© 2000 Prentice Ha
Enter an integer: 2
Fibonacci(2) = 1 Outline
Enter an integer: 3
Fibonacci(3) = 2
Program Output
Enter an integer: 4
Fibonacci(4) = 3
Enter an integer: 5
Fibonacci(5) = 5
Enter an integer: 6
Fibonacci(6) = 8
Enter an integer: 10
Fibonacci(10) = 55
Enter an integer: 20
Fibonacci(20) = 6765
Enter an integer: 30
Fibonacci(30) = 832040
Enter an integer: 35
Fibonacci(35) = 9227465
© 2000 Prentice Ha
Recursion: The Fibonacci Series
Fibonacci series: 0, 1, 1, 2, 3, 5, 8...
Each number is the sum of the previous two
Can be solved recursively:
fib( n ) = fib( n - 1 ) + fib( n – 2 )
Code for the fibaonacci function
long fibonacci( long n )
{
if (n == 0 || n == 1) // base case
return n;
else
return fibonacci( n - 1) +
fibonacci( n – 2 );
}
return f( 2 ) + f( 1 )
return f( 1 ) + f( 0 ) return 1
return 1 return 0
Example
125
Common Functions (continued)
In Kernighan & Ritchie
<assert.h> // for diagnostics, loop invariants, etc.
<stdarg.h> // for parsing arguments
<time.h> // time of day and elapsed time
<limits.h> // implementation dependent numbers
<float.h> // implementation dependent numbers.
126
Programming exercises
Exercise
Any year is entered through the keyboard. Write a function
to determine whether the year is leap year or not.