Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPS125.

Similar presentations


Presentation on theme: "CPS125."— Presentation transcript:

1 CPS125

2 HOW TO DEFINE A FUNCTION IN C CALLING C FUNCTIONS
FUNCTIONS IN C HOW TO DEFINE A FUNCTION IN C CALLING C FUNCTIONS

3 Modular Programming Proper programming is always modular. Modular programming is accomplished by using functions. Functions: Separate program modules corresponding to the individual steps in a problem solution.

4 Functions We know already about the math library functions that are predefined for our use. Now we will learn how to program our own functions. Functions are “black boxes” that accept data and return results. Most library functions have 1 input and 1 result (like sqrt). Others have 2 inputs and one result (like pow).

5 Arguments We call the value sent to a function (the input) an argument. Functions can have many arguments but only one result. With sqrt (x), x is the argument. The result is sqrt(x) which can be assigned to a variable like in y=sqrt(x);

6 Functions in C An independent collection of code (separate from the main), Designed to perform ONE task, May return only ONE value, May return NO value, May accept any number of arguments, Same as subroutine, procedure, methods in other programming languages.

7 Dividing tasks into separate functions makes the program:
Easier to read/understand Easier to modify/debug Reusability Reliability

8 Defining functions A function is like a separate program that can be called by the main program to perform a specific task. The function definition is placed on top of the program code just after the preprocessor directives and before the main program. It is important that the function be declared before the main program because it has to exist before it can be used.

9 Defining functions A function from the math library is already defined. So, when you use it in a program you actually call the function. For a function that is defined by you, you will need first to define the function before using (or calling) it.

10 ‘C’ Function A function may call more than a function. Functions can be called from within main( ) or other functions. A function may call itself (recursion). The order of defining functions in the program is not important. Some of the useful functions have already been built into the C library.

11 Examples of such functions include:
puts( ) printf( ) scanf( )

12 How to define a function in C?
We need to: Declare the function (prototyping) Define the function

13 Declare Must be done at the beginning of the program before main( ) Explain how the function is to be used. General Syntax: type name (type par1, type par2, …);

14 The function prototype explains:
The type of the variable returned by the fn. Name of the fn. List and type of the parameters to be passed to the fn.

15 Defining functions Here is the syntax to define a function:
functiontype functionname (type and names of parameters separated by commas) { Local variable declarations; . . . C statements executed when function is called return (result); (only if there is one, must match function type) }

16 Functions in ‘C’ Q) Why do we need to pass parameters from the main to the function?

17 A) main( ) and functions are 2 independent bodies of code. As such those parameters (variables) that are local to the main are NOT known to the function. So, if we need to use some of those variables for our calculation in the function we need somehow pass them to the function.

18 Q? Why do we need to return a value from the function to the main?

19 A) as explained above the variables in the function are NOT known to the main as such if we need the result of the calculations in the function then we have to return it to the main.

20 Functions types: Functions have types just like variables:
float: function returns a single floating point number. int: function returns a single integer number. char: function returns a single character. void: function does not return any value. NOTE!!! Exit(0): a C function that causes normal termination of a program.

21 A function without argument nor result
/* a function definition */ void stars (void) { printf ("************************\n"); }. .. Inside the main program, the function would be called like this: stars ();

22 PROGRAM-FUNCTION /* A user-defined function without argument nor result */ #include <stdio.h> /* the function definition */ void stars (void) { printf ("********************************\n"); } /* end of function definition */ /* main program */ int main (void) printf ("The line of stars comes from a function.\n"); /* calling the function */ stars (); /* the function can be called as will*/ printf ("\n"); printf ("Hello world!\n"); return (0);

23 Function Definition To define a function, we need to define: Function Header Function Body

24 Define a function Function Header Function Body
Function type Function name Function parameter list Function Body Provide instructions performing the function task Depending on the function type, we need to end the function with a return( ) command and returns its argument.

25 Variables can be GLOBAL
Scope of Variables Variables have scope. Variables can be LOCAL Any variable declared within a function, is known only to that function and not of the others. i.e., a variable is LOCAL to the function in which it is declared. Variables can be GLOBAL If a variable is declared before the main( ), then it is GLOBAL and is known to all the functions in the C program.

26 Why we don’t necessarily like the Global Variables?
Scope… Why we don’t necessarily like the Global Variables? Can cause unexpected results. Any function can change the value of a global variable. Difficult to debug Good Programming Practice: Keep your variables as local as possible.


Download ppt "CPS125."

Similar presentations


Ads by Google