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

week9-function

The document discusses the concept of functions in programming, emphasizing their role in breaking down problems into manageable tasks and improving code readability. It provides examples of function definitions, such as calculating the square of a number, and explains the 'pass by value' method for function arguments. Additionally, it includes several exercises for defining and implementing various functions, such as checking for prime numbers and calculating salaries.

Uploaded by

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

week9-function

The document discusses the concept of functions in programming, emphasizing their role in breaking down problems into manageable tasks and improving code readability. It provides examples of function definitions, such as calculating the square of a number, and explains the 'pass by value' method for function arguments. Additionally, it includes several exercises for defining and implementing various functions, such as checking for prime numbers and calculating salaries.

Uploaded by

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

Week 9

Function
Functions

•a group of declarations and statements that is


assigned a name
•a sub-program
• when we write our program we always define a
function named main
• inside a function we can call other functions
Example: Square

double square(double a)
{ This is a function defined
return a * a; outside main
}

int main()
{
double num = 0.0, sqr = 0.0;

printf("enter a number\n");
scanf("%lf",&num);

sqr = square(num); Here is where we call


the function square
printf("square of %g is %g\n", num, sqr);

return 0;
}
Example: Square
double square(double a); /* prototype */
int main()
{
double num = 0.0, sqr = 0.0;

printf("enter a number\n");
scanf("%lf",&num);

sqr = square(num); Here is where we call the


function square
printf("square of %g is %g\n", num, sqr);

return 0;
}

double square(double a)
{
return a * a;
}
Why use functions?

•Break your problem down into smaller sub-tasks


• easier to solve complex problems
•Generalize a repeated set of instructions
• we don’t have to keep writing the same thing over and
over
• printf and scanf are good examples…
•They make a program much easier to read and
maintain
Pass by value

•Function arguments are passed to the function


by copying their values rather than giving the
function direct access to the actual variables
•A change to the value of an argument in a
function body will not change the value of
variables in the calling function
Pass by value
void swap (int a, int b)
{
int temp = a;
a = b;
b = temp;
}

int main ()
{
int a = 10;
int b = 20;
printf ("Before swap, a = %d, b = %d\n", a, b);
swap (a, b);
printf ("After swap, a = %d, b = %d\n", a, b);
return 0;
}
Exercise 9.1

1. Define a function isPrime that accepts a


positive integer and returns 1 if it’s a prime
number, and 0 otherwise.
prototype: int isPrime(int n);
2. Now write a program that gets a positive
integer from the user and prints all the prime
numbers from 2 up to that integer.
Use the function isPrime
Exercise 9.2

• Define three functions as follows:


• A function to calculate sum of the cube of integers
from 1 to n (input n, output 13 + 23 +..+ n3)
• A function to list all factors of the integer n
• A function to list the first n square numbers
• Write a program that uses three functions
above. The program asks users to enter a
number, then call the corresponding function.
Exercise 9.3

•Define a function to calculate the worker’s salary


by a week. The pay is 50.000 VND for one
working hour. The workers need to do 40 hours
a week. If they work overtime, the money is paid
more 1.5 time for each hour.
•Write a program that asks users to provide the
number of working hours, then show the salary
of this person.
•Data validation: A worker can not work less than
10 hours or more than 65 hours a week.
Exercise 9.4
• Write an algorithm isLeapYear as a function that
determines whether a given year is a leap year.
Pass the year as a parameter. A year is a leap
year if
• It is a multiple of 4 but not a multiple of 100 OR
• It is a multiple of 400
• For example, 1996 and 2000 are leap years, but 1900,
2002 and 2100 are not.
• Use this function in a program that asks from
user the month, year and return the number of
days of the month.
Exercise 9.5

• Write a function that counts the occurrences of


vowel characters (a, e, u, i, y, o) in a sequence
of characters
• Using this function in the program that
requires the user to enter a string. Print out the
number of vowel characters in this string.
Exercise 9.6

•Define a function that returns the number of


years until a father will have an age double of its
son’s age.
•Write a program that asks the ages of father and
his son. Use the defined function to show the
result.
Exercise 9.7

• Write a program to convert a decimal number


to a binary number using the function.

• Example:
Input:
any decimal number : 65
Output :
The binary value is : 1000001

You might also like