Full Transcript

# CPE-PC112 Programming Logic and Design ## 03 Functions **Function** The basic philosophy of function is divide and conquer by which a complicated tasks are successively divided into simpler and more manageable tasks which can be easily handled. A program can be divided into smaller subprograms...

# CPE-PC112 Programming Logic and Design ## 03 Functions **Function** The basic philosophy of function is divide and conquer by which a complicated tasks are successively divided into simpler and more manageable tasks which can be easily handled. A program can be divided into smaller subprograms that can be developed and tested successfully. ## Advantages of using functions: 1. It facilitates top down modular programming. 2. The length of the source program can be reduced by using functions at appropriate places. 3. It is easy to locate and isolate a faulty function for further investigation. 4. A function may be used by many other programs. This means that a C programmer can build on what others have already done, instead of starting over from scratch. 5. A program can be used to avoid rewriting the same sequence of code at two or more locations in a program. 6. Programming teams does a large percentage of programming. If the program is divided into subprograms, each subprogram can be written by one or two team members of the team rather than having the whole team to work on the complex program. ## Function - A function is a complete and independent program which is used (or invoked) by the main program or other subprograms. - A subprogram receives values called arguments from a calling program, performs calculations and returns the result to the calling program. ## Difference between the main program and the function | | Main Program | Function | |---------------|---------------|---------------------| | **Input** | scanf() | parameters | | **Output** | printf() | return | ## Function - Unless the function is supposed to do an actual input, we try **NOT** to put `scanf` inside. Instead, whatever the function needs as input, we pass it as **parameter.** - The output of the function on the other hand is returned to the user through the **return statement.** - This way, function is a stand-alone function. ## Two types of functions: 1. **Built-in function or Predefined Standard Function** - e.g. `main()`, `scanf()`, `printf()`, `pow()`, `toupper()`, `strchr()` - These functions are already defined in header files (files with `.h` extensions are called header files such as `stdio.h`), so we just call them whenever there is a need to use them. - For a comprehensive list of C standard libraries refer to: https://www.tutorialspoint.com/c_standard_library/index.htm ## Two types of functions: **Predefined Standard Function** **Example:** The C Standard Library "math.h" - This header defines various mathematical functions and one macro. All the functions available in this library take double as an argument and return double as the result. - `double acos(double x)`: Returns the arc cosine of x in radians. - `double asin(double x)`: Returns the arc sine of x in radians. - `double atan(double x)`: Returns the arc tangent of x in radians. ## Two types of functions: **Predefined Standard Function** **Example:** math.h - `double cos(double x)`: Returns the cosine of a radian angle x. - `double sin(double x)`: Returns the sine of a radian angle x. - `double tan(double x)`: Returns the tangent of a radian angle x. - `double pow(double x, double y)`: Returns x raised to the power of y. - `double sqrt(double x)`: Returns the square root of x. ## Two types of functions: **Predefined Standard Function** **Example:** math.h - `double ceil(double x)`: Returns the smallest integer value greater than or equal to x. - `double fabs(double x)`: Returns the absolute value of x. - `double floor(double x)`: Returns the largest integer value less than or equal to x. - `double fmod(double x, double y)`: Returns the remainder of x divided by y. ## Two types of functions: **User / Programmer-defined Function** 2. **User / Programmer-defined Function** - The functions that we/users create in a program. - **Syntax**: `returndata_type functionName (parameter list)` ```c { <local variable declaration> statements; Block of code return expression; } ``` - **Note:** No function can have the same name as the one of C's reserved words. ## User / Programmer-defined Function where: - **return dataType:** Return type can be of any data type such as int, double, char, void, short etc. A C function returns a value of type int as the default data type when no other type is specified explicitly. - **function name:** It can be anything, however it is advised to have a meaningful name for the functions so that it would be easy to understand the purpose of function just by seeing it's name. - **parameter list:** a list (separated by commas) contains variables names along with their data types. These arguments are kind of inputs for the function. - **Block of code:** Set of C statements, which will be executed whenever a call will be made to the function. ## WHAT ARE PARAMETERS - Parameters serve as a way to communicate information to a function. One use of parameters as stated earlier is to be able to supply input to the function. - You need to declare variables inside the parameter brackets to store the values of the parameters that are passed to the function. ## 2 types of Parameter - **Formal Parameters:** are parameters as they appear in function declarations - **Actual Parameters/Arguments:** are parameters as they appear in function calls ## Example: ```c #include <stdio.h> } int calcSum (int augen, int addend) { int sum= augen+addend; return sum; int main() { int sum, op1,op2; scanf("%d %d",&op1,&op2) sum= calcSum (op1, op2); printf("The sum of %d and %d is %d", op1,op2,sum); return 0; } ``` ## Parameter Passing Two types: - **Pass by reference:** the variable argument is renamed, not copied. Only the address of the argument is passed to the function. - **Pass by value:** a copy of the data is passed to the subroutine, so that no matter what happens to the copy the original is unaffected. **Note:** some books in C refers to parameter passing as call-by-value and call-by-reference, they are synonymous. ## Example ```c int feetToInches(int feet) { feet 5 int inches; inches = feet * 12; Value is inches 60 return inches; copied at the point of call } void main() f 5 { int f = 5; i = 0; i= feetToInches(f); i 0 } ```

Use Quizgecko on...
Browser
Browser