Are they the same? If so, which one is preferred by the more advance programmer and why?

Thanks,
Scott

Recommended Answers

All 3 Replies

>> Are they the same?
Kinda sorta, but not really. A switch is a control flow structure that directs execution through a certain block of code depending on an integral value. In other words, it's like door #1, door #2, and door #3 in the old gameshows. Those three doors together would be the body of a switch, 1, 2, and 3 would be the cases, and the condition is the very confused contestant trying to pick which one to open.

A function is a named block of code that can optionally accept data to work with and optionally return data when it's done. Think of a function like a drive through carwash. The carwash is the function, a dirty car going in is an argument, the body of the function cleans the car, and a clean car coming out is the return value.

Thanks Dogtree.. Let me know. If you were to do a multiple choice question, which would you use. Switch or Function?

Scott

Anytime you need to split off into one or more separate paths, you're looking at a switch or if chain.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.