0% found this document useful (0 votes)
13 views140 pages

C Functions

The document discusses the concept of functions in C programming, including their definition, benefits, and examples of usage. It provides code snippets for calculating sums of integers and demonstrates how to create user-defined functions for various tasks. Additionally, it emphasizes the importance of reusability and reducing complexity in programming.
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)
13 views140 pages

C Functions

The document discusses the concept of functions in C programming, including their definition, benefits, and examples of usage. It provides code snippets for calculating sums of integers and demonstrates how to create user-defined functions for various tasks. Additionally, it emphasizes the importance of reusability and reducing complexity in programming.
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/ 140

Function

Kind Acknowledgment for material


Our kind acknowledgement to the following source for the
references and exercise:

Let us C by yashwant Kanetkar


Programming in ANSI C by E. Balagurusamy
C How to Program by Paul Deitel and Harvey Deitel
Programming in C by Pradip dey and Manas Ghosh
Opening Problem

Find the sum of integers


(a) from 1 to 10
(b) from 20 to 30
(c) from 35 to 45
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);

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

printf("Sum from 20 to 30 is %d“, sum(20, 30));

printf("Sum from 35 to 45 is %d “, sum(35, 45));


return 0;
} 6
Benefit of function

Write a method once and


reuse it anywhere.

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

• Write a method once and reuse it anywhere.


• Within a particular program or across different programs

• Hide the implementation from the user.


• Reduce complexity.
• Smaller blocks of code
• Easier to read

• Allows independent development of code

10
Functions - Mathematical View

Returned
X Function value
Function Input and Output
Sample Function Call
#include <stdio.h>

int main ( ) printf is the name of a predefined


{ function in the stdio library

printf (“Hello World!\n”) ; this statement is


return 0 ; is known as a
} function call
this is a string we are passing
as an argument (parameter) to
the printf function
Functions
The ‘building blocks’ of a C program
You’ve used predefined functions already:
main()
printf(), scanf(), pow()
User-defined functions
Your own code
In combination with predefined functions
int main(){
int a,b,c,sum;
printf(“Enter three number”);
scanf(“%d %d %d”,&a,&b,&c);
sum = calculateSum(a,b,c);
printf(“Sum=%d”, sum);
return 0;
}

int calculateSum(int x, int y, int z){


int d;
d = x+y+z;
return d;
}
int main(){
int a,b,c,sum;
printf(“Enter three number”);
scanf(“%d %d %d”,&a,&b,&c);
sum = calculateSum(a,b,c);
printf(“Sum=%d”, sum);
return 0;
}

int calculateSum(int x, int y, int z){


int d;
d = x+y+z;
return d;
}
int main(){
int a,b,c,sum;
printf(“Enter three number”);
scanf(“%d %d %d”,&a,&b,&c);
sum = calculateSum(a,b,c);
printf(“Sum=%d”, sum);
return 0;
}

int calculateSum(int x, int y, int z){


int d;
d = x+y+z;
return d;
}
int main(){
int a,b,c,sum;
printf(“Enter three number”);
scanf(“%d %d %d”,&a,&b,&c);
sum = calculateSum(a,b,c);
printf(“Sum=%d”, sum);
return 0; Call
}

int calculateSum(int x, int y, int z){


int d;
d = x+y+z;
return d;
}
int main(){
int a,b,c,sum;
printf(“Enter three number”);
scanf(“%d %d %d”,&a,&b,&c);
sum = calculateSum(a,b,c);
printf(“Sum=%d”, sum);
return 0;
}

int calculateSum(int x, int y, int z){


int d;
d = x+y+z;
return d;
}
int main(){
int a,b,c,sum;
printf(“Enter three number”);
scanf(“%d %d %d”,&a,&b,&c);
sum = calculateSum(a,b,c);
printf(“Sum=%d”, sum);
return 0;
}

int calculateSum(int x, int y, int z){


int d;
d = x+y+z;
return d;
}
int main(){
int a,b,c,sum;
printf(“Enter three number”);
scanf(“%d %d %d”,&a,&b,&c);
sum = calculateSum(a,b,c);
printf(“Sum=%d”, sum);
return 0;
}

int calculateSum(int x, int y, int z){


int d;
d = x+y+z;
return d;
}
int main(){
int a,b,c,sum;
printf(“Enter three number”);
scanf(“%d %d %d”,&a,&b,&c);
sum = calculateSum(a,b,c);
printf(“Sum=%d”, sum); Return
return 0;
}
Value
int calculateSum(int x, int y, int z){
int d;
d = x+y+z;
return d;
}
int main(){
int a,b,c,sum;
printf(“Enter three number”);
scanf(“%d %d %d”,&a,&b,&c);
sum = calculateSum(a,b,c);
printf(“Sum=%d”, sum);
return 0;
}

int calculateSum(int x, int y, int z){


int d;
d = x+y+z;
return d;
}
int main(){
int a,b,c,sum;
printf(“Enter three number”);
scanf(“%d %d %d”,&a,&b,&c);
sum = calculateSum(a,b,c);
printf(“Sum=%d”, sum);
return 0;
}

int calculateSum(int x, int y, int z){


int d;
d = x+y+z;
return d;
}
int main(){
int a,b,c,sum;
printf(“Enter three number”);
scanf(“%d %d %d”,&a,&b,&c);
sum = calculateSum(a,b,c);
printf(“Sum=%d”, sum);
return 0; Method
name
}

int calculateSum(int x, int y, int z){


int d;
d = x+y+z;
return d;
}
int main(){
int a,b,c,sum;
printf(“Enter three number”);
scanf(“%d %d %d”,&a,&b,&c);
sum = calculateSum(a,b,c);
printf(“Sum=%d”, sum); Method
parameters
return 0;
}

int calculateSum(int x, int y, int z){


int d;
d = x+y+z;
return d;
}
int main(){
int a,b,c,sum;
printf(“Enter three number”);
scanf(“%d %d %d”,&a,&b,&c);
sum = calculateSum(a,b,c);
printf(“Sum=%d”, sum);
return 0;
}

int calculateSum(int x, int y, int z){


int d;
Body
d = x+y+z; of function,
return d; It is a collection of statements that are
grouped together to perform an operation.
}
int main(){
int a,b,c,sum;
printf(“Enter three number”);
scanf(“%d %d %d”,&a,&b,&c);
sum = calculateSum(a,b,c);
printf(“Sum=%d”, sum); A function,
return 0; which returns value
}

int calculateSum(int x, int y, int z){


int d;
d = x+y+z;
return d;
}
summary - Definition Structure

Function 'header' type function_name (type arg1, type arg2 )


Return data type
(if any) {
Name statements;
Descriptive }
Arguments (or parameter
list)
Notice: data type and name A function that calculates the product of two numbers
Statements double product(double x, double y)
Variable declaration {
Operations double result;
Return value (if any) result = x * y;
return result;
}
Function - Practice 1

Write a function named 'sum'


sums two integers
returns the sum
2 min. on your own
Share with neighbor
Function - sum()

int sum_int(int x, int y)


{
int result;

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

Write a program that prints square of 1 to 10 using


function.
Solution
Example

Write a program that determines the largest number from


a set of 3 numbers.
2 Finding the maximum of three integers */ Outline
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
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 17
Maximum is: 85
© 2000 Prentice Ha
Solution
Exercise
Exercise
Exercise
Exercise
Exercise
A function to divide two floating points numbers is as follows:

divide (float x, float y)


{
return x/y;
}
What will be the value of the following “function calls”?
(a) divide(10,2);
(b) divide(9, 2);
(c) divide(4.5, 1.5);
(d) divide(2.0, 3.0) ;
What will be the effect on the function calls if we
change the header line as follows:
(a) int divide(int x, int y);
(b) float divide(float x, float y);
Exercise
Determine the output of the following program?
int main(){
int x=10;
int y=20;
int p,q;
p = prod(x, y);
q = prod(p, prod(x,y));
printf(“%d %d”, p,q);
return 0;
}

int prod(int a, int b) {


return a*b;
}
Exercise
int test(int number){
int m,n=0;
while(number){
m = number % 10;
if(m % 2)
n = n+1;
number = number/10;
}
return n;
}
What will be the values of x and y when the
following statements are executed?
int x = test(135);
int y = test(246);
Exercise
Find the error in each of the following program segments
and explain how the error can be corrected.
Exercise
Find the error in each of the following program segments
and explain how the error can be corrected.
Exercise
Find the error in each of the following program segments
and explain how the error can be corrected.
Exercise
Find the error in each of the following program segments
and explain how the error can be corrected.
Exercise
Exercise
Exercise
int main() {

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

The scope of variable determines over newI(I);


printf(“%d\n”, I);
what region of the
program a variable is actually available }
for the use.
Output?
Same variable name, which variable is #include <stdio.h>
going to be considered for output?
newI (int I) {
I is a automatic ( or local ) variable.
They are created when the function
I = 5;
printf(“%d\n”, I);
is called and destroyed automatically
}
when the function is existed.
main() {
• One important feature int I;
is that their value cannot be
changed accidentally
by what happens in some
I = 4;
printf(“%d\n”, I);
other function in the program.
This assure that we may declare newI(I);
and use the same variable name printf(“%d\n”, I);
in different functions in the
}
same program without causing
any confusion to the compiler.
Answer
#include <stdio.h>

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 );
}

