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

Week13_Functions_Call_by_Value

The document provides an overview of functions in C programming, including built-in and programmer-defined functions, as well as the concept of call-by-value. It outlines the structure of function definitions, examples of function usage, and the distinction between local and global variables. Additionally, it includes sample problems and solutions related to function implementation and variable scope.

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

Week13_Functions_Call_by_Value

The document provides an overview of functions in C programming, including built-in and programmer-defined functions, as well as the concept of call-by-value. It outlines the structure of function definitions, examples of function usage, and the distinction between local and global variables. Additionally, it includes sample problems and solutions related to function implementation and variable scope.

Uploaded by

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

Functions

Call-by-Value
Scope of Variables
Asst. Prof. Dr. Damla TOPALLI,
Atilim University

CMPE113- 2020-21 Spring


Recall: What is a function?
• A function is a small program performing a certain operation
• 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.
Recall: Built-in functions
Function sqrt as a “Black Box”
#include <math.h> includes definition of sqrt

© 2016 Pearson Education, Ltd. All rights reserved. 3


Recall: Built-in functions
• You need to prepend library name with #include<math.h>
• Examples
• abs(x)
• ceil(x)
• log(x)
• sin(x)
• sqrt(x)

© 2016 Pearson Education, Ltd. All rights reserved. 4


Recall: 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
Recall: Function Definition
functionType functionName ( parameter list )
{
local variable declarations
other C statements body
}
Recall: Functions..
Write a C Program to compute 1 + 2 + 3 + ….+ n by using a function named total_n.
1. Functions without arguments that return no value.
• void total_n (void); //Function prototype
2. Functions with input arguments that return no value.
• void total_n (int x);
3. Functions without arguments that returns a single value.
• int total_n ( );
4. Functions with input arguments that returns a single value.
• int total_n (int x);
The functions we have written up to now:
Received no parameters, or one or more input parameters,
Returned no result, or a single result.
Call by VALUE
• When we pass a parameter to a function, the value within the actual
parameter is sent to the function and put into the location of the
corresponding formal parameter.
• Thus, a copy of the actual parameter’s value is made and passed to
the called function.
• Therefore, when the function makes a change on the formal
parameter, no change happens on the actual parameter.
• Such calls are referred as call by value.
Example-1
Write a C program to calculate the total of numbers between two
integers n and m, where n and m are defined by the user.
Your program should call a function named total_n which takes two
parameters n and m to compute the sum and return the result.
SAMPLE RUN:
Enter two integers n & m (n<m): 3 10
Sum=52

CMPE113- 2020-21 Spring


Example-1- Solution
Write a C program to
calculate the total of
numbers between two
integers n and m, where
n and m are defined by
the user.
Your program should
call a function named
total_n which takes two
parameters n and m to
compute the sum and
return the result.
CMPE113- 2020-21 Spring
Example-2
• Write a function which receives three real numbers a, b and c,
computes and returns the sum of squares of those numbers.

Call-by-value
CMPE113- 2020-21 Spring
Example-3
• Write two functions which receives a radius of type double then: find_area
calculates and returns the area and find_circum returns the circumference.
Example-3
• Write two functions which receives a radius of type double then: find_area
calculates and returns the area and find_circum returns the circumference.
Call-by-value

circum = find_circum (radius);

© 2016 Pearson Education, Ltd. All rights reserved. 14


Example-4
• Write a function which receives an integer number n as a parameter,
then calculates and returns the sum of digits of that number.

SAMPLE RUN
Enter an integer:34564
Sum of digits:22
Example-4
• Write a function which receives an integer number n as a parameter, then calculates and returns
the sum of digits of that number.

SAMPLE RUN
Enter an integer:34564
Sum of digits:22
Example-5
• Write four functions to perform 4 basic mathematical operations: add, subtract, multiply and
divide.

• add function receives two integer parameters, computes and returns the sum of two
numbers.

• subtract function receives two integer parameters, computes and returns the difference of
two numbers.

• multiply function receives two integer parameters, computes and returns the product of two
numbers.

• divide function receives two integer parameters, computes and outputs the result. Do not
forget to check division by zero!

• In the main program, create a menu and continue until user wants to exit.
Example-5
SAMPLE RUN

Menu Menu

1.Add 1.Add

2.Subtract 2.Subtract

3.Multiply 3.Multiply

4.Divide 4.Divide

5.Exit 5.Exit

Enter two numbers:3 4 Enter two numbers:5 2

Enter your choice:1 Enter your choice:4

3+4=7 The result is 2.50


Example-5
Example-6
• Write a complete C program that reads 10 integer values and outputs a result for
each of these integers according to the following rule:
• Write two functions: a function that accepts an integer n and returns the factorial
of it. A function that accepts integer n and returns the sum as shown in below
formula.
Example-6

SAMPLE RUN:
Example-6
Scope of Variables
Inside a function or a block which is called local variables.

Outside of all functions which is called global variables.


• Local variables
– Declared inside body of given function
– Available only within that function. Local variables are not known to functions
outside their own.
• Global Variables
• A global variable can be accessed by any function. That is, a global variable is
available for use throughout your entire program after its declaration.

CMPE113- 2020-21 Spring


Example 7.
• What is the output for the given program?

CMPE113- 2020-21 Spring

You might also like