0% found this document useful (0 votes)
40 views22 pages

CSCI-1190: Beginning C Programming For Engineers: Lecture 3: Functions Gang Chen

This document provides an overview of functions in the C programming language as covered in the lecture CSCI-1190: Beginning C Programming for Engineers. It discusses function prototypes, definitions, parameters, return types, scope, recursion, and provides examples of math functions, random number generation, and calculating factorials recursively and iteratively.

Uploaded by

Abdul Wahab
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views22 pages

CSCI-1190: Beginning C Programming For Engineers: Lecture 3: Functions Gang Chen

This document provides an overview of functions in the C programming language as covered in the lecture CSCI-1190: Beginning C Programming for Engineers. It discusses function prototypes, definitions, parameters, return types, scope, recursion, and provides examples of math functions, random number generation, and calculating factorials recursively and iteratively.

Uploaded by

Abdul Wahab
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

CSCI-1190: Beginning C

Programming for Engineers


Lecture 3: Functions
Gang Chen
Control Structures
• if, if-else, switch
• while, do-while
• for
From 1 to 100 step 1

int i=1; int i=1; for(i=1;i<=100;i++)


do{ while(i<=100)
{
{
… …

i++; i++; }
} while (i<=100); }
From 1 to 100 step 2

int i=1; int i=1; for(i=1;i<=100;i+=2)


do{ while(i<=100)
{
{
… …

i=i+2; }
i+=2;
} while (i<=100); }

Question: What is the value of i in the last iteration?


From 100 to 1 step -1

int i=100; int i=100; for(i=100;i>0;i--)


do{ while(i>0)
{
{
… …

i--; i--; }
} while (i>0); }
1,2,4,8,16,32,64,…,1024
int i=1; int i=1; for(i=1;i<=1024;i*=2)
do{ while(i<=1024)
{
{
… …

i*=2; }
i*=2;
} while (i<=1024); }

Other sequences:
• 1,-2,3,-4,5,-6,7,-8,…
• 1,1/2,1/4,1/8,1/16,1/32,1/64,…
• 1,1/2,1/3,1/4,1/5,1/6,1/7,…
• 1,2,3,5,8,13,21,34,…
• …
Functions
• Functions are modules with parameters
(or arguments), statements, and a result
• Why functions?
– Decomposition
– Code reuse
Prototypes and Definitions
• A prototype declares a function with a return type and a parameter
list.
float power (float a, float b);
• A definition implements the function
float power (float a, float b)
{
float s;
/* calculate the power of a and b */
/* put the result into the variable s*/
return s;
}
• The definition must match the prototype
• Either the prototype or the definition must appear before the function
call
Calling Functions
• Variable(s) must match the parameter(s)
– Same number, same order, compatible type,
– Names could be different
• How about printf, scanf?
printf(“hello”);printf(“a=%d”,a);
– Variable-length parameter list
int printf(char*, … ); /*stdio.h*/
Type Conversion
float power(float a, float b);

float x,y,z;
int i,j;
power(x); /* error */
power(x,y,z); /* error */
power(i,j); /* ok */
void
• For functions taking no parameters
int f(void);
• For functions returning no values
void g(void);
Math Functions
•Header file: #include <math.h>
•Compiling: gcc –Wall –o prog prog.c -lm
sqrt(x) Square root of x
exp(x) Exponential function
log(x) Natural logarithm of x
fabs(x) Absolute value of x
pow(x,y) x raised to power y
sin(x) Trigonometric sine of x
cos(x) Trigonometric cosine of x
tan(x) Trigonometric tangent of x
Random Number Generator
1. #include <stdio.h>
2. #include <stdlib.h> /* srand() and rand() */
3. #include <time.h> /* time() */
4. int main()
5. {
6. float r;
7. srand(time(NULL));
8. r=1.0*rand()/RAND_MAX;
9. printf("Random number: %f\n",r);
10. return 0;
11. }
In-Class Exercise 3-1
• Write a program to generate 1000 floating
point random numbers between 0 and 1.
• Find out the largest number, the smallest
number and the average.
Call by Value
• The execution of a function does not affect
the value of variables passed as
parameters
void increment(int a)
{
a++;
}
int main()
{
int i=1;
increment(i);
printf("i=%d\n",i); /* i=1, not 2 */
}
Variable Scopes
• A scope of a variable is the portion of the
program where the variable can be
recognized
• File scope
– global variables
• Function scope
– local variables, parameters
• Block scope
– local variables
Variable Scopes: An Example
1. #include <stdio.h>
2. int x; /* global variable */
3. int f(float x) /* parameter */
4. {
5. return 0;
6. }
7. int g()
8. {
9. int x; /* local variable */
10. return 0;
11. }
12. int main()
13. {
14. float x; /* local variable */
15. {
16. int x; /* local variable */
17. }
18. return 0;
19. }
Recursion
• A function can call itself, either directly or
indirectly through other functions
• Some conditional code is needed to stop
the recursion
• Recursion is equivalent to iteration
Factorial
• The factorial of an integer:
– n!=n*(n-1)*(n-2)*…*2*1;
– 1!=1;0!=1;
• Recursive equation:
– n!=n*(n-1)!, if n=0;
– n!=1, if n=0;
Factorial: Recursion
1. #include <stdio.h>
2. int factorial(int n)

3. {

4. if(n==0) return 1;
5. else return n*factorial(n-1);
6. }

7. int main()

8. {

9. int n;
10. printf("Enter n: ");
11. scanf("%d",&n);
12. printf("%d! = %d\n",n,factorial(n));
13. return 0;
14. }
Factorial: Iteration
1. int factorial(int n)
2. {
3. int i,f;
4. for(i=1,f=1;i<=n;i++)
5. f*=i;
6. return f;
7. }
In-Class Exercise 3-1
• Use recursion to implement the power
function that takes two integers.
– Hint: power(a,b)=a*power(a,b-1);

You might also like