0% found this document useful (0 votes)
17 views11 pages

5 Functions

Functions are building blocks that allow programmers to divide programs into reusable modules. Functions take in parameters, perform operations, and return results. Common math functions are available in the math library and are called by providing the function name and arguments. Defining functions involves specifying the return type, name, parameters, and function body. Function prototypes declare the function signature to validate function calls.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views11 pages

5 Functions

Functions are building blocks that allow programmers to divide programs into reusable modules. Functions take in parameters, perform operations, and return results. Common math functions are available in the math library and are called by providing the function name and arguments. Defining functions involves specifying the return type, name, parameters, and function body. Function prototypes declare the function signature to validate function calls.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Programming Language

Functions
Functions
• Modules in C
• Programs written by combining user-defined functions with library
functions
▫ C standard library has a wide variety of functions
▫ Makes programmer's job easier - avoid reinventing the wheel
Function Call
• Invoking functions
▫ Provide function name and arguments (data)
▫ Function performs operations or manipulations
▫ Function returns results
• Boss asks worker to complete task
▫ Worker gets information, does task, returns result
▫ Information hiding: boss does not know details
Math Library Functions
Math library functions
▫ perform common mathematical calculations
▫ #include <math.h>
• Format for calling functions
FunctionName (argument);
▫ If multiple arguments, use comma-separated list
• printf( "%.2f", sqrt( 900.0 ) );
▫ Calls function sqrt, which returns the square root of its
argument
▫ All math functions return data type double
• Arguments may be constants, variables, or expressions
Benefits of Functions
• Divide and conquer
▫ Manageable program development
• Software reusability
▫ Use existing functions as building blocks for new programs
▫ Abstraction - hide internal details (library functions)
• Avoids code repetition
Function Definition
• Function definition format
return-value-type function-name( parameter-list )
{
declarations and statements
}
• Function-name: any valid identifier
• Return-value-type: data type of the result (default int)
▫ void - function returns nothing
• Parameter-list: comma separated list, declares parameters (default
int)
Function Definition
• Declarations and statements: function body (block)
▫ Variables can be declared inside blocks (can be nested)
▫ Function can not be defined inside another function

• Returning control
▫ If nothing returned
 return;
 or, until reaches right brace
• If something returned
▫ return expression;
Function Prototypes
• Function name
• Parameters - what the function takes in
• Return type - data type function returns (default int)
• Used to validate functions
• Prototype only needed if function definition comes after use in
program
int maximum( int, int, int );
▫ Takes in 3 ints
▫ Returns an int
Example
• Write down a function that will take an integer as parameter and
will return the reverse of the number.

int reverse (int numb) {


int r, sum = 0;
do{
r = numb%10;
numb = numb/10;
sum = sum*10+r;
}while (numb > 0);
return sum;
}
int main(){
int x, y;
x = 1243;
y = reverse(x);
printf("Reversing: %d Result %d", x , y);
}
Example
• Write down a function that will take two integers x and y as
parameter and will return xy.

int power(int x, int y) {


int i, p = 1;
for(i = 1; i <= y; i++)
p = p*x;
return p;
}
int main() {
int r;
r = power(2,5);
printf("Power of 2^5 is %d", r);
}
Example
• Write down a function that will take two integers x and y as
parameters and will return GCD(x, y).

int gcd(int x, int y) {


int i, gcd = 1,min;
min = (x > y)? y: x;
for(i = 2; i <= min; i++) {
if (x % i = = 0 && y % i = = 0)
gcd = i;
}
return gcd;
}
int main() {
int r;
r = gcd(12,15);
printf(“GCD of 12 and 15 is %d", r);
}

You might also like