You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Open PuTTY/Terminal and connect to the Linux server.
Create a minimal C program in your editor, save it as function.c and transfer it to the Linux server. Compile and run the program to make sure everything is working.
#include<stdio.h>intmain() {
return0;
}
Part 1
Define a function that takes a single argument of type double, representing the radius of a circle,
and returns a value of type double that is the area of a circle with the given radius. Name the function
areaOfCircle.
Use the function you defined twice in the main function of your program, passing in different values each
time and printing the result.
Part 2
Define a function that takes a single argument of type double, representing the length of a side of square
and returns a value of type double that is the area of the square with the given side length. Name the
function areaOfSquare.
Part 3
Define a function that takes two arguments of type double. The first argument represents the height of
a triangle. The second argument represents the width of the base of the triangle. The function should
return a value of type double that is the area of the triangle with the given width and height. Name
the function areaOfTriangle.
Part 4
Define a function that takes two arguments of type int. The function does not return a value, but prints
the sum of the two numbers. The output should include the phrase "The sum is " and a new line, for example
if the sum is 10, it should print "The sum is 10" followed by a new line.
Someone asked a question last class that was something like: "How can we compile to something other than a.out"? You can do this by running gcc with the -o flag and providing a filename, like this:
gcc -o myOutput.out myProgram.c
./myOutput.out
Review - printing numbers
We will cover input and output in more detail later. But for now, here is a variety of ways you can print a number in C:
/* 1. Printing a single whole number: the following statement will print the number 10 followed by a new line. */printf("%d\n", 10);
/* 2. Printing a single decimal number: the following statement will print a floating point representation of the number 10.2 followed by a new line. */printf("%lf\n", 10.2);
/* 3. In statements 1 and 2, you can think of `%d` and `%lf` as placeholders - `printf` is replacing the placeholder with the number that appears after the comma. This means you can combine the placeholder with other text to print longer messages. The following statement will print the text "The number 10 is one more than 11" followed by a new line. */printf("The number %d is one more than 10\n", 11);
/* 4. The number in each of the above statements can be replaced with any expression that results in a value of the appropriate type, like a mathematical operation. The following will print the same thing as statement #3. */printf("The number %d is one more than 10\n", 5+6);
/* 5. In a similar fashion you can also print numbers that are stored in variables. Assuming you had the value 11 stored in a variable of type int called result, the following would print the same thing as statements 3 & 4. */printf("The number %d is one more than 10\n", result);
Functions
A function is a named, reusable section of code.
A function in C can accept zero or more arguments and it can optionally return
a value. Arguments are the inputs to a function and the return value is the output.
You can think of this as somewhat analagous to a mathematical function such as:
f(x, y) = 2x + y
Here the function f accepts two arguments - x and y. It returns a value that is the result of
the calculation. So, f(2, 4) would return the result 8. A function in C is a bit more
general purpose - it isn't limited to calculating and returning numbers. It can accept
arguments of any data type, perform any task that can be expressed in code, and return any
type of value.
Functions create reusable block of code. Therefore they are extremely useful for avoiding repetition.
You can define some logic one time, and use it multiple times in your program.
Defining a Function
You are already familiar with the general syntax for defining a function because main
is a function:
intmain()
{
return0;
}
This defines a function with the name main that accepts no arguments and returns an integer.
The mathematical function from above can be defined in C like this:
intf(intx, inty)
{
return2*x+y;
}
In the above defintion, x and y are called parameters. They specify what arguments you can
pass to the function as input. In this case, the parameters say you can pass two arguments to the
function f, each of type int. The code between the braces is called the function body. It
defines the work that the function is going to do. Withing the function body you can use the parameters
as you would variables - they are initialzed with the values that are passed in as arguments (i.e. the
first argument passed is assigned to x and the second is assigned to y) To return a value, you
use the keyword return followed by an expression that has a value that matches the return type.
Using a Function
To use a function you call or invoke the function by providing its name
and a list of arguments. The arguments are enclosed in parenthesis and separated
by commas. For example, the following:
intresult=f(2, 4); // 8 is assigned to result
Functions must be defined before you can use them. For example:
Functions don't have to return a value. To define a function that does not return a value, use
the keyword void as the return type. For example:
voidprintANumber(inttheNumber)
{
printf("You gave me the number: %d\n", theNumber);
}
Functions that don't return a value can't be used in expressions like assignments and mathematical
operations. You simply invoke them:
voidprintANumber(inttheNumber)
{
printf("You gave me the number: %d\n", theNumber);
}
intmain() {
printANumber(10); // prints 10return0;
}
Example program from class:
#include <stdio.h>
// f(x, y) = 2x + 4y
int f(int x, int y)
{
return 2 * x + 4 * y;
}
void printTheSum(int x, int y)
{
printf("The sum is %d\n", x + y);
}
int main()
{
printf("Before the function\n");
int result1 = f(2, 3);
int result2 = f(3, 4);
printf("The result is %d\n", result1);
printf("The result is %d\n", result2);
printf("After the function\n");
printTheSum(4, 3);
printTheSum(2, 6);
return 0;
}