

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This document offers an introduction to functions in c programming, covering their definition, usage, and advantages in modular programming. it explores various aspects, including function arguments, return values, void functions, and the use of math library functions. the document also illustrates top-down design principles and their application in creating structured programs. Examples and case studies are included to enhance understanding.
Typology: Summaries
1 / 3
This page cannot be seen from the preview
Don't miss anything!
This lesson focuses on functions and their use in writing modular programs.
The primary objective is to understand functions and how they facilitate the creation of programs with distinct modules.
Two case studies are presented: finding the area and circumference of a circle, and computing the weight of a batch of flat washers. The circle example contrasts a non-modular implementation with a modular one. The flat washer example uses a formula to compute the rim area.
Some operations require functions beyond predefined operators. A function is a program unit performing a specific operation, acting as a "black box" where only inputs and outputs are known.
Square roots in C are computed using a function from the math library. Math functions can be integrated into C statements and expressions, such as z = a + sqrt(b-c);. All math functions accept and return values of type double. For example, double x=100; printf ('The square root of %0.1lf is %0.1lf', x, sqrt(x)); will output "The square root of 100. is 10.0".
Several other math functions are available:
y=floor (x): returns the largest whole number less than or equal to x. y=fabs(x): returns the absolute value of x. sin(x), cos(x), and tan(x): trigonometric functions that return the sine, cosine, and tangent of an angle in radians. Radians can be calculated from degrees using the formula: radians = degrees * PI / 180; y = exp (x): returns e to the power of x. z = pow (x,y): returns x to the power of y.
atan(x): calculates the arc tangent of a real number, returning an angle in radians.
Other functions reside in the standard library, requiring #include <stdlib.h> to be used.
Shorthand notations exist for modifying a variable's value based on its current value:
i=i+1; can be shortened to ++i or i++; i=i-1; can be shortened to --i or i--;
The prefix (++i) and postfix (i++) forms of incrementing a variable are equivalent regarding the final value of i.
Top-down design is a problem-solving method. Structure charts can visually represent the organization of a program, such as one for drawing a stick figure.
Function subprograms offer procedural abstraction, where the main function consists of function calls, and each function is implemented separately. They also allow for reuse, as functions can be executed multiple times within a program.
The math library functions are predefined examples of functions. Functions can accept multiple arguments but return only one result. A function is like a separate program called by the main program to perform a specific task.
The syntax for defining a function is:
functiontype functionname (type and names of parameters separated by comma Local variable declarations
... }
Before writing a function, problem analysis is necessary. A function can be expressed as: