Functions Exercise 5. Functions a group of declarations and statements that is assigned a name ...

35
Functions Exercise 5

Transcript of Functions Exercise 5. Functions a group of declarations and statements that is assigned a name ...

Page 1: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Functions

Exercise 5

Page 2: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Functions

a group of declarations and statements that is assigned a name effectively, a named statement block usually has a value

a sub-program when we write our program we always define a

function named main inside main we can call other functions

which can themselves use other functions, and so on…

Page 3: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Example - Square #include <stdio.h>

double square(double a) { return a*a;}

int main(void) { double num;

printf("enter a number\n"); scanf("%lf",&num);

printf("square of %g is %g\n",num,square(num));

return 0;}

This is a function defined outside main

Here is where we call the function square

Page 4: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Why use functions?

they can break your problem down into smaller sub-tasks easier to solve complex problems

generalize a repeated set of instructions we don’t have to keep writing the same thing over

and over printf and scanf are good examples…

they make a program much easier to read and maintain

Page 5: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Characteristics of Functions

return-type name(arg_type1 arg_name1, arg_type2 arg_name2, …)

{

function body;

return value;

}

double square(double a){

return a*a;}

int main(void){

…}

Page 6: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Return Statement

Return causes the execution of the function to terminate and returns a value to the calling function

The type of the value returned must be the same as the return-type defined for the function (or a ‘lower’ type)

Page 7: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Exercise

Write a program that gets a positive integer from the user and prints all the prime numbers from 2 up to that integer.

(Use a function that returns 1 if its parameter is prime, 0 otherwise)

Page 8: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Solution

is_prime_func.c

Page 9: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

The Great Void

Sometimes there’s no reason for a function to return a value

In these cases, the function return type should be ‘void’

If the ‘return’ keyword is used within such a function it exits the function immediately. No value needs be specified

Page 10: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

The Great Void

Calling ‘return’ in a function returning void is not obligatory

If the function receives no parameters, the parameter list should be replaced by ‘void’ (or just nothing)

Page 11: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Example

void ShowHelp(void){

printf("This function explains what this program does…\n");

printf("Yadayadayada"); /* ... */}

int main(void){

char choice;

printf("Please enter your selection: ");scanf("%c", &choice);

if (choice==‘h’) ShowHelp();else if /* Program continues … */

}

Page 12: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Pass-by-value

Function arguments are passed to the function by copying their values rather than giving the function direct access to the actual variables

A change to the value of an argument in a function body will not change the value of variables in the calling function

Example – add_one.c

Page 13: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

add_one – step by step

int add_one(int b){

b=b+1;return b;

}

int main(void){

int a=34,b=1;

a = add_one(b);

printf("a = %d, b = %d\n", a, b);return 0;

}

a b

34 1

main() memory state

Page 14: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

a b

34 1

int add_one(int b){

b=b+1;return b;

}

int main(void){

int a=34,b=1;

a = add_one(b);

printf("a = %d, b = %d\n", a, b);return 0;

}

main() memory state

add_one – step by step

Page 15: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

add_one – step by step

a b

34 1

b

1

add_one memory state

main() memory state

int add_one(int b){

b=b+1;return b;

}

int main(void){

int a=34,b=1;

a = add_one(b);

printf("a = %d, b = %d\n", a, b);return 0;

}

Page 16: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

int add_one(int b){

b=b+1;return b;

}

int main(void){

int a=34,b=1;

a = add_one(b);

printf("a = %d, b = %d\n", a, b);return 0;

}

a b

34 1

add_one – step by step

b

2

add_one memory state

main() memory state

Page 17: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

a b

34 1

add_one – step by step

b

2

add_one memory state

main() memory state

int add_one(int b){

b=b+1;return b;

}

int main(void){

int a=34,b=1;

a = add_one(b);

printf("a = %d, b = %d\n", a, b);return 0;

}

Page 18: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

add_one – step by step

