0% found this document useful (0 votes)
2 views

Week12_Functions

The document discusses modular programming in C, emphasizing the importance of functions as manageable units of code. It outlines the types of functions, including built-in and programmer-defined functions, and explains their structure, including function types, names, parameters, and bodies. Additionally, it provides examples and exercises to illustrate the concepts of function definitions and their applications in programming.

Uploaded by

armangezerer1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Week12_Functions

The document discusses modular programming in C, emphasizing the importance of functions as manageable units of code. It outlines the types of functions, including built-in and programmer-defined functions, and explains their structure, including function types, names, parameters, and bodies. Additionally, it provides examples and exercises to illustrate the concepts of function definitions and their applications in programming.

Uploaded by

armangezerer1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Functions

Asst. Prof. Dr. Damla TOPALLI,


Atilim University

CMPE113- 2020-21 Spring


Modular Programming
• The best way to develop and maintain a large program is to construct
it from smaller pieces or modules each of which is more manageable
than the original program.
• This technique is called as top-down design or divide and conquer.
Programs written using this technique is referred as modular
programs
• In C language, modules are called functions.
What is a function?
• a function is a small program performing a certain operation, such as
finding the square root of a given number.
• There are two types of functions:
• Built-in functions and (sqrt, pow, abs ...)
• programmer-defined functions.
• A function is a separate unit in a C program.
• One function cannot be defined in another function (nested function
definitions are NOT allowed.
Programmer-defined functions
• Programmer-defined functions are those written by the programmer
to define specific tasks that may be used at many points in a program.
• Functions are invoked by a function call.
• The function call specifies the function name and provides
information (arguments) that the function needs in order to perform
its designated task
Example of modular programming
• Given integer values for n and k, calculate
n!
⎯⎯⎯⎯⎯⎯
k! (n – k)!
• Notice that, to solve our problem we need three loops:
• One to calculate n!,
• one to calculate k!, and
• one to calculate (n – k)!.
• All those loops will be very similar to each other
Example of modular programming

• you can solve this problem by dividing it into two parts:


• Calculate the factorial of a number by writing a function.

• Read the values of n and k, calculate the required factorials by calling the
function you defined in the first part three times, and then calculate the
result of the formula by using the results obtained from the factorial function.
Function Definition
functionType functionName ( parameter list )
{
local variable declarations
other C statements body
}
Function type
• Function type indicates the data type of the result produced by the
function. (return)
• It may be int, double, char, etc.
• If the function will return no results, the function type must be void.
Function name
• Function name is the name chosen by the programmer.
• It must be a valid identifier, and it should indicate what the function is
doing
Parameters List
• (type1 parameter1, type2 parameter2, ... typen parametern)
• The parameters provide the means for communicating information
between functions.
• If the function does not have any parameters, void may be written in
place of the parameter list.
Function body
• The body of a function must be written within curly braces. It
may include local declarations and any number of statements.
• All variables declared in function definitions are local variables,
thus, they are known only in the function in which they are
defined.
• A function’s parameters are also local variables
Declaring functions
• Just like other identifiers in C, a function must
be defined before it can be referred (i.e.,
called).
• Thus, in a C program, the place of a function
definition is in between preprocessor
directives and main. Thus, the ordering of
modules in the source file is:
Preprocessor Directives
Function Definitions
Main
Declaring functions
• It is also possible to insert the
function prototype before the main
function, and defining the function
after the main. The function
prototype is the starting line of the
function definition ending with a
semicolon (not including the
function’s body).
• The names of the parameters may not
be written in the prototype, but their
data types must be indicated.
Two Questions:
1. Does the function take parameter(s)?
Functions.. 2. Does the function return a value?

1. Functions without arguments that return no value.


2. Functions with input arguments that return no value.
3. Functions without arguments that returns a single value.
4. Functions with input arguments that returns a single value.
1. Functions without arguments that return no value.

• General form: void functionname(void);


• Function call: functionname();

• Return type is void. Means no return.


• They don’t need any input.
• They don’t return any result to the caller.
• They perform a specific task.
1. Functions without arguments that return no value.

• Example 1: Write a C program


to display a rectangle or
triangle of stars depending on
users’ choice. Use two
functions to draw those shapes
and main program should only
call the function.
1. Functions without arguments that return no value.
1. Functions without arguments that return no value.

• Write a C Program to compute 1 + 2 + 3 + ….+ n by using a function.

The function total_n inputs the number n, calculates the total of numbers
from 1 to n and outputs the result. Main only calls this function.
1. Functions without arguments that return no value.
Solution (parameter?-No , return?-No)
2. Functions with input arguments that return no value.
Formal Parameters
• General form: void functionname(formal parameter list);
• Function call: functionname(actual parameter list);

• Formal parameter list: Variables declared in the


formal parameter list of the function header.
They are used to send values to the function.

• Actual parameter list: arguments, constants,


variables or expressions in a function call that Actual Parameters
corresponds its formal parameters.
2. Functions with input arguments that return no value.

Write a C Program to compute 1 + 2 + 3 + ….+ n by using a function.

The main program inputs n value from user and call the function
total_n. The function total_n calculates the total of numbers from 1 to
n and outputs the result.
2. Functions with input arguments that return no value.
Solution (parameter?-Yes , return?-No)
3. Functions without arguments that returns a single value.

• General form: functionType functionname(void);


• Function call: functionname();
3. Functions without arguments that returns a single value.

• Write a C Program to compute 1 + 2 + 3 + ….+ n by using a function.

The function total_n inputs n value from user, calculates the total of
numbers from 1 to n and returns the result to main program. In main,
call the function and output the result.
3. Functions without arguments that returns a single value.
Solution (parameter?-No , return?-Yes)
A function Only 1 value
Returning Values can receive
Function
returned
many values

• The datatype of the value returned from a function should


be specified in the header. If an integer value is returned
Example:
then:
int total_n(void){
..
return total;
}
• To use a returned value, we must either provide a variable
to store the value or we directly use it in an expression.
• In the main:
int s;
s= total_n();
4. Functions with input arguments that returns a single value

• General form: functionType functionname(formal parameter list);


• Function call: functionname (actual parameter list);
4. Functions with input arguments that returns a single value

• Write a C Program to compute 1 + 2 + 3 + ….+ n by using a function.

The main function inputs n from user and outputs the result.

The function total_n receives n as a parameter, calculates the total of


numbers from 1 to n and returns the result to main program.
4. Functions with input arguments that returns a single value
Solution (parameter?-Yes , return?-Yes)
Exercises on Functions

CMPE113- 2020-21 Spring


Exercise 1.
• Write a C program which takes 3 midterm grades of a student as an
input in main and send these numbers as parameter to a function.
This function should receive and display the average of grades.
Exercise 2.
• Write a function to calculate factorial of n (n!). The value of n should be
entered by the user in the main. The function will receive the value of n
as a parameter and returns the result.
Exercise 3.
• Write a program to compute the combination by using the formula.

𝒏!
𝑪(𝒏, 𝒓) =
𝒓! 𝒏 − 𝒓 !

The values of n and r should be taken from the user in the main program.
Then compute combination by using a function and return the result.
int combination (int n, int r);
Hint: We should use a function factorial to compute combination:
int factorial (int n);
SAMPLE RUN
Enter n and r values:3 2
C(3,2)=3
Exercise 3.
• Write a program to
compute the
combination by using
the formula.

𝒏!
𝑪(𝒏, 𝒓) =
𝒓! 𝒏 − 𝒓 !

You might also like