Docsity

Functions-Introduction to Programming-Lecture Slides, Slides of Computer Programming

Partial preview of the text

Download Functions-Introduction to Programming-Lecture Slides and more Slides Computer Programming in PDF only on Docsity!

Dr. Hanif Durad

2

Lecture Outline

Functions^ 

Prototype

Calling Functions

Call by Value

Variable Scopes

Recursion

Iteration

Preprocessing

Functions^ 

Functions are modules with parameters (orarguments), statements, and a result

Why functions?^ 

Decomposition

Code reuse

Dr. Hanif Durad

D:\C\CSCI-1190\ lecture-3.ppt, P-

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/

Dr. Hanif Durad

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 */

Dr. Hanif Durad

FindingPrime Numbers (1/2)#include <stdio.h>#include <math.h>main(){

int i;for( i = 1; i <= 100; i++ ) {printf("%d", i);if ( isPrime(i) )

printf("*");if ( i % 10 == 0 )printf("\n");elseprintf("\t"); }^

Dr. Hanif Durad

8

D:\C\Numerical Methods and Programming\lecture3.ppt

prime.c

Function for Prime Numbers(2/2)int isPrime(int num){

int

i;

if ( num < 3 ) return 1;for (i=2; i<=(int) sqrt(num);i++ )if ( num % i == 0 ) break;

if ( num % i ) return 1;return 0; }

Dr. Hanif Durad

9

Random Number Generator1.

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

return 0;

rand_gen.c

In-Class Exercise 3-

Write a program to generate 1000 floating pointrandom numbers between 0 and 1.

Find out the largest number, the smallest numberand the average.

Variable Scopes (1/2)

A scope of a variable is the portion of theprogram where the variable can be recognized

File scope^ 

global variables

Function scope^ 

local variables, parameters

Block scope^ 

local variables

Variable Scopes (2/2)

The creation and destruction of variables can becontrolled:^ 

extern: the variable is defined in a different module

static: for local variables: makes them last the entireprogram, for global variables: restricts the scope tothe current module

register: try to use a CPU register for the variable

auto: default for local variables

D:\C\Hanif Search\C.tn.pdf

Variable Scopes: An Example

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

Dr. Hanif Durad

17

scope_example.c

Recursion

A function can call itself, either directly orindirectly through other functions

Some conditional code is needed to stop therecursion

Recursion is equivalent to iteration

Factorial: Recursion1.^

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

printf("Enter n: ");

scanf("%d",&n);

printf("%d! = %d\n",n,factorial(n));

return 0;

fact_recurs.c

Factorial: Iteration1.^

int factorial(int n)

int i,f;

for(i=1,f=1;i<=n;i++)

f*=i;

return f;

fact_iter.c