New version page

Purdue CS 15900 - Functions

Upgrade to remove ads

This preview shows page 1-2-3-4-5-6 out of 17 pages.

Save
View Full Document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience

Upgrade to remove ads
Unformatted text preview:

Chapter 4FunctionsCS 159 - C ProgrammingFunctions▪ Advantages• Program is easier to understand and manage• Program is easier to debug and maintain• Code can be reused in other programs• Encapsulation of local data (black box)Function – a named block of code (consisting of a header and a body) that performs a specific task within the program.int main(void){// task 1statementA;statementB;statementC;// task 2statementD;statementE;statementF;return; }int main(void){task1();task2();}void task1(){statementA;statementB;statementC;return;}void task2(){statementD;statementE;statementF;return;}Functions▪ Calling function – function that makes the call▪ Passes zero or more pieces of data▪ Receives back zero or one piece of data as expression is evaluated▪ Called function – function that is called• Receives zero or more pieces of data• Processes the data (possible side effects)• Returns zero or one piece of dataFunctionsOptionalSide effectsOptionalParametersOptionalReturn ValueStandard Libraries#include <stdio.h>int main(void){...scanf(…);...printf(…);...}int scanf(…);int printf(…);...int scanf(…){...return …;}int printf(…);{...return …;}Declaration from stdio.hDefinition from linkerStandard LibrariesLibraryDescription<assert.h>Diagnostics Functions<ctype.h>Character Handling Functions<locale.h>Localization Functions<math.h>Mathematics Functions<setjmp.h>Nonlocal Jump Functions<signal.h>Signal Handling Functions<stdarg.h>Variable Argument List Functions<stdio.h>Input/OutputFunctions<stdlib.h>General Utility Functions<string.h>String Functions<time.h>Date and Time Functionsmath.hFunctionDescriptiondouble ceil(double x);Round up valuedouble floor(double x);Round down valuedouble fabs(double x);Compute absolute valuedouble modf(double x, double *ip);Break into integer and fractional partsdouble fmod(double x, double y);Compute remainder of divisiondouble pow(double x, double y);Compute powerdouble sqrt(double x);Compute square rootdouble exp(double x);Compute exponentialdouble frexp(double x, int *exp);Split into fraction and exponentdouble ldexp(double x, int n);Combine fraction and exponentdouble log(double x);Compute natural logarithmdouble log10(double x);Compute common logarithmdouble sin(double x);Compute sinedouble cos(double x);Compute cosinedouble tan(double x);Compute tangentdouble asin(double x);Compute arc-cosinedouble acos(double x);Compute arc-sinedouble atan(double x);Compute arc-tangentdouble atan2(double y, double x);Compute arc-tangent of quotientdouble sinh(double x);Compute hyperbolic sinedouble cosh(double x);Compute hyperbolic cosinedouble tanh(double x);Compute hyperbolic tangentmath.hConstantDescriptionM_EThe base of natural logarithmsM_LOG2EThe logarithm to base 2 of M_EM_LOG10EThe logarithm to base 10 of M_EM_LN2The natural logarithm of 2M_LN10The natural logarithm of 10M_PIPi, the ratio of a circle's circumference to its diameterM_PI_2Pi divided by twoM_PI_4Pi divided by fourM_1_PIThe reciprocal of pi (1/pi)M_2_PITwo times the reciprocal of piM_2_SQRTPITwo times the reciprocal of the square root of piM_SQRT2The square root of twoM_SQRT1_2The reciprocal of the square root of two#include <stdio.h>#include <math.h>int main(void){float radius;float area;printf("Enter the radius of a circle: ");scanf("%f", &radius);area = M_PI * pow(radius,2);printf("Area of a circle = %5.2f\n", area);return 0;}Enter the radius of a circle: 3Area of a circle = 28.27stdlib.hFunctionDescriptionintabs(int n);Absolute value of integerlong labs(long n);Absolute value of long integerdiv_tdiv(int num, int denom);Integer divisionldiv_tldiv(long num, long denom);Long integer divisiondouble atof(char *s);Convert string to floating-pointintatoi(char *s);Convert string to integerlong atol(char *s);Convert string to long integervoid* calloc(size_t nobj, size_t size);Allocate and clear memory blockvoid* malloc(size_t size);Allocate memory blockvoid* realloc(void *p, size_t size);Resize memory blockvoid free(void *p);Free memory blockintrand();Generate pseudo-random numbervoid srand(unsigned int seed);Seed pseudo-random number generatorvoid abort();Abort programvoid exit(int status);Exit from programintsystem(char *s);Perform operating system commandchar getenv(char *name);Get environment string…#include <stdio.h>#include <stdlib.h>#include <time.h>int main(){srand(time(NULL)); // provide the seed only onceprintf("%d\n",rand());printf("%d\n",rand());printf("%d\n",rand());printf("%d\n",rand());return 0;}5853522742998542391733269956198980185Standard LibrariesProgramming Standards: ▪ Any #include pre-processor directives must go at the top of your program, just below the program header comment.▪ You are only permitted to use the standard C libraries functions mentioned in the book or introduced in class up to the current chapter.User-Defined FunctionsDeclaration – used in the global declaration sectionFunction Call – place in program where function is invokedDefinition – code that performs the processingdata-type name (formal-parameter-list);name(actual-parameter-list);data-type name (formal-parameter-list){// declare local variables...// statements to process...return expression; // (or use return; if void)}x = name(actual-parameter-list);User-Defined Functions/************************************************************** Function: function name* Description: brief description of the function* Parameters: variable1 name, data type, and description* variable2 name, data type, and description* Return: data type and description*************************************************************/Programming Standards: Every user-define function (not including main) must be preceded with a header comment.User-Defined Functions▪ Can make use of any of these optional elements:• Parameter passing• Side effects• Return valueUser-Defined Functions#include <stdio.h>void greeting();int main(void){greeting();return 0;}void greeting(){printf("Hello World\n");return;}Side effectNo ParameterNo Return ValueUser-Defined Functions#include <stdio.h>int timestwo(int num);int main(void){int x = 3;x = timestwo(x);return 0;}int timestwo(int num){int y;y = num * 2;return y;}No Side effectParameterReturn Value#include <stdio.h>char letter1();char letter2();int main(){printf("%c\n",letter1());return 0;}char letter1(){printf("b");printf("%c",letter2());printf("%c",letter2());return 'a';}char letter2(){printf("a");return 'n';}Parameters▪ Formal


View Full Document
Download Functions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Functions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Functions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?