Section 12.1 Single Functions
You have already seen the use of a function in C, namely the main()
function! We don’t tend to think of main()
as a function but it is indeed one.
Functions allow you to group a bunch of statements together that can be called upon to execute at any point in your code. Depending on the type of function you write, you can pass information to the function and it can return information back to you.
Here is a first example of a function in C:
If you cannot see this codecast, please click here.
Check Your Understanding Check Your Understanding
1.
Please add the correct prototype for the square()
function to the program below:
admin.....open in new window
When your program performs correctly you’ll be given a keyword to enter in Canvas.
2.
Write a function divide()
that takes two floats as arguments and returns a float, namely the first number divided by the second. Obviously, you shouldn’t be dividing by zero...
You may assume that your function is not called with such input.
admin.....open in new window
When your program performs correctly you’ll be given a keyword to enter in Canvas.