a b

2 1

main() memory state

int add_one(int b){

b=b+1;return b;

}

int main(void){

int a=34,b=1;

a = add_one(b);

printf("a = %d, b = %d\n", a, b);return 0;

}

Page 19: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

add_one – step by step

a b

2 1

main() memory state

int add_one(int b){

b=b+1;return b;

}

int main(void){

int a=34,b=1;

a = add_one(b);

printf("a = %d, b = %d\n", a, b);return 0;

}

Page 20: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Riddle me this

#include <stdio.h>

int factorial(int n){

int fact = 1;

while (n>1) { fact *= n; n--; }

return fact;}

int main(void){ int n;

printf("enter a number\n"); scanf("%d",&n);

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

/* What will this print? */ printf("n = %d\n", n); return 0;}

Page 21: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Scope of variables

A variable declared within a function is unrelated to variables declared elsewhere, even if they have the same name

A function cannot access variables that are declared in other functions

Example – scope.c

Page 22: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Wrong way to do it

int add_one(int b){

a=b+1;}

int main(void){

int a=34,b=1;

add_one(b);

printf("a = %d, b = %d\n", a, b);return 0;

}

Page 23: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Function Declaration

Most software projects in C are composed of more than one file

We want to be able to define the function in one file, and to use it in all files

Page 24: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Function Declaration

For this reason, the function must be declared in every file in which it’s called, before it’s called for the first time

the declaration contains:

return_type Function_name(argument types);

Page 25: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Function Declaration

#include <stdio.h>

int factorial(int a); /* Function Declaration! */

int main(void){ int num;

printf("enter a number\n"); scanf("%d", &num);

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

return 0;}

int factorial(int a){ int i, b = 1; for(i = 1; I <= a; i++) b = b*i; return b;}

Page 26: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Function Declaration

stdio.h actually contains a large set of function declarations

The #include directive tells the compiler to insert these declarations into the file, so that these functions could be called

Page 27: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

The math library

A collection of mathematical functions Need to include the header file math.h

(#include <math.h>) Use functions of the library, e.g.

double s,p;

s = sqrt(p); Declared in math.h :

double sqrt (double x);

Page 28: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

The math library

sin(x), cos(x), tan(x) x is given in radians

asin(x), acos(x), atan(x) log(x) sqrt(x) pow(x,y) – raise x to the yth power. ceil(x), floor(x) …and more

Page 29: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Exercise

Write a function that uses the formula

in order to approximate . The function should acceptan argument n which determines the number of terms inthe formula. It should return the approximation of .

Write a program that gets an integer n from the user,and approximate using n terms of the above formula.

2

21

π 1 1 1 11

6 4 9 16n n

Page 30: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Solution

pi.c

Page 31: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Exercise

Modify the previous function that approximates

. The function should accept an argumentspecifying the desired accuracy, and keepadding terms until the contribution of the nextterm drops below this level.

Write a program that gets a (small) doubleepsilon from the user, and approximates within this function.

Page 32: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

Solution

pi_eps.c

Page 33: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

The debugger

Some programs may compile correctly, yet not produce the desirable results.

These programs are valid and correct C programs, yet not the programs we meant to write!

The debugger can be used to follow the program step by step and may help detecting bugs in an already compiled program.

Page 34: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

The debugger’s common features

Setting breakpoints (a point where the execution stops): bring the cursor to desired line and press the palm icon or F9. A dark red dot appears near the line.

Executing a debugged run: Build->start debug->go or F5. The program will run and stop at the first breakpoint.

Page 35: Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.

The debugger’s common features (cont.)

Stopping at a specific line: Bringing the cursor to the line and press ctrl+F10, or Build->start debug->go to cursor. The program will stop at that point.

Stepping to the next line – F10. Entering a function – F11. Seeing variable values – quickwatch and/or

debug window at the bottom. The yellow arrow indicates our whereabouts

at any given moment.