Why Use Functions? (2024)

Why Use Functions? (1)Why Use Functions? (2)Why Use Functions? (3)Why Use Functions? (4)Why Use Functions? (5)Why Use Functions? (6)Why Use Functions? (8)

So why is it useful for C programmers to divide their programs intoseparate--but cooperating--functions? Why not write every program as one big``chunk'' of statements?

Think back again to the homework for the previous lesson, in which you foundthe positions of a squatter's joints. In that program you used the built-insine and cosine functions.

Those functions were predefined for you, meaning that you didn't have to tellthe computer how to compute the sine and cosine of an angle. The work ofteaching the computer about sines and cosines had already been done for you.And because that programming job had already been done, you could simply usethose functions in your own program, without worrying about how the sine andcosine functions actually worked inside.

This example highlights the two most important reasons that C programmers usefunctions.

The first reason is reusability. Once a function is defined, it can beused over and over and over again. You can invoke the same function many timesin your program, which saves you work. Imagine what programming would be likeif you had to teach the computer about sines every time you needed tofind the sine of an angle! You'd never get your program finished!

Another aspect of reusability is that a single function can be used in severaldifferent (and separate) programs. When you need to write a new program, youcan go back to your old programs, find the functions you need, and reuse thosefunctions in your new program. You can also reuse functions that somebody elsehas written for you, such as the sine and cosine functions.

The second reason is abstraction. In order to use a particular functionyou need to know the following things:

  1. The name of the function;
  2. What the function does;
  3. What arguments you must give to the function; and
  4. What kind of result the function returns.

But notice: If you just want to use the function in your program, you don't have to know how it works inside! You don't have to understandanything about what goes on inside the function.

It's sort of like driving a car or using a telephone. With an automobile, youdon't need to understand every detail about the engine and drive train andwheels, if all you want to do is drive the car. Similarly, with a telephone,you don't have to understand everything about the phone system in order to makea call.

The only time you need to know how a function works inside is when youneed to write the function, or change how it works. (It's like a car again;you need to know how a car works in order to build one or fix one.) But once afunction is written and working, you never need to look at its insides again.

Together, these two reasons make functions extremely useful--practicallyessential!-for programmers who write large programs. The ability todivide a program into abstract, reusable pieces is what makes it possible towrite large programs that actually work right.

Why Use Functions? (9)Why Use Functions? (10)Why Use Functions? (11)Why Use Functions? (12)Why Use Functions? (13)Why Use Functions? (14)Why Use Functions? (16)
Eric N. Eide
Hamlet Project
Department of Computer Science
University of Utah
Why Use Functions? (2024)
Top Articles
Marketing Finance 101: How to dissect your brand's financial statement
Bracing for Impact: How to Prepare and Strategize for a Stock Market Crash
9.4: Resonance Lewis Structures
Jail Inquiry | Polk County Sheriff's Office
Mcgeorge Academic Calendar
Farepay Login
Access-A-Ride – ACCESS NYC
Watch Mashle 2nd Season Anime Free on Gogoanime
Santa Clara College Confidential
Hawkeye 2021 123Movies
RuneScape guide: Capsarius soul farming made easy
Tx Rrc Drilling Permit Query
Ree Marie Centerfold
I Wanna Dance with Somebody : séances à Paris et en Île-de-France - L'Officiel des spectacles
Aldi Bruce B Downs
Juicy Deal D-Art
Hobby Stores Near Me Now
12 Top-Rated Things to Do in Muskegon, MI
Bethel Eportal
Dark Entreaty Ffxiv
Inbanithi Age
Devotion Showtimes Near Regency Buenaventura 6
Meridian Owners Forum
Gilchrist Verband - Lumedis - Ihre Schulterspezialisten
Where to eat: the 50 best restaurants in Freiburg im Breisgau
Rs3 Bring Leela To The Tomb
1964 Impala For Sale Craigslist
Till The End Of The Moon Ep 13 Eng Sub
Kleinerer: in Sinntal | markt.de
R/Mp5
James Ingram | Biography, Songs, Hits, & Cause of Death
Kids and Adult Dinosaur Costume
Wow Quest Encroaching Heat
American Bully Xxl Black Panther
Elisabeth Shue breaks silence about her top-secret 'Cobra Kai' appearance
Mvnt Merchant Services
Restored Republic May 14 2023
How To Upgrade Stamina In Blox Fruits
Ferguson Showroom West Chester Pa
Weekly Math Review Q2 7 Answer Key
2013 Honda Odyssey Serpentine Belt Diagram
My Eschedule Greatpeople Me
Hawkview Retreat Pa Cost
Canada Life Insurance Comparison Ivari Vs Sun Life
Upcoming Live Online Auctions - Online Hunting Auctions
Puss In Boots: The Last Wish Showtimes Near Valdosta Cinemas
O'reilly's On Marbach
Bluebird Valuation Appraiser Login
Billings City Landfill Hours
Overstock Comenity Login
Access One Ummc
Latest Posts
Article information

Author: Msgr. Refugio Daniel

Last Updated:

Views: 5826

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Msgr. Refugio Daniel

Birthday: 1999-09-15

Address: 8416 Beatty Center, Derekfort, VA 72092-0500

Phone: +6838967160603

Job: Mining Executive

Hobby: Woodworking, Knitting, Fishing, Coffee roasting, Kayaking, Horseback riding, Kite flying

Introduction: My name is Msgr. Refugio Daniel, I am a fine, precious, encouraging, calm, glamorous, vivacious, friendly person who loves writing and wants to share my knowledge and understanding with you.