Functions that call itself


Recursion: The Fibonacci Series
Set of recursive calls to function fibonacci
f( 3 )

return f( 2 ) + f( 1 )

return f( 1 ) + f( 0 ) return 1

return 1 return 0
Example

Find a factorial of a given number using recursion.


Solution
Exercise

Write a function that returns the smallest of three floating


point numbers.
Exercise

Write a function to calculate the factorial value of any


integer entered through the keyboard.
Exercise

Write a function to calculate power(a,b), to calculate


the value of a raised to b.
Exercise
Exercise
int main() {
int i = 1;
{
int i = 2;
{
int i = 3;
printf(“%d”, i);
}
printf(“%d”, i);
}
printf(“%d”, i);
}
exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Reference
Common Functions

#include <math.h> #include <stdio.h>


sin(x) // radians printf()
cos(x) // radians fprintf()
tan(x) // radians scanf()
atan(x) sscanf()
atan2(y,x) ...
exp(x) // ex #include <string.h>
log(x) // loge x strcpy()
log10(x) // log10 x strcat()
sqrt(x) // x ≥ 0 strcmp()
pow(x, y) // xy strlen()
... ...

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.

<setjmp.h> // beyond scope of this course


<signal.h> // beyond scope of this course

126
Programming exercises
Exercise
Any year is entered through the keyboard. Write a function
to determine whether the year is leap year or not.

A positive integer is entered through the keyboard. Write


a function to obtain the prime factors of this number.
For example, prime factors of 35 are 5 and 7.
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Develop our own functions for performing the following
operations:
(a) Copying one string to another
(b) Comparing two strings
(c) Adding a string to the end of another string

Write a program to test your functions.


Exercise
Write a program that invokes a function called find()to
perform the following tasks:
(a) Receives a character array and a single character
(b) Return 1 if the specifies character is found in the
array, o otherwise
Exercise
Design a function locate() that takes two character
array s1 and s2 and one integer value m as parameters
and insert the string s2 and s1 immediately after the index
m.
Exercise
Develop a top-down modular program that will perform
the following tasks:
(a) Read two integer arrays with unsorted elements
(b) Sort them in increasing order.
(c) Merge the sorted list
(d) Print the sorted list.
Exercise
Write a function exchange to interchange the value of
two variables x and y. illustrate the use of this function, in
the calling function, Assume that x and y are defined as
global variables.
Exercise
Write a function that receives a floating point value x an
returns it as a value rounded to two nearest decimal places.
For example, the value 123.4567 will be rounded to
123.46 ( Hint: seek help of one of the math functions
available in the math library).

You might also like