1 / 21

Functions in Computer Science - Break down problems and improve code readability

Functions in computer science help break down complex problems into smaller sub-tasks, making it easier to solve them. They also improve code readability and maintainability. Learn more about functions in this department.

eapple
Download Presentation

Functions in Computer Science - Break down problems and improve code readability

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions Department of Computer Science-BGU

  2. Why use functions? • They can break your problem down into smaller sub-tasks (modularity). • easier to solve complex problems • They make a program much easier to read and maintain • abstraction – we don’t have to know how a function is implemented to use it (printf, scanf, etc.). • Generalize a repeated set of instructions • we don’t have to keep writing the same thing over and over . Department of Computer Science-BGU

  3. Functions • A group of declarations and statements that is assigned a name • effectively, a named statement block . • usually return 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… Department of Computer Science-BGU

  4. Example - Square #include <stdio.h> double square(double a) { return a*a; } void main() { double num; printf("enter a number\n"); scanf("%lf",&num); printf("square of %g is %g\n",num,square(num)); } This is a function defined outside main Here is where we call the function square Department of Computer Science-BGU

  5. Characteristics of Functions returnType name(argType1arg_name1, argType2,arg_name2, …) { function body; return value; } Department of Computer Science-BGU

  6. Return Statement • Return causes the execution of the function to terminate and usually 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). • If no value is to be returned, the return-type of the function should be set to ‘void’. Department of Computer Science-BGU

  7. Factorial calculation example #include <stdio.h> int factorial(int n) { inti, fact = 1; for (i=2; i<=n; i++) fact *= i; return fact; } void main() { int num; printf("enter a number\n"); scanf("%d",&num); printf(“The factorial of %d is %d\n",num,factorial(num)); } Department of Computer Science-BGU

  8. A Detailed Example Write a program that receives a nominator and a denominator from the user, and displays the reduced form of the number. For example: if the input is 6 and 9, the program should display 2/3. Department of Computer Science-BGU

  9. Example – solution (step I) #include <stdio.h> int main(void) { int n, d; printf("Please enter nominator and denominator: "); scanf("%d%d", &n, &d); Calculate n’s and d’s Greatest Common Divisor printf("The reduced form of %d/%d is %d/%d", n, d, n/gcd, d/gcd); return 0; } Department of Computer Science-BGU

  10. Example – solution (step II) #include <stdio.h> int main(void) { int n, d, g; printf("Please enter nominator and denominator: "); scanf("%d%d", &n, &d); g = gcd(n, d); printf("The reduced form of %d/%d is %d/%d", n, d, n/g, d/g); return 0; } Department of Computer Science-BGU

  11. Example – solution (step III) /* Returns the greatest common divisor of its two parameters. Assumes both are positive. The function uses the fact that if r = mod(y, x) then the gcd of y and x equals the gcd of x and r. */ int gcd(int x, int y) { int tmp; while(x > 0) { tmp = x; x = y % x; y = tmp; } return y; } Department of Computer Science-BGU

  12. 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. • If the function receives no parameters, the parameter list should be replaced by ‘void’. Department of Computer Science-BGU

  13. 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 Department of Computer Science-BGU

  14. Scope of variables • The scope of a variable is the boundaries within which it can be used in a program. • Normally variables are local in scope - this means they can only be used in the function where they are declared. • A variable declared within a function is unrelated to variables declared elsewhere, even if they have the same name • Global variables can be declared , which can be used in any function. • Global variables are a BAD PRACTICE. Department of Computer Science-BGU

  15. Function swap void swap(int x, int y){ int temp; temp = x; x = y; y = temp; } void main() { int a=7,b=8; printf("a = %d, b = %d\n", a, b); swap(a,b); printf("a = %d, b = %d\n", a, b); } Department of Computer Science-BGU

  16. Function Declaration - Prototype #include <stdio.h> int factorial(int a); /* Function Declaration! – Prototype */ void main(){ int num; printf("enter a number\n"); scanf("%d",&num); printf("%d!=%d\n",num,factorial(num)); } int factorial(int a){ inti,b=1; for(i=1; i<=a; i++) b=b*i; return b; } Department of Computer Science-BGU

  17. 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 ); Department of Computer Science-BGU

  18. 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 Department of Computer Science-BGU

  19. Passing arrays to functions • Functions can accept arrays as arguments. • Usually the array’s size also needs to be passed (why?). • For example - int CalcSum(int arr[], int size); • It is not necessary when the size of the array fix in a #define declaration. • Within the function, arr is accessed in the usual way. Department of Computer Science-BGU

  20. Passing arrays to functions • Arrays can be passed to functions and have their values changed from within the function! • Unlike regular variables • This is possible because an array variable is actually an address and the function receive this address. Department of Computer Science-BGU

  21. Two Dimensional Arrays • When passing a two-dimensional array to a function, it is necessary to indicate the size of the second dimension in the function header • But this does not mean the array’s size needn’t be passed as well int func(int arr[][4], int a); • Same was true when initializing the array double B[][2] = {{1,2}, {2,3}, {3,4}}; Department of Computer Science-BGU

More